NodeJS ExpressJS - how to log errors for the production server?

岁酱吖の 提交于 2019-12-04 07:23:42

winston is a popular library for logging.

You can transport all logs to one file or keep different files for error, debug and info logs.

Example:

  var winston = require('winston');

  var logger = new winston.Logger({
    level: 'error',
    transports: [
      new (winston.transports.File)({ filename: 'error.log' })
    ]
  });

In the code:

logger.log('error', 'test error message %s', 'my string');

You can also use winston daily rotate file with winston to rotate your log files based on size or date.

You can either add a logging library like morgan or if you have your node app running under a process manager, like pm2, then pm2 will keep track of the logging for you. I haven't used pm2 a ton, but from what I understand you could keep you console.log statements the same and pm2 will save all the output from your app into log files for you. You can read more about pm2's logging capabilities here.

you may feel irrelevant as PM2 is process manager but i found PM2 also best to deal error loggs.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!