diff --git a/.bob.json b/.bob.json index c8c1e02..2ae59b4 100644 --- a/.bob.json +++ b/.bob.json @@ -1,12 +1,12 @@ { - "build": "clean lint coverage test", + "build": "clean lint test coverage", "lint": { "type": "jshint" }, "coverage": { - "type": "vows" + "type": "mocha-istanbul" }, "test": { - "type": "vows" + "type": "mocha" } } diff --git a/.jshintrc b/.jshintrc index fe10cba..bee0109 100644 --- a/.jshintrc +++ b/.jshintrc @@ -10,6 +10,9 @@ "maxlen": 100, "globals": { "describe": true, - "it": true + "it": true, + "before": true, + "beforeEach": true, + "after": true } } diff --git a/lib/appenders/dateFile.js b/lib/appenders/dateFile.js index 6317c68..ba4c259 100644 --- a/lib/appenders/dateFile.js +++ b/lib/appenders/dateFile.js @@ -47,10 +47,6 @@ function configure(config, options) { config.alwaysIncludePattern = false; } - if (options && options.cwd && !config.absolute) { - config.filename = path.join(options.cwd, config.filename); - } - return appender(config.filename, config.pattern, config.alwaysIncludePattern, layout); } diff --git a/lib/layouts.js b/lib/layouts.js index 9043448..acda6a3 100644 --- a/lib/layouts.js +++ b/lib/layouts.js @@ -160,20 +160,17 @@ function patternLayout (pattern, tokens) { return loggerName; } + var formats = { + "ISO8601": dateFormat.ISO8601_FORMAT, + "ISO8601_WITH_TZ_OFFSET": dateFormat.ISO8601_WITH_TZ_OFFSET_FORMAT, + "ABSOLUTE": dateFormat.ABSOLUTETIME_FORMAT, + "DATE": dateFormat.DATETIME_FORMAT + }; + function formatAsDate(loggingEvent, specifier) { var format = dateFormat.ISO8601_FORMAT; if (specifier) { - format = specifier; - // Pick up special cases - if (format == "ISO8601") { - format = dateFormat.ISO8601_FORMAT; - } else if (format == "ISO8601_WITH_TZ_OFFSET") { - format = dateFormat.ISO8601_WITH_TZ_OFFSET_FORMAT; - } else if (format == "ABSOLUTE") { - format = dateFormat.ABSOLUTETIME_FORMAT; - } else if (format == "DATE") { - format = dateFormat.DATETIME_FORMAT; - } + format = formats[specifier] || specifier; } // Format the date return dateFormat.asString(format, loggingEvent.startTime); diff --git a/lib/log4js.js b/lib/log4js.js index 3a112f8..48b4ce0 100644 --- a/lib/log4js.js +++ b/lib/log4js.js @@ -125,7 +125,14 @@ function workerDispatch(event) { function dispatch(event) { debug("event is ", event); var category = categories[event.category] || categories.default; - debug("category.level[", category.level, "] <= ", event.level, " ? ", category.level.isLessThanOrEqualTo(event.level)); + debug( + "category.level[", + category.level, + "] <= ", + event.level, + " ? ", + category.level.isLessThanOrEqualTo(event.level) + ); if (category.level.isLessThanOrEqualTo(event.level)) { category.appenders.forEach(function(appender) { @@ -182,7 +189,11 @@ function validateCategories(cats) { } category.level = levels.toLevel(inputLevel); if (!category.level) { - throw new Error("Level '" + inputLevel + "' is not valid for category '" + categoryName + "'. Acceptable values are: " + levels.levels.join(', ') + "."); + throw new Error( + "Level '" + inputLevel + + "' is not valid for category '" + categoryName + + "'. Acceptable values are: " + levels.levels.join(', ') + "." + ); } if (!category.appenders || !category.appenders.length) { @@ -191,7 +202,11 @@ function validateCategories(cats) { category.appenders.forEach(function(appender) { if (!appenders[appender]) { - throw new Error("Appender '" + appender + "' for category '" + categoryName + "' does not exist. Known appenders are: " + Object.keys(appenders).join(', ') + "."); + throw new Error( + "Appender '" + appender + + "' for category '" + categoryName + + "' does not exist. Known appenders are: " + Object.keys(appenders).join(', ') + "." + ); } }); }); @@ -213,14 +228,20 @@ function appenderByName(name) { function configureAppenders(appenderMap) { clearAppenders(); Object.keys(appenderMap).forEach(function(appenderName) { - var appender, appenderConfig = appenderMap[appenderName]; - loadAppender(appenderConfig.type); - appenderConfig.makers = appenderMakers; - try { - appenders[appenderName] = appenderMakers[appenderConfig.type](appenderConfig, appenderByName); - } catch(e) { - throw new Error("log4js configuration problem for appender '" + appenderName + "'. Error was " + e.stack); - } + var appender, appenderConfig = appenderMap[appenderName]; + loadAppender(appenderConfig.type); + appenderConfig.makers = appenderMakers; + try { + appenders[appenderName] = appenderMakers[appenderConfig.type]( + appenderConfig, + appenderByName + ); + } catch(e) { + throw new Error( + "log4js configuration problem for appender '" + appenderName + + "'. Error was " + e.stack + ); + } }); } diff --git a/test/categoryFilter-test.js b/test/categoryFilter-test.js index 99a477c..dcdc111 100644 --- a/test/categoryFilter-test.js +++ b/test/categoryFilter-test.js @@ -85,7 +85,10 @@ describe('log4js', function() { 'utf8', function(err, contents) { var lines = contents.trim().split('\n'); - lines.should.eql(["This should not", "This shouldn't be included by the appender anyway"]); + lines.should.eql([ + "This should not", + "This shouldn't be included by the appender anyway" + ]); done(err); } ); diff --git a/test/clusteredAppender-test.js b/test/clusteredAppender-test.js index 34ba46f..511a07d 100755 --- a/test/clusteredAppender-test.js +++ b/test/clusteredAppender-test.js @@ -43,7 +43,7 @@ describe('log4js in a cluster', function() { }); it('should listen for fork events', function() { - clusterOnFork.should.be.true; + clusterOnFork.should.eql(true); }); it('should listen for messages from workers', function() { diff --git a/test/consoleAppender-test.js b/test/consoleAppender-test.js index c36e509..c537326 100644 --- a/test/consoleAppender-test.js +++ b/test/consoleAppender-test.js @@ -25,7 +25,7 @@ describe('../lib/appenders/console', function() { }); it('should output to console', function() { - messages.should.eql(["blah"]); + messages.should.eql(["blah"]); }); }); diff --git a/test/dateFileAppender-test.js b/test/dateFileAppender-test.js index 6ebf32b..5082a59 100644 --- a/test/dateFileAppender-test.js +++ b/test/dateFileAppender-test.js @@ -1,4 +1,5 @@ "use strict"; +/*jshint expr:true */ var should = require('should') , async = require('async') , path = require('path') diff --git a/test/date_format-test.js b/test/date_format-test.js index de2c546..06e46ec 100644 --- a/test/date_format-test.js +++ b/test/date_format-test.js @@ -15,10 +15,20 @@ describe('date_format', function() { it('should provide a ISO8601 with timezone offset format', function() { date.getTimezoneOffset = function() { return -660; }; - dateFormat.asString(dateFormat.ISO8601_WITH_TZ_OFFSET_FORMAT, date).should.eql("2010-01-11T14:31:30+1100"); + dateFormat.asString( + dateFormat.ISO8601_WITH_TZ_OFFSET_FORMAT, + date + ).should.eql( + "2010-01-11T14:31:30+1100" + ); date.getTimezoneOffset = function() { return 120; }; - dateFormat.asString(dateFormat.ISO8601_WITH_TZ_OFFSET_FORMAT, date).should.eql("2010-01-11T14:31:30-0200"); + dateFormat.asString( + dateFormat.ISO8601_WITH_TZ_OFFSET_FORMAT, + date + ).should.eql( + "2010-01-11T14:31:30-0200" + ); }); it('should provide a just-the-time format', function() { diff --git a/test/fileAppender-test.js b/test/fileAppender-test.js index 5e889ac..aee123a 100644 --- a/test/fileAppender-test.js +++ b/test/fileAppender-test.js @@ -1,4 +1,5 @@ "use strict"; +/*jshint expr:true */ var fs = require('fs') , async = require('async') , path = require('path') @@ -18,9 +19,12 @@ describe('log4js fileAppender', function() { before(function() { var logfile , count = 5 - , config = { appenders: {}, categories: { default: { level: "debug", appenders: ["file0"] } } }; + , config = { + appenders: {}, + categories: { default: { level: "debug", appenders: ["file0"] } } + }; - initialCount = process.listeners('exit').length + initialCount = process.listeners('exit').length; while (count--) { logfile = path.join(__dirname, '/fa-default-test' + count + '.log'); @@ -98,7 +102,7 @@ describe('log4js fileAppender', function() { "file": { type: "file", filename: testFile } }, categories: { - default: { level: "debug", appenders: [ "file" ] } + "default": { level: "debug", appenders: [ "file" ] } } }); @@ -147,7 +151,7 @@ describe('log4js fileAppender', function() { "file": { type: "file", filename: testFile, maxLogSize: 100, backups: 0 } }, categories: { - default: { level: "debug", appenders: [ "file" ] } + "default": { level: "debug", appenders: [ "file" ] } } }); logger.info("This is the first log message."); @@ -206,7 +210,7 @@ describe('log4js fileAppender', function() { "file": { type: "file", filename: testFile, maxLogSize: 50, backups: 2 } }, categories: { - default: { level: "debug", appenders: [ "file" ] } + "default": { level: "debug", appenders: [ "file" ] } } }); @@ -315,7 +319,8 @@ describe('log4js fileAppender', function() { } ); fileAppender.configure({ - filename: 'test1.log', maxLogSize: 100 + filename: 'test1.log', + maxLogSize: 100 }); errorHandler({ error: 'aargh' }); }); diff --git a/test/layouts-test.js b/test/layouts-test.js index 26bce67..bcf6b2d 100644 --- a/test/layouts-test.js +++ b/test/layouts-test.js @@ -76,22 +76,19 @@ describe('log4js layouts', function() { }); it('should print the stacks of a passed error objects', function() { - assert.ok( - Array.isArray( - layout({ - data: [ new Error() ], - startTime: new Date(2010, 11, 5, 14, 18, 30, 45), - category: "cheese", - level: { - colour: "green", - toString: function() { return "ERROR"; } - } - }).match( - /Error\s+at Context\..*\s+\((.*)test[\\\/]layouts-test\.js\:\d+\:\d+\)\s/ - ) - ), - 'regexp did not return a match' - ); + assert.ok(Array.isArray( + layout({ + data: [ new Error() ], + startTime: new Date(2010, 11, 5, 14, 18, 30, 45), + category: "cheese", + level: { + colour: "green", + toString: function() { return "ERROR"; } + } + }).match( + /Error\s+at Context\..*\s+\((.*)test[\\\/]layouts-test\.js\:\d+\:\d+\)\s/ + ) + ), 'regexp did not return a match'); }); describe('with passed augmented errors', function() { @@ -203,7 +200,13 @@ describe('log4js layouts', function() { event.startTime.getTimezoneOffset = function() { return 0; }; it('should default to "time logLevel loggerName - message"', function() { - test(layout, event, tokens, null, "14:18:30 DEBUG multiple.levels.of.tests - this is a test\n"); + test( + layout, + event, + tokens, + null, + "14:18:30 DEBUG multiple.levels.of.tests - this is a test\n" + ); }); it('%r should output time only', function() { @@ -295,24 +298,26 @@ describe('log4js layouts', function() { test(layout, event, tokens, '%[%r%]', '\x1B[36m14:18:30\x1B[39m'); }); - it('%x{testString} should output the string stored in tokens', function() { - test(layout, event, tokens, '%x{testString}', 'testStringToken'); - }); - - it('%x{testFunction} should output the result of the function stored in tokens', function() { - test(layout, event, tokens, '%x{testFunction}', 'testFunctionToken'); - }); - - it('%x{doesNotExist} should output the string stored in tokens', function() { - test(layout, event, tokens, '%x{doesNotExist}', '%x{doesNotExist}'); - }); - - it('%x{fnThatUsesLogEvent} should be able to use the logEvent', function() { - test(layout, event, tokens, '%x{fnThatUsesLogEvent}', 'DEBUG'); - }); - - it('%x should output the string stored in tokens', function() { - test(layout, event, tokens, '%x', '%x'); + describe('%x{}', function() { + it('%x{testString} should output the string stored in tokens', function() { + test(layout, event, tokens, '%x{testString}', 'testStringToken'); + }); + + it('%x{testFunction} should output the result of the function stored in tokens', function() { + test(layout, event, tokens, '%x{testFunction}', 'testFunctionToken'); + }); + + it('%x{doesNotExist} should output the string stored in tokens', function() { + test(layout, event, tokens, '%x{doesNotExist}', '%x{doesNotExist}'); + }); + + it('%x{fnThatUsesLogEvent} should be able to use the logEvent', function() { + test(layout, event, tokens, '%x{fnThatUsesLogEvent}', 'DEBUG'); + }); + + it('%x should output the string stored in tokens', function() { + test(layout, event, tokens, '%x', '%x'); + }); }); }); diff --git a/test/levels-test.js b/test/levels-test.js index f3fc645..e901f26 100644 --- a/test/levels-test.js +++ b/test/levels-test.js @@ -417,6 +417,11 @@ describe('../lib/levels', function() { it('should return the default value if argument is not recognised', function() { levels.toLevel("cheese", levels.DEBUG).should.eql(levels.DEBUG); }); + + it('should return the default value if argument is falsy', function() { + levels.toLevel(undefined, levels.DEBUG).should.eql(levels.DEBUG); + levels.toLevel(null, levels.DEBUG).should.eql(levels.DEBUG); + }); }); }); diff --git a/test/log4js-test.js b/test/log4js-test.js index fe1658f..d4571f6 100644 --- a/test/log4js-test.js +++ b/test/log4js-test.js @@ -54,40 +54,44 @@ describe('../lib/log4js', function() { }); - it('should complain if the config does not specify an appender for the default category', function() { - - (function() { - log4js.configure( - { + it( + 'should complain if the config does not specify an appender for the default category', + function() { + + (function() { + log4js.configure( + { + appenders: { + "console": { type: "console" } + }, + categories: {} + } + ); + }).should.throw( + "You must specify an appender for the default category" + ); + + (function() { + log4js.configure({ appenders: { "console": { type: "console" } - }, - categories: {} - } - ); - }).should.throw( - "You must specify an appender for the default category" - ); - - (function() { - log4js.configure({ - appenders: { - "console": { type: "console" } - }, - categories: { - "cheese": { level: "DEBUG", appenders: [ "console" ] } - } - }); - }).should.throw( - "You must specify an appender for the default category" - ); - - }); + }, + categories: { + "cheese": { level: "DEBUG", appenders: [ "console" ] } + } + }); + }).should.throw( + "You must specify an appender for the default category" + ); + + } + ); it('should complain if a category does not specify level or appenders', function() { (function() { log4js.configure( - { appenders: { "console": { type: "console" } }, + { + appenders: { "console": { type: "console" } }, categories: { "default": { thing: "thing" } } @@ -99,7 +103,8 @@ describe('../lib/log4js', function() { (function() { log4js.configure( - { appenders: { "console": { type: "console" } }, + { + appenders: { "console": { type: "console" } }, categories: { "default": { level: "DEBUG" } } @@ -113,7 +118,8 @@ describe('../lib/log4js', function() { it('should complain if a category specifies a level that does not exist', function() { (function() { log4js.configure( - { appenders: { "console": { type: "console" }}, + { + appenders: { "console": { type: "console" }}, categories: { "default": { level: "PICKLES" } } @@ -128,7 +134,8 @@ describe('../lib/log4js', function() { it('should complain if a category specifies an appender that does not exist', function() { (function() { log4js.configure( - { appenders: { "console": { type: "console" }}, + { + appenders: { "console": { type: "console" }}, categories: { "default": { level: "DEBUG", appenders: [ "cheese" ] } } @@ -212,8 +219,8 @@ describe('../lib/log4js', function() { it('should read config from a file', function() { var events = [], log4js_sandbox = sandbox.require( '../lib/log4js', - { requires: - { + { + requires: { 'cheese': { configure: function() { return function(event) { events.push(event); }; @@ -235,8 +242,8 @@ describe('../lib/log4js', function() { , noisyLogger , log4js_sandbox = sandbox.require( '../lib/log4js', - { requires: - { + { + requires: { 'cheese': { configure: function() { return function(event) { events.push(event); }; @@ -264,8 +271,8 @@ describe('../lib/log4js', function() { var events = [] , log4js_sandbox = sandbox.require( '../lib/log4js', - { requires: - { + { + requires: { 'cheese': { configure: function() { return function(event) { events.push(event); }; diff --git a/test/streams/BaseRollingFileStream-test.js b/test/streams/BaseRollingFileStream-test.js index daf4b11..cb4b2db 100644 --- a/test/streams/BaseRollingFileStream-test.js +++ b/test/streams/BaseRollingFileStream-test.js @@ -73,14 +73,14 @@ describe('../../lib/streams/BaseRollingFileStream', function() { }); it('should not want to roll', function() { - stream.shouldRoll().should.be.false; + stream.shouldRoll().should.eql(false); }); it('should not roll', function() { var cbCalled = false; //just calls the callback straight away, no async calls stream.roll('basetest.log', function() { cbCalled = true; }); - cbCalled.should.be.true; + cbCalled.should.eql(true); }); }); }); diff --git a/test/streams/DateRollingFileStream-test.js b/test/streams/DateRollingFileStream-test.js index 92fe269..960443e 100644 --- a/test/streams/DateRollingFileStream-test.js +++ b/test/streams/DateRollingFileStream-test.js @@ -178,14 +178,19 @@ describe('DateRollingFileStream', function() { var stream; before(function(done) { - testTime = new Date(2012, 8, 12, 0, 10, 12), - stream = new DateRollingFileStream( - __dirname + '/test-date-rolling-file-stream-pattern', - '.yyyy-MM-dd', - { alwaysIncludePattern: true }, - now + testTime = new Date(2012, 8, 12, 0, 10, 12); + remove( + __dirname + '/test-date-rolling-file-stream-pattern.2012-09-12', + function() { + stream = new DateRollingFileStream( + __dirname + '/test-date-rolling-file-stream-pattern', + '.yyyy-MM-dd', + { alwaysIncludePattern: true }, + now + ); + stream.write("First message\n", 'utf8', done); + } ); - stream.write("First message\n", 'utf8', done); }); after(function(done) { diff --git a/test/streams/rollingFileStream-test.js b/test/streams/rollingFileStream-test.js index eb3465d..18a20f2 100644 --- a/test/streams/rollingFileStream-test.js +++ b/test/streams/rollingFileStream-test.js @@ -14,7 +14,7 @@ if (semver.satisfies(process.version, '>=0.10.0')) { RollingFileStream = require('../../lib/streams').RollingFileStream; function remove(filename, cb) { - fs.unlink(filename, function() { cb(); }); + fs.unlink(filename, function(err) { cb(); }); } function create(filename, cb) {