|
| 1 | +var vows = require('vows'), |
| 2 | + assert = require('assert'), |
| 3 | + path = require('path'), |
| 4 | + fs = require('fs'), |
| 5 | + log4js = require('../lib/log4js'); |
| 6 | + |
| 7 | +function removeFile(filename) { |
| 8 | + return function() { |
| 9 | + fs.unlink(path.join(__dirname, filename), function(err) { |
| 10 | + if (err) { |
| 11 | + console.log("Could not delete ", filename, err); |
| 12 | + } |
| 13 | + }); |
| 14 | + }; |
| 15 | +} |
| 16 | + |
| 17 | +vows.describe('../lib/appenders/dateFile').addBatch({ |
| 18 | + 'appender': { |
| 19 | + 'adding multiple dateFileAppenders': { |
| 20 | + topic: function () { |
| 21 | + var listenersCount = process.listeners('exit').length, |
| 22 | + dateFileAppender = require('../lib/appenders/dateFile'), |
| 23 | + count = 5, |
| 24 | + logfile; |
| 25 | + |
| 26 | + while (count--) { |
| 27 | + logfile = path.join(__dirname, 'datefa-default-test' + count + '.log'); |
| 28 | + log4js.addAppender(dateFileAppender.appender(logfile)); |
| 29 | + } |
| 30 | + |
| 31 | + return listenersCount; |
| 32 | + }, |
| 33 | + teardown: function() { |
| 34 | + removeFile('datefa-default-test0.log')(); |
| 35 | + removeFile('datefa-default-test1.log')(); |
| 36 | + removeFile('datefa-default-test2.log')(); |
| 37 | + removeFile('datefa-default-test3.log')(); |
| 38 | + removeFile('datefa-default-test4.log')(); |
| 39 | + }, |
| 40 | + |
| 41 | + 'should only add one `exit` listener': function (initialCount) { |
| 42 | + assert.equal(process.listeners('exit').length, initialCount + 1); |
| 43 | + } |
| 44 | + }, |
| 45 | + |
| 46 | + 'with default settings': { |
| 47 | + topic: function() { |
| 48 | + var that = this, |
| 49 | + testFile = path.join(__dirname, 'date-appender-default.log'), |
| 50 | + appender = require('../lib/appenders/dateFile').appender(testFile), |
| 51 | + logger = log4js.getLogger('default-settings'); |
| 52 | + log4js.clearAppenders(); |
| 53 | + log4js.addAppender(appender, 'default-settings'); |
| 54 | + |
| 55 | + logger.info("This should be in the file."); |
| 56 | + |
| 57 | + setTimeout(function() { |
| 58 | + fs.readFile(testFile, "utf8", that.callback); |
| 59 | + }, 100); |
| 60 | + |
| 61 | + }, |
| 62 | + teardown: removeFile('date-appender-default.log'), |
| 63 | + |
| 64 | + 'should write to the file': function(contents) { |
| 65 | + assert.include(contents, 'This should be in the file'); |
| 66 | + }, |
| 67 | + |
| 68 | + 'should use the basic layout': function(contents) { |
| 69 | + assert.match(contents, /\[\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}\.\d{3}\] \[INFO\] default-settings - /); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + } |
| 74 | +}).addBatch({ |
| 75 | + 'configure': { |
| 76 | + 'with dateFileAppender': { |
| 77 | + topic: function() { |
| 78 | + var log4js = require('../lib/log4js') |
| 79 | + , logger; |
| 80 | + //this config file defines one file appender (to ./date-file-test.log) |
| 81 | + //and sets the log level for "tests" to WARN |
| 82 | + log4js.configure('test/with-dateFile.json'); |
| 83 | + logger = log4js.getLogger('tests'); |
| 84 | + logger.info('this should not be written to the file'); |
| 85 | + logger.warn('this should be written to the file'); |
| 86 | + |
| 87 | + fs.readFile(path.join(__dirname, 'date-file-test.log'), 'utf8', this.callback); |
| 88 | + }, |
| 89 | + teardown: removeFile('date-file-test.log'), |
| 90 | + |
| 91 | + 'should load appender configuration from a json file': function(err, contents) { |
| 92 | + assert.include(contents, 'this should be written to the file\n'); |
| 93 | + assert.eq
C621
ual(contents.indexOf('this should not be written to the file'), -1); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + } |
| 98 | +}).exportTo(module); |
0 commit comments