如何使用 Pino 和 Express.js 控制台登录终端并同时写入文件

dok*_*han 7 javascript node.js express pinojs

我创建了这个简单的函数来记录代码中发生的任何情况:

const pino = require('pino')

module.exports = pino({
  transport: {
    target: "pino-pretty",
    options: {
      translateTime: "SYS:dd-mm-yyyy HH:MM:ss",
      ignore: "pid,hostname",
      destination: './logs/logs.txt'
    }
  }
})
Run Code Online (Sandbox Code Playgroud)

的问题destination。如果此选项是,pino将把所有内容写入文件中,而不是控制台中,如果没有,将在控制台中打印,但不打印在文件中。我想在控制台中打印并同时写入日志。

这可能吗?

小智 7

I know this is old, but I thought I'd drop this in as I'm working with it right now.

transport: {
    targets: [
        {
            level: 'info',
            target: 'pino-pretty',
            options: {}
        },
        {
            level: 'trace',
            target: 'pino/file',
            options: { destination: './pino-logger.log' }
        }
    ],
},
Run Code Online (Sandbox Code Playgroud)

You can set up multiple targets and distinguish which will receive what levels and options. Hope that helps anyone still looking around.