From 6b397aaeaeb802124eb2101712f1f2c90c242df0 Mon Sep 17 00:00:00 2001 From: Johan Brook Date: Wed, 25 Feb 2015 21:36:04 +0100 Subject: [PATCH] Loggers should accept a varying list of parameters as tags. --- loggers/local.js | 19 +++++++++++++++++-- loggers/loggly.js | 11 +++++++++-- tests/logger-spec.coffee | 4 ++++ 3 files changed, 30 insertions(+), 4 deletions(-) diff --git a/loggers/local.js b/loggers/local.js index e29c92b..9013b6a 100644 --- a/loggers/local.js +++ b/loggers/local.js @@ -1,4 +1,9 @@ // Local log + +function toString(arr) { + return '[' + _.invoke(arr, 'toUpperCase').join(' ') + ']'; +} + Loggers.local = function(level, message, tags) { var method = (function() { switch (level) { @@ -16,8 +21,18 @@ Loggers.local = function(level, message, tags) { [EMAILS SOMETHING-ELSE] This is my message. */ - tags = _.invoke(tags, 'toUpperCase').join(' '); - console[method]('[' + tags + ']', message); + + var tagsArgs = _.rest(arguments, 2); + + if(!Array.isArray(tags) && tagsArgs.length) { + tags = tagsArgs; + } + + if(Array.isArray(tags)) { + tags = toString(tags); + } + + console[method](tags, message); } else { console[method](message); } diff --git a/loggers/loggly.js b/loggers/loggly.js index 9b8b2ed..3e64bbf 100644 --- a/loggers/loggly.js +++ b/loggers/loggly.js @@ -7,8 +7,15 @@ Loggers.loggly = function(logglyClient, baseParams) { // @param {string} message // @param {(string|array)} [tags] return function log(level, message, tags) { - if (tags && _.isString(tags)) - tags = [tags]; + if(tags) { + var tagsArgs = _.rest(arguments, 2); + + if(_.isString(tags)) { + tags = [tags]; + } else if(tagsArgs.length){ + tags = tagsArgs; + } + } var toLog = { message: message, diff --git a/tests/logger-spec.coffee b/tests/logger-spec.coffee index 74b154d..7dcec12 100644 --- a/tests/logger-spec.coffee +++ b/tests/logger-spec.coffee @@ -42,6 +42,10 @@ describe 'Logger', -> Logger[method]('Foo', ['bar', 'baz']) spies[method].should.have.been.calledWith '[BAR BAZ]', 'Foo' + it 'should be able to take a dynamic list of tags', -> + Logger.info 'Foo', 'tag1', 'tag2', 'tag3' + spies.info.should.have.been.calledWith '[TAG1 TAG2 TAG3]', 'Foo' + describe 'logger', -> it 'should exist as a function', ->