pinojs / pino-mongodb

:evergreen_tree: Insert JSON from stdin into MongoDB
MIT License
54 stars 18 forks source link

[Question] Any better way to implement multi loggers? #154

Closed maicss closed 10 months ago

maicss commented 10 months ago

Goal

add more than one logger for my project, like:

aka: I want a mongo version of name option: https://getpino.io/#/docs/api?id=name-string

Idea

Add one field to transport message is easy: 1, add a name filed in targets options

import { pino } from 'pino'
const transport = pino.transport({
  targets: [
    { target: 'pino-pretty', options: { destination: 1 } },
    {
      target: '../pino-mongo-transport.mjs',
      options: {
        uri: 'mongodb://xxx',
        collection: 'logs',
        database: 'someDB',
        unified: false,
+       name: 'API',
        mongoOptions: {
          appName: 'pino-mongodb',
          retryWrites: true,
          w: 'majority',
        },
      },
    },
  ],
})
export default pino(transport)

2, add name filed to the object for transport, like this: https://github.com/pinojs/pino-mongodb/blob/master/lib/log.js#L9-L14

log.name = options.name

So far, I get what I need, save a document with a field named name, and value is API.

Question

Because webwork cannot share a mongodb connect cursor, so, if I have 10 different loggers, I should write 10 different transport, and keep 10 mongo connections? Is there any way to add the name field to the pino object before logger pipe all the transport?