nodejs+express使用log4js总结

为方便定位问题,推荐大家使用log4js组件

目前已实现如下功能:

1、运行日志run.log,每一个最大8M,最多5个,循环覆盖。

2、错误日志error.log,每一个最大8M,最多10个,循环覆盖。

3、不再使用日志文件名,日期文件名无法解决文件过大,且时间长了需要手工清理。

配置文件log4js_config.js

module.exports = {
  appenders: {
    console: {
      "type": "console",
      "category": "console"
    },
    everything: {
      type: "file",
      filename: "log/run.log",
      //最大文件大小,按字节计算 1024 * 1024 * 8 = 8M 
      //integer (optional) - the maximum size (in bytes) for the log file. If not specified, then no log rolling will happen.
      maxLogSize: 1024 * 1024 * 8,
      //文件保留数量 
      //integer (optional, default value = 5) - the number of old log files to keep during log rolling.
      backups: 5,
      keepFileExt: true,
      compress: false
    },
    errorFile: {
      type: 'file',
      filename: "log/error.log",
      //最大文件大小,按字节计算 1024 * 1024 * 8 = 8M 
      //integer (optional) - the maximum size (in bytes) for the log file. If not specified, then no log rolling will happen.
      maxLogSize: 1024 * 1024 * 8,
      //文件保留数量 
      //integer (optional, default value = 5) - the number of old log files to keep during log rolling.
      backups: 10,
      keepFileExt: true,
      compress: false
    },
    errors: { type: 'logLevelFilter', level: 'error', appender: 'errorFile' }
  },
  "categories": {
    "default": {
      "appenders": [
        "everything",
        "errors",
        "console"
      ],
      //设置日志记录级别,记录当前级别及以后 ALL < TRACE < DEBUG < INFO < WARN < ERROR < FATAL < MARK < OFF
      //调试时可以设置为all,线上可设置为INFO
      "level": "INFO"
    }
  },
  //若您的 app 使用了 pm2,则这里必须设置为true,否则日志将不会工作(另外您还得下载 pm2-intercom作为 pm2模块: pm2 install pm2-intercom)
  "pm2": true,
  //(默认 ‘NODE_APP_INSTANCE’):如果您使用pm2并更改了默认名称,则这里必须要设置。
  // pm2InstanceVar: 'INSTANCE_ID',
  //使用的 log4js 忽略集群环境(clustered environments)或者你在 pm2 日志中遇到了麻烦。每一个工作进程都将进行自己的日志记录。请小心使用这里如果你要把日志记录输出到文件。
  // disableClustering: true,
  "replaceConsole": true
};

app.js中增加

var path = require('path');
var log4js = require('log4js');
var log4js_config = require('./config/log4js_config');
//接管console日志,自动区分类别
log4js.configure(log4js_config);
const loggerOfConsole = log4js.getLogger('console');
console.log = loggerOfConsole.info.bind(loggerOfConsole); // do the same for others - console.debug, etc.
console.debug = loggerOfConsole.info.bind(loggerOfConsole);

app.use(log4js.connectLogger(loggerOfConsole, { level: 'auto' }));
//app.use(log4js.connectLogger(log4js.getLogger("http"), { level: 'auto' }));
//log4js.replaceConsole(log4js.getLogger("db"));

记录日志的代码:

var log = log4js.getLogger(path.basename(__filename));
log.info('app start...');
// log.trace('log trace test');
// log.debug('log debug test');
// log.info('log info test');
// log.warn('log warn test');
// log.error('log error test');
// log.fatal('log fatal test');
console.log('请在程序运行目录下查看log4js日志');//这里打一个console.log是为了让程序调试时,运行时,或者在pm2里,能够得一个提示,如果不输出任何信息可能会被识以为未启动,因为log4js可能已经接管了console。

 可以微信联系我哦

发布了22 篇原创文章 · 获赞 15 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/zhupengfei/article/details/100557739