GithubHelp home page GithubHelp logo

Comments (11)

waynebloss avatar waynebloss commented on June 10, 2024 4

I'd like this option as well.

The suggested 11.11.111.111 - GET /url/path - Status Code: 200 format would be preferred in any configuration instead of the default request completed message, however i'd also add an option to remove details of req and res unless I opt into getting them for specific endpoints.

This code looks easy enough to fork though, so I won't wait around too long for it.

from pino-http.

clemenspeters avatar clemenspeters commented on June 10, 2024 3

Might customSuccessMessage be a solution for you?
Similar to:

const requestLogger = require("express-pino-logger")({
  customSuccessMessage: function (res) {
    return `${res.req.ip} - ${res.req.originalMethod} ${res.req.originalUrl} - ${res.statusCode}`;
  },
});

(See

var successMessage = opts.customSuccessMessage || function () { return 'request completed' }
)

from pino-http.

rstoermer avatar rstoermer commented on June 10, 2024 2

How about using pino-pretty in the following way?

const options = {
    name: "example",
    ...(process.env.NODE_ENV !== "production"
        ? {
              prettyPrint: {
                  ignore: 'req,res,responseTime',
                  messageFormat: (log, messageKey, levelLabel) => {
                      if (log.req) return `${log.responseTime}ms - ${log.msg} - ${log.req.url}`;
                      return `${log.msg}`;
                  }
              }
          }
        : null)
};

from pino-http.

davidmarkclements avatar davidmarkclements commented on June 10, 2024 1

from pino-http.

mcollina avatar mcollina commented on June 10, 2024

I'm -1 in removing the lines from pino-http for formatting reasons. I'm happy to review a PR that make things more flexible here.

from pino-http.

taylorjdawson avatar taylorjdawson commented on June 10, 2024

Okay I did some digging and I think what I am looking for is something like this. I think it would be possible by using the stream like you did to manipulate the logging format. Is that true? If so I guess it wouldn't apply to the pino-http module.

from pino-http.

waynebloss avatar waynebloss commented on June 10, 2024

I couldn't get pino-http-print to work at all.

Attempt 1:

import express from "express";
import pino from "pino";
import expressPino from "express-pino-logger";
import pinoHttpPrint from "pino-http-print";

const log = pino({
  prettyPrint: {
    translateTime: "SYS:yyyy-mm-dd HH:MM:ss.l",
  },
});

const app = express();

app.use(expressPino(pinoHttpPrint()));

Attempt 2:

app.use(expressPino(pinoHttpPrint({ all: true })));

Attempt 3:

app.use(
  expressPino({
    logger: log,
    stream: pinoHttpPrint({ all: true }),
  }),
);

Anyway, this is probably the wrong place for this comment but instead of starting new issues that I don't really care about I figured I'd just leave it here, fork pino-http and do what I want just for my own projects.

This seems like a really good logger but it's just too bad that I have to compose 3 different packages together just to get some useful Express logging without all of the req/res diarrhea.

from pino-http.

rohit-gohri avatar rohit-gohri commented on June 10, 2024

I couldn't get pino-http-print to work at all.

An update was released for pino-http-print right after this that added some options, I suggest you check it out again.

The difference is that the pino-http-print module returns a factory instead of a print stream directly. So you have to call it again to get a stream.

My current setup:

const express =  require("express");
const pino = require('pino');
const pinoPrint = require('pino-http-print');
const expressPino = require('express-pino-logger')

const prettyPrintFactory = pinoPrint({
	all: true,
	relativeUrl: true,
	translateTime: 'SYS:standard',
});

const prettyPrint = prettyPrintFactory();

const log = pino(prettyPrint);

const app = express();

app.use(expressPino(prettyPrint));
// OR reuse existing pino instance instead
// app.use(expressPino({logger: log}));

app.get('/test', (req, res) => {
    res.send('OK');
})

app.listen(3000, () => {
    log.info('Listening now...');
});

Output:

[2020-02-16 18:52:07.083 +0530] INFO  (22235 on lg-gram-elementary): Listening now...
[2020-02-16 18:52:11.892 +0530] GET /test 200
[2020-02-16 18:52:12.007 +0530] GET /favicon.ico 404

from pino-http.

davidmarkclements avatar davidmarkclements commented on June 10, 2024

@waynebloss to be clear, there is a much simpler pattern for this http://getpino.io/#/docs/pretty?id=pretty-printing

you pipe the JSON output to a prettifer, that's it.

from pino-http.

rohit-gohri avatar rohit-gohri commented on June 10, 2024

you pipe the JSON output to a prettifer, that's it.

pino-http-print also works as a command. Equivalent example would be:

node index.js | pino-http-print -a -r -t 'SYS:standard'

with the below index.js file.

// index.js
const express =  require("express");
const expressPino = require('express-pino-logger')

const app = express();
app.use(expressPino());

Though setting up pino with a prettifier conditionally in dev environment is also pretty convenient.

@davidmarkclements Would a pino-http-print example in the pretty printing section be a good idea for the docs? I know a few people who migrated from morgan to pino/express-pino but had a hard time setting up pretty printing for dev environments for request logs.

from pino-http.

giovanni-bertoncelli avatar giovanni-bertoncelli commented on June 10, 2024

I'd like to have that too... The pino-http-print does not work for me...
I'll adopt the @clemenspeters solution but it will be a better if I can have req and res already serialized as parameters there (there is no way to access responseTime in the customSuccessMessage function...).

This solution is actually a workaround and if I want to have only that message I have to put req and res to null in the serializers...

from pino-http.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.