From 9a49d350231edbe82c9982df9a99f39596fb96b7 Mon Sep 17 00:00:00 2001 From: kpdecker Date: Mon, 3 Aug 2015 11:36:36 -0500 Subject: [PATCH] Improve logging API Adds multiple variable support and the ability to set statement level logging semantics. This breaks that logger API, cleaning up the manner in which enums are set, but the other behaviors are backwards compatible. Fixes #956 --- lib/handlebars/helpers/log.js | 19 +++++++++-- lib/handlebars/logger.js | 33 +++++++++++++------ spec/builtins.js | 62 ++++++++++++++++++++++++++++++++++- 3 files changed, 100 insertions(+), 14 deletions(-) diff --git a/lib/handlebars/helpers/log.js b/lib/handlebars/helpers/log.js index ab83604b2..4bde4a10d 100644 --- a/lib/handlebars/helpers/log.js +++ b/lib/handlebars/helpers/log.js @@ -1,6 +1,19 @@ export default function(instance) { - instance.registerHelper('log', function(message, options) { - let level = options.data && options.data.level != null ? parseInt(options.data.level, 10) : 1; - instance.log(level, message); + instance.registerHelper('log', function(/* message, options */) { + let args = [undefined], + options = arguments[arguments.length - 1]; + for (let i = 0; i < arguments.length - 1; i++) { + args.push(arguments[i]); + } + + let level = 1; + if (options.hash.level != null) { + level = options.hash.level; + } else if (options.data && options.data.level != null) { + level = options.data.level; + } + args[0] = level; + + instance.log(... args); }); } diff --git a/lib/handlebars/logger.js b/lib/handlebars/logger.js index 823d71599..1d583ddb9 100644 --- a/lib/handlebars/logger.js +++ b/lib/handlebars/logger.js @@ -1,18 +1,31 @@ let logger = { - methodMap: { 0: 'debug', 1: 'info', 2: 'warn', 3: 'error' }, + methodMap: ['debug', 'info', 'warn', 'error'], + level: 'info', - // State enum - DEBUG: 0, - INFO: 1, - WARN: 2, - ERROR: 3, - level: 1, + // Maps a given level value to the `methodMap` indexes above. + lookupLevel: function(level) { + if (typeof level === 'string') { + let levelMap = logger.methodMap.indexOf(level.toLowerCase()); + if (levelMap >= 0) { + level = levelMap; + } else { + level = parseInt(level, 10); + } + } + + return level; + }, // Can be overridden in the host environment - log: function(level, message) { - if (typeof console !== 'undefined' && logger.level <= level) { + log: function(level, ...message) { + level = logger.lookupLevel(level); + + if (typeof console !== 'undefined' && logger.lookupLevel(logger.level) <= level) { let method = logger.methodMap[level]; - (console[method] || console.log).call(console, message); // eslint-disable-line no-console + if (!console[method]) { // eslint-disable-line no-console + method = 'log'; + } + console[method](...message); // eslint-disable-line no-console } } }; diff --git a/spec/builtins.js b/spec/builtins.js index e5d923f87..6a02dc627 100644 --- a/spec/builtins.js +++ b/spec/builtins.js @@ -286,7 +286,7 @@ describe('builtin helpers', function() { }; shouldCompileTo(string, [hash,,,, {level: '03'}], ''); - equals(3, levelArg); + equals('03', levelArg); equals('whee', logArg); }); it('should output to info', function() { @@ -327,6 +327,66 @@ describe('builtin helpers', function() { shouldCompileTo(string, [hash,,,, {level: '03'}], ''); }); + + it('should handle string log levels', function() { + var string = '{{log blah}}'; + var hash = { blah: 'whee' }; + var called; + + console.error = function(log) { + equals('whee', log); + called = true; + }; + + shouldCompileTo(string, [hash,,,, {level: 'error'}], ''); + equals(true, called); + + called = false; + + shouldCompileTo(string, [hash,,,, {level: 'ERROR'}], ''); + equals(true, called); + }); + it('should handle hash log levels', function() { + var string = '{{log blah level="error"}}'; + var hash = { blah: 'whee' }; + var called; + + console.error = function(log) { + equals('whee', log); + called = true; + }; + + shouldCompileTo(string, hash, ''); + equals(true, called); + }); + it('should handle hash log levels', function() { + var string = '{{log blah level="debug"}}'; + var hash = { blah: 'whee' }; + var called = false; + + console.info = console.log = console.error = console.debug = function(log) { + equals('whee', log); + called = true; + }; + + shouldCompileTo(string, hash, ''); + equals(false, called); + }); + it('should pass multiple log arguments', function() { + var string = '{{log blah "foo" 1}}'; + var hash = { blah: 'whee' }; + var called; + + console.info = console.log = function(log1, log2, log3) { + equals('whee', log1); + equals('foo', log2); + equals(1, log3); + called = true; + }; + + shouldCompileTo(string, hash, ''); + equals(true, called); + }); /*eslint-enable no-console */ });