log4js - 如何用环境变量指定日志文件名?

时间:2015-04-01 06:51:58

标签: node.js logging environment-variables

我正在使用log4js-node登录我的节点js应用程序。我想使用环境变量在log4js config json中指定日志文件名的路径。这是我正在使用的配置json:

{
    "appenders": [{
        "type": "file",
        "filename": "${env.API_HOME}/logs/apiserver_log_file.log",
        "maxLogSize": 20480,
        "backups": 3,
        "category": "apiserver"
    }]
}

当应用程序启动时,它无法找到指定的路径,因为未正确读取环境变量。

Error: ENOENT, open '${env.API_HOME}/logs/apiserver_log_file.log'

如何在log4js配置json中正确使用环境变量?

1 个答案:

答案 0 :(得分:0)

示例:

var log4js = require('log4js'); 
//console log is loaded by default, so you won't normally need to do this
//log4js.loadAppender('console');
log4js.loadAppender('file');
//log4js.addAppender(log4js.appenders.console());
log4js.addAppender(log4js.appenders.file('logs/cheese.log'), 'cheese');

var logger = log4js.getLogger('cheese');
logger.setLevel('ERROR');

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.');

输出:

[2010-01-17 11:43:37.987] [错误]奶酪 - 奶酪太熟了! [2010-01-17 11:43:37.990] [致命]奶酪 - 奶酪是李斯特菌的滋生地。