log4js的简单配置

末鹿安然 提交于 2019-11-30 22:12:01

js记录日志工具log4js,参数请参考官网文档https://log4js-node.github.io/log4js-node/index.html

const log4js = require('log4js');
const path = require('path');

log4js.configure({
    appenders: {
        info1: {
            type: 'file',
            filename: path.resolve(__dirname, 'logs/info.log')
        },
        info2: {
            type: 'dateFile',
            filename: path.resolve(__dirname, 'logs/statistics'),
            pattern: 'yyyy-MM-dd.log',
            alwaysIncludePattern: true
        },
        errors: {
            type: 'file',
            filename: path.resolve(__dirname, 'logs/error.log')
        }
    },
    categories: {
        autoInfo: {
            appenders: ['info1'],
            level: 'info'
        },
        staInfo: {
            appenders: ['info2'],
            level: 'info'
        },
        error: {
            appenders: ['errors'],
            level: 'error'
        },
        default: {
            appenders: ['errors'],
            level: 'error'
        }
    }
});
// getLogger参数是categories里的
module.exports = {
    infoLog: log4js.getLogger('autoInfo'),
    errorLog: log4js.getLogger('error'),
    staLog: log4js.getLogger('staInfo')
};
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!