简体   繁体   English

如何使用winston logger记录节点错误?

[英]How to log node errors with winston logger?

In my node application I use the Winstonjs logger. 在我的节点应用程序中,我使用Winstonjs记录器。 Today my application somehow seemed to freeze, but it failed to log something. 今天我的应用程序似乎冻结了,但它没有记录某些东西。 I stopped the application and ran it manually which showed me this error 我停止了应用程序并手动运行它,这显示了我的错误

ReferenceError: totalValue is not defined

I clearly made a mistake in my code, but my main problem here is that I couldn't know from the Winston logs. 我在代码中明显犯了一个错误,但我的主要问题是我从Winston日志中无法知道。

I pasted my Winston implementation below. 我在下面粘贴了我的Winston实现。 I created this so that I can simply use log('The log message'); 我创建了这个,以便我可以简单地使用log('The log message'); . But this doesn't log any occurring node errors. 但是这不记录任何发生的节点错误。

Does anybody know how I can get every occurring node error into my Winston logs? 有谁知道如何将每个发生的节点错误都记录到我的Winston日志中?

const myFormat = winston.format.printf(info => {
    return `${info.timestamp} ${info.level}: ${info.message}`;
});
const logger = winston.createLogger({
    level: 'info',
    format: winston.format.combine(winston.format.timestamp(), myFormat),  // winston.format.json(),
    transports: [
        new winston.transports.File({filename: 'logs/error.log', level: 'error'}),
        new winston.transports.File({filename: 'logs/combined.log'}),
    ]
});
function log(message, level='info'){
    if (typeof message === 'object'){
        message = JSON.stringify(message);
    }
    logger[level](message);
}

Winston can log exceptions for you. Winston可以为您记录例外情况。 From the docs: Exceptions 来自文档: 例外

With winston, it is possible to catch and log uncaughtException events from your process. 使用winston,可以捕获并记录进程中的uncaughtException事件。 With your own logger instance you can enable this behavior when it's created or later on in your applications lifecycle: 使用您自己的记录器实例,您可以在应用程序生命周期中创建或稍后启用此行为:

const { createLogger, transports } = require('winston');

// Enable exception handling when you create your logger.
const logger = createLogger({
  transports: [
    new transports.File({ filename: 'combined.log' }) 
  ],
  exceptionHandlers: [
    new transports.File({ filename: 'exceptions.log' })
  ]
});

// Or enable it later on by adding a transport or using `.exceptions.handle`
const logger = createLogger({
  transports: [
    new transports.File({ filename: 'combined.log' }) 
  ]
});

// Call exceptions.handle with a transport to handle exceptions
logger.exceptions.handle(
  new transports.File({ filename: 'exceptions.log' })

You could simply use it like this: 您可以像这样简单地使用它:

const winston = require('winston');
require('winston-daily-rotate-file');

function getLogger(module) {

var transport = new (winston.transports.DailyRotateFile)({
  filename: './logs/log',
  datePattern: 'yyyy-MM-dd.',
  prepend: true,
  level: process.env.ENV === 'development' ? 'debug' : 'error'
});

const logger = new (winston.Logger)({
  transports: [
    transport
  ]
});
return logger;
}

module.exports = getLogger;

Have it in a seperate file, require it wherever you want to use it and then you can use it like so: 将它放在一个单独的文件中,无论你想在哪里使用它都需要它然后你可以这样使用它:
log.debug('This is debug statement!')
log.error('Logging exception here')

You can always change the log level in the transport object and accordingly your statements will get logged. 您始终可以更改transport对象中的日志级别,因此将记录您的语句。 Also, this code will rotate your file every day and create a new one based on the date. 此外,此代码将每天轮换您的文件并根据日期创建一个新文件。 This helps not to have one single large file 这有助于不拥有一个大文件

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

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