1

I have a node.js project on cygwin, which I test with mocha.

My logger is log4js.

I have an appender to get log4js to write to file, but I don't know how to get it to write it exactly as I want.

Must-haves:

  • A different file for each time mocha runs (it's OK if mocha clobbers the old log files)
  • Unix line endings

Nice-to-have:

  • No color encodings in the file (I can get around this when mocha is run with -C, but ideally there would be something in log4js-config.js.)

How do I do this?


My current setup is as follows.

My 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;

My test file is:

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");
    })
  })
})

(For the purposes of this question, there is no code under test. I run mocha with ./node_modules/mocha/bin/mocha.)

The log file that is output is this:

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

Note three mocha runs here; the third time, I left out the -C to show the color encoding.
I can't show the line endings directly in the question, but there's this:

$ file jxg_log.log
jxg_log.log: ASCII text, with CRLF line terminators, with escape sequences

So, again: how can I overwrite the log file on subsequent runs, output unix newlines, and suppress the color codes?

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.