单独的日志级别

时间:2019-01-27 00:30:48

标签: log4js-node

我正在寻找一种方法,该方法仅将“ INFO”级别(对于用户)发布到控制台,而将“ DEBUG”级别发布到文件。目前,我只找到一种可行的解决方案-使用很少的“ getLogger()”函数。例如:

  • log4js.getLogger('debug');
  • log4js.getLogger('info');

其中,函数的每个参数都将配置类别与configure()函数分开。 但我不喜欢它,并假设使用一个 getLogger()函数有更好的选择。 我上次尝试的尝试是:

    log4js.configure({
    appenders: {
        everything:{ type: 'stdout' },
        file_log: { type: 'file' ,filename: 'all-the-logs.log' },
        logLevelFilter: {  type:'logLevelFilter',level: 'debug', appender: 'file_log' }
    },
    categories: {
       default: {
           appenders: [ 'logLevelFilter','everything'], level: 'info'},
    }
});

我在控制台中只能看到“ INFO”级别,但是在文件中我也只能看到“ INFO”级别。如果我添加到appender-级别“ ALL”-我将在控制台中看到所有级别,并且在文件中也将看到

1 个答案:

答案 0 :(得分:0)

log4js提供了特殊的附加类型categoryFilterlogLevelFilter,请参见Log4js - Appenders,步骤如下

  1. 根据每个日志级别添加附加器和类别;
  2. 添加类型为logLevelFilter的附加附加程序,指向相关附加程序;
  3. 在步骤2中使用附加程序更改config.categories.default.appenders
  4. 最后使用log4js.getLogger()创建记录器并在任何地方记录日志,日志文件按爱级别分开

用打字稿写成

import {Appender, Configuration, LogLevelFilterAppender} from "log4js";

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

let levels = ['trace', 'debug', 'info', 'warn', 'error', 'fatal'];
let conf: Configuration = {
    appenders : {
        default: {
            type  : "console",
            layout: {
                type   : "colored",
                pattern: "%m"
            },
        }
    },
    categories: {
        default: {
            appenders: levels,
            level    : 'all'
        }
    },
};

let logs = path.join(__dirname, 'logs');
if (!fs.existsSync(logs)) {
    fs.mkdirSync(logs);
}

for (let level of levels) {
    let appender: Appender = {
        type      : "file",
        filename  : path.join(logs, level + '.log'),
        maxLogSize: 65536,
        backups   : 10,
        layout    : {
            type   : "pattern",
            pattern: "%d{dd/MM hh:mm} %-5p %m"
        }
    };
    conf.appenders[level] = appender;
    conf.categories[level] = {
        appenders: [level],
        level    : level.toUpperCase(),
    }
}

let prefix = 'only-';
for (let level of levels) {
    let appender: LogLevelFilterAppender = {
        type    : "logLevelFilter",
        appender: level,
        level   : level,
        maxLevel: level,
    };
    let name = prefix + level;
    conf.appenders[name] = appender;
    conf.categories[name] = {
        appenders: [level],
        level    : level.toUpperCase(),
    }
}
conf.categories.default.appenders = levels.map(x => prefix + x);
//console.log(JSON.stringify(conf, null, 2));
export default conf;

样本用量

// const log4js = require('log4js');
import * as log4js from 'log4js';
import log4jsConfig from './log4js.config';

log4js.configure(log4jsConfig );

(function () {
    const logger = log4js.getLogger();
    logger.trace('Entering cheese testing');
    logger.debug('Got cheese.');
    logger.info('Cheese is Gouda.');
    logger.warn('Cheese is quite smelly.');
    logger.error('Cheese is too ripe!');
    logger.fatal('Cheese was breeding ground for listeria.');

    log4js.shutdown(console.log);
})();