diff --git a/index.js b/index.js index 8c3c102..4978cbe 100644 --- a/index.js +++ b/index.js @@ -1,4 +1,5 @@ const _ = require('lodash'); +const util = require('util'); module.exports = () => { // initialize arrays for all levels @@ -11,7 +12,16 @@ module.exports = () => { get: (layer) => { // add error(msg), info(msg), etc to getLayer() to form logger functionality return Object.keys(levels).reduce((acc, level) => { - return _.set(acc, level, msg => levels[level].push(msg)); + return _.set(acc, level, function() { + // add a function to $level that 'apply's to util.format + levels[level].push( + util.format.apply( + null, + // convert arguments to an array + Array.prototype.slice.call(arguments) + ) + ); + }); }, { getLayer: () => layer } ); }, // return the supported logging levels diff --git a/test/index.js b/test/index.js index 003fd6e..0c912cf 100644 --- a/test/index.js +++ b/test/index.js @@ -30,6 +30,45 @@ test('all levels should initially be empty', (t) => { }); +test('varargs should be supported for logging events (util.format gets called under the hood)', t => { + const rootLogger = require('../index')(); + const logger = rootLogger.get('layer value'); + + rootLogger.getLevels().forEach(level => { + logger[level](`${level} message %d %s`, 1, 'blah', { a: 3 }, [4, 5]); + + const actual = rootLogger.getMessages(level); + + const expected = [ + `${level} message 1 blah { a: 3 } [ 4, 5 ]`, + ]; + + t.deepEquals(actual, expected, `all ${level} messages should have been returned`); + + }); + + t.end(); + +}); + +test('0-parameter logging event calls should log empty strings', t => { + const rootLogger = require('../index')(); + const logger = rootLogger.get('layer value'); + + rootLogger.getLevels().forEach(level => { + // log nothing + logger[level](); + + const actual = rootLogger.getMessages(level); + + t.deepEquals(actual, [''], 'empty string should have been logged'); + + }); + + t.end(); + +}); + // TESTS FOR GET test('getMessages should return all messages logged at the specified level', (t) => { const rootLogger = require('../index')();