频道栏目
首页 > 程序开发 > web前端 > HTML/CSS > 正文
nodelog4js配置代码展示
2019-01-30 15:31:59           
收藏   我要投稿

1.新建log4js.js

const log4js = require('log4js');
log4js.configure({
  "replaceConsole": true,
  "appenders": {
    "stdout": {//控制台输出
      "type": "console"
    },
    "req": {
      "type": "dateFile",
      "filename": "logs/reqlog/",
      "pattern": "req-yyyy-MM-dd.log",
      "alwaysIncludePattern": true,
      "maxLogSize": 104800,
      "backups": 100
    },
    "err": {//错误日志
      "type": "dateFile",
      "filename": "logs/errlog/",
      "pattern": "err-yyyy-MM-dd.log",
      "alwaysIncludePattern": true,
      "maxLogSize": 104800,
      "backups": 100
    },
    "oth": {//其他日志
      "type": "dateFile",
      "filename": "logs/othlog/",
      "pattern": "oth-yyyy-MM-dd.log",
      "alwaysIncludePattern": true,
      "maxLogSize": 104800,
      "backups": 100
    }
  },
  "categories": {//level:设置级别
    "default": {
      "appenders": [
        "stdout",
        "req"
      ],
      "level": "debug"
    },
    "err": {
      "appenders": [
        "stdout",
        "err"
      ],
      "level": "error"
    },
    "oth": {
      "appenders": [
        "stdout",
        "oth"
      ],
      "level": "info"
    }
  }
});
exports.getLogger = function (name) {//name取categories项
    return log4js.getLogger(name || 'default')
};

exports.useLogger = function (app, logger) {//用来与express结合
    app.use(log4js.connectLogger(logger || log4js.getLogger('default'), {
        format: '[:remote-addr :method :url :status :response-timems][:referrer HTTP/:http-version :user-agent]'//自定义输出格式
    }))
};

1.引入log4js文件

const log4js= require('./logs.js')
const logger = log4js.getLogger()//根据需要获取logger
const errlogger = log4js.getLogger('err')
const othlogger = log4js.getLogger('oth')
log4js.useLogger(app,logger)
log4js.useLogger(app,errlogger)
log4js.useLogger(app,othlogger)
点击复制链接 与好友分享!回本站首页
上一篇:UI设计分享
下一篇:vue引入自己写的js教学
相关文章
图文推荐
点击排行

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站