我在cygwin上有一个node.js项目,我用mocha测试。
我的记录器是log4js。
我有一个appender来让log4js写入文件,但我不知道如何让它完全按照我的意愿编写它。
必备:
尼斯到有:
-C
运行mocha时解决此问题,但理想情况下log4js-config.js
会有一些内容。)我该怎么做?
我目前的设置如下。
我的log4js-config.js
:
var log4js = require('log4js');
var log4js_config = {
"appenders": [{
"type": "console",
"layout": {
"type": "pattern",
"pattern": "%[%d{yyyy-MM-ddThh:mm:ss.SSS} [pid=%x{pid}] %p %c -%] %m",
"tokens": {pid: function(){return process.pid;}}
}
},{
"type": "file",
"filename": "jxg_log.log",
"layout": {
"type": "pattern",
"pattern": "%d{yyyy-MM-ddThh:mm:ss.SSS} [pid=%x{pid}] %p %c - %m",
"tokens": {pid: function(){return process.pid;}}
}
}],
replaceConsole: true
};
log4js.configure(log4js_config, {});
exports.logging = log4js;
我的测试文件是:
var assert = require("assert");
var logger = require('../src/log4js-config').logging.getLogger('Mocha-Test');
describe('Array', function(){
describe('#indexOf()', function(){
it('should return -1 when the value is not present', function(){
logger.debug("logger debug line 1");
assert.equal(-1, [1,2,3].indexOf(5));
assert.equal(-1, [1,2,3].indexOf(0));
logger.debug("logger debug last line");
})
})
})
(出于这个问题的目的,没有正在测试的代码。我使用./node_modules/mocha/bin/mocha
运行mocha。)
输出的日志文件是:
2014-11-24T13:45:00.012 [pid=90652] INFO console - %m
2014-11-24T13:45:00.018 [pid=90652] INFO console - %m
2014-11-24T13:45:00.019 [pid=90652] INFO console - Array
2014-11-24T13:45:00.020 [pid=90652] INFO console - #indexOf()
2014-11-24T13:45:00.021 [pid=90652] DEBUG Mocha-Test - logger debug line 1
2014-11-24T13:45:00.021 [pid=90652] DEBUG Mocha-Test - logger debug last line
2014-11-24T13:45:19.090 [pid=90180] INFO console - %m
2014-11-24T13:45:19.093 [pid=90180] INFO console - %m
2014-11-24T13:45:19.095 [pid=90180] INFO console - Array
2014-11-24T13:45:19.095 [pid=90180] INFO console - #indexOf()
2014-11-24T13:45:19.096 [pid=90180] DEBUG Mocha-Test - logger debug line 1
2014-11-24T13:45:19.096 [pid=90180] DEBUG Mocha-Test - logger debug last line
2014-11-24T13:50:13.716 [pid=88476] INFO console - %m
2014-11-24T13:50:13.720 [pid=88476] INFO console - [0m[0m
2014-11-24T13:50:13.721 [pid=88476] INFO console - [0m Array[0m
2014-11-24T13:50:13.722 [pid=88476] INFO console - [0m #indexOf()[0m
2014-11-24T13:50:13.722 [pid=88476] DEBUG Mocha-Test - logger debug line 1
2014-11-24T13:50:13.723 [pid=88476] DEBUG Mocha-Test - logger debug last line
注意三个摩卡在这里运行;第三次,我遗漏了-C
来显示颜色编码
我不能直接在问题中显示行结尾,但是就是这样:
$ file jxg_log.log
jxg_log.log: ASCII text, with CRLF line terminators, with escape sequences
所以,再说一遍:如何在后续运行中覆盖日志文件,输出unix换行符,并禁止显示颜色代码?