GithubHelp home page GithubHelp logo

Comments (10)

mcollina avatar mcollina commented on June 10, 2024

Can you add an example to reproduce the problem you are facing?

from pino-http.

GuskiS avatar GuskiS commented on June 10, 2024

I have something like this:

router.use(pinoHttp(), (req, res, next) => {
  req.log = req.log.child({  something: true })
  next();
});

router.get("/example", (req, res) => {
  req.log.info("some message")
  res.json({ ok: true });
});

req.log.info("some message") will have something: true in its output, however, the request completed will not have it, because that one relays on res.

I hope this make it more clear.

from pino-http.

davidmarkclements avatar davidmarkclements commented on June 10, 2024

if you're always binding the same value, this isn't a good pattern, instead do

const logger = require('pino')().child({ something: true })
const pinoHttp = require('pino-http')({ logger })
router.use(pinoHttp)

from pino-http.

davidmarkclements avatar davidmarkclements commented on June 10, 2024

for injecting values on a per request basis use the serializers

from pino-http.

GuskiS avatar GuskiS commented on June 10, 2024

Value changes per request. I tried setting that value on req object, however that didn't show up in serializers from req.raw.

from pino-http.

mcollina avatar mcollina commented on June 10, 2024

I think it would be helpful to have a full example of what you are trying to do.

from pino-http.

GuskiS avatar GuskiS commented on June 10, 2024

#74 (comment)
This is almost the same thing as I'm doing, but here goes:
1.

app.use(pinoHttp());

const originalRouteMiddleware = (originalRoute) => (req, res, next) => {
  req.log = req.log.child({ originalRoute });
  next();
});
app.use(pinoHttp({
  serializers: {
    req(req) {
      req.originalRoute = req.raw.originalRoute;
      return req;
    },
  },
}));

const originalRouteMiddleware = (originalRoute) => (req, res, next) => {
  req.originalRoute = originalRoute;
  next();
});

Route:

router.get("/user/:userId", originalRouteMiddleware("/user/:userId"), (req, res) => {
  req.log.info("some message");
  res.json({ ok: true });
});

So the issues I'm facing:

  1. I would need to do req.log = res.log = res.log.child({ originalRoute }); to make it work, which is misleading and not obvious if you don't take a look at sourcecode. Also, as suggested, not a good pattern.
  2. In this case req.raw doesn't contain my originalRoute, not sure why tho. Maybe .child creates copy of original object? 🤔

from pino-http.

mcollina avatar mcollina commented on June 10, 2024

May I get a full example that I can run? Ideally without express (or maybe this is express-only).

from pino-http.

GuskiS avatar GuskiS commented on June 10, 2024

Here it is: https://github.com/GuskiS/pino-express-failure

from pino-http.

mcollina avatar mcollina commented on June 10, 2024

In this case req.raw doesn't contain my originalRoute, not sure why tho. Maybe .child creates copy of original object?

Child serializes the request object once at the time the log line is created. In order for that property to show up, you'd have to add it in a middleware before the pino-http one.

I do not think there is any other way than req.log = res.log = res.log.child({ originalRoute }); tbh.
You could potentially disable autoLogging and log after you have set that value.

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.