簡體   English   中英

為什么 Winston 不將錯誤記錄到文件中?

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

我是 Winston 的新手,當代碼出現錯誤時,它會記錄到控制台而不是日志文件。 信息正確記錄到另一個文件,但錯誤不會。

這是我的 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

我在這里使用了代碼,但它不起作用。 任何幫助將非常感激。

我懷疑您的進程在有機會寫入日志之前已經退出。 我建議等到所有消息都記錄下來后再退出,請參閱等待日志寫入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