简体   繁体   English

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

[英]log4js - how to specify log file name with an environment variable?

I am using log4js-node to log in my node js application. 我正在使用log4js-node登录到我的node js应用程序。 I want to specify the path to the log file name inside my log4js config json using an environment variable. 我想使用环境变量在我的log4js config json中指定日志文件名的路径。 Here is the config json I am using: 这是我正在使用的配置json:

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

When the application starts, it can't find the specified path because the environment variable is not correctly read. 应用程序启动时,由于找不到正确的环境变量,因此找不到指定的路径。

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

How do I use the environment variables correctly inside the log4js configuration json? 如何在log4js配置json中正确使用环境变量?

Example: 例:

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

Output: 输出:

[2010-01-17 11:43:37.987] [ERROR] cheese - Cheese is too ripe! [2010-01-17 11:43:37.987] [错误]奶酪-奶酪太熟了! [2010-01-17 11:43:37.990] [FATAL] cheese - Cheese was breeding ground for listeria. [2010-01-17 11:43:37.990] [致命]奶酪-奶酪是李斯特菌的温床。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM