trentm / node-bunyan

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

Added support for Request ID generators #670

Open slavafomin opened 3 years ago

slavafomin commented 3 years ago

When processing HTTP requests, it's a good practice to specify a unique request ID value for each log entry. This way, multiple log entries could be grouped together according to the specified request ID, which dramatically helps in debugging/analysing requests processing.

This PR allows to specify custom request ID generation function for logger instances. Which opens a path for easy cls-rtracer integration. Please see the updated README for more details and examples.

This should be considered a non-breaking minor release.



So I'm not sure regarding the maintenance status of this project, I've created and published a forked version of the library, so it could be used straight away if someone would want to.

slavafomin commented 3 years ago

Hello @trentm, could you look into this, please?

LewisCowlesMotive commented 1 year ago

Feels like it should be separate to a logger, and bound to .child

jmaver-plume commented 5 months ago

Any updates on this?

jmaver-plume commented 5 months ago

For anyone who wants to use something similar, you can take advantage of the get accessor property.

const logger = bunyan.createLogger({ ...options })
Object.defineProperty(log.fields, 'traceId', {
  enumable: true,
  get: function () {
    return 'some-id' // e.g., you can retrieve it from AsyncLocalStorage
  }
})