node-bunyan icon indicating copy to clipboard operation
node-bunyan copied to clipboard

Added support for Request ID generators

Open slavafomin opened this issue 3 years ago • 4 comments

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.


  • added support for Request ID generators
  • introduced optional req_id property that can be passed to the logger constructor
  • added tests
  • updated README
  • updated .gitignore file to exclude IDEA files
  • arranged AUTHORS alphabetically
  • fixed "engines" property of the package manifest
  • added myself as an author

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 avatar Jul 26 '21 17:07 slavafomin

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

slavafomin avatar Sep 07 '21 14:09 slavafomin

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

LewisCowlesMotive avatar Apr 24 '23 07:04 LewisCowlesMotive

Any updates on this?

jmaver-plume avatar Apr 09 '24 17:04 jmaver-plume

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
  }
})

jmaver-plume avatar Apr 10 '24 11:04 jmaver-plume