我是Node.js和Express的新手。
我想使用log4js,但不确定应该在哪个文件中配置我的记录器。
是否有用于初始化的常规文件?如果没有,我应该在哪里创建一个新的配置文件?
谢谢:)
答案(基于@jFri00应答)
在logger.js
中
'use strict';
var log4js = require('log4js');
log4js.configure({
"appenders": [...]
});
var logger = log4js.getLogger("structuredLogger");
module.exports = logger
在client.js
中
var logger = require('../../../../../config/logger.js');
logger.info('My message');
这个模块将使我能够:
发布于 2016-01-09 15:23:44
需要初始化一次的模块的一个常见选项是创建自己的容器模块来进行初始化。然后,希望使用日志记录的其他每个模块都可以加载容器模块,如果没有初始化,容器模块将初始化日志记录。
// mylog.js
// initialization code will only be called the first time the module is loaded
// after that, the module is cached by the `require()` infrastructure
var log4js = require('log4js');
log4js.configure({
appenders: [
{ type: 'console' },
{ type: 'file', filename: 'logs/cheese.log', category: 'cheese' }
]
});
module.exports = log4js;
然后,希望使用公共配置日志记录的每个模块都可以在模块顶部附近执行以下操作:
var log4js = require('./mylog.js');
https://stackoverflow.com/questions/34699555
复制相似问题