Skip to content

Commit

Permalink
feat: Merge pull request #8 from pelias/support-varargs-log-events
Browse files Browse the repository at this point in the history
Support varargs log events
  • Loading branch information
trescube authored Sep 19, 2017
2 parents ca50aa2 + c69cdc8 commit a8db74c
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
12 changes: 11 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const _ = require('lodash');
const util = require('util');

module.exports = () => {
// initialize arrays for all levels
Expand All @@ -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
Expand Down
39 changes: 39 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')();
Expand Down

0 comments on commit a8db74c

Please sign in to comment.