trentm / node-bunyan

a simple and fast JSON logging module for node.js services
Other
7.15k stars 513 forks source link

Add extra metada into log record #680

Open akravets opened 2 years ago

akravets commented 2 years ago

Hello,

Currently I am only aware of doing this using serializers: logger.error({ req: request, err: err }, "message");

I'd like to achieve this without needing to pass request object to each log call, but rather set this data when logger is initialized and appear in logs when log is called:

logger = require("lgger") logger.setId(req.getId()) logger.info("test")

// INFO - 55 - test

Doesn't have to be the exact approach, but some variation of this. Would this be possible with child log feature?

gratus-acuitymd commented 1 year ago

I think you can do this with

const child = logger.child({requestId: req.getId()});
child.info("something");

That should now include requestId see https://github.com/trentm/node-bunyan#logchild