简体   繁体   English

为什么 Winston 不将错误记录到文件中?

[英]Why does Winston not log errors to file?

I am new to Winston, when there is an error on the code it gets logged to the console but not to the log file.我是 Winston 的新手,当代码出现错误时,它会记录到控制台而不是日志文件。 Infos get logged to another file correctly but errors don't.信息正确记录到另一个文件,但错误不会。

Here's my logger.js file:这是我的 logger.js 文件:

const winston = require('winston')

let logger

module.exports.init = () => {
  logger = winston.createLogger({
    level: 'info',
    format:  winston.format.simple(),
    transports: [
      new winston.transports.Console(),
      new winston.transports.File({ filename: './logs/error.log', level: 'error' }),
      new winston.transports.File({ filename: './logs/info.log' })
    ]
  })

  process.on('uncaughtException', ex => {
    logger.error(ex.message)
    process.exit(1)
  })

  process.on('unhandledRejection', ex => {
    logger.error(ex.message)
    process.exit(1)
  })
}

module.exports.use = () => logger

I've used the code here , but it won't work.我在这里使用了代码,但它不起作用。 Any help would be really appreciated.任何帮助将非常感激。

I suspect your process is exiting before getting a chance to write to the logs.我怀疑您的进程在有机会写入日志之前已经退出。 I'd suggest waiting until all messages are logged before exiting, see Awaiting logs to be written in winston我建议等到所有消息都记录下来后再退出,请参阅等待日志写入winston

process.on('uncaughtException', ex => {
  logger.error(ex.message)
  logger.on('finish', () => {
      process.exit(1);
  });
})

process.on('unhandledRejection', reason => {
  logger.error("unhandledRejection: " + reason)
  logger.on('finish', () => {
      process.exit(1);
  });
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM