Skip to content

Commit 9a49d35

Browse files
committed
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
1 parent b664997 commit 9a49d35

File tree

3 files changed

+100
-14
lines changed

3 files changed

+100
-14
lines changed

Diff for: lib/handlebars/helpers/log.js

+16-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,19 @@
11
export default function(instance) {
2-
instance.registerHelper('log', function(message, options) {
3-
let level = options.data && options.data.level != null ? parseInt(options.data.level, 10) : 1;
4-
instance.log(level, message);
2+
instance.registerHelper('log', function(/* message, options */) {
3+
let args = [undefined],
4+
options = arguments[arguments.length - 1];
5+
for (let i = 0; i < arguments.length - 1; i++) {
6+
args.push(arguments[i]);
7+
}
8+
9+
let level = 1;
10+
if (options.hash.level != null) {
11+
level = options.hash.level;
12+
} else if (options.data && options.data.level != null) {
13+
level = options.data.level;
14+
}
15+
args[0] = level;
16+
17+
instance.log(... args);
518
});
619
}

Diff for: lib/handlebars/logger.js

+23-10
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,31 @@
11
let logger = {
2-
methodMap: { 0: 'debug', 1: 'info', 2: 'warn', 3: 'error' },
2+
methodMap: ['debug', 'info', 'warn', 'error'],
3+
level: 'info',
34

4-
// State enum
5-
DEBUG: 0,
6-
INFO: 1,
7-
WARN: 2,
8-
ERROR: 3,
9-
level: 1,
5+
// Maps a given level value to the `methodMap` indexes above.
6+
lookupLevel: function(level) {
7+
if (typeof level === 'string') {
8+
let levelMap = logger.methodMap.indexOf(level.toLowerCase());
9+
if (levelMap >= 0) {
10+
level = levelMap;
11+
} else {
12+
level = parseInt(level, 10);
13+
}
14+
}
15+
16+
return level;
17+
},
1018

1119
// Can be overridden in the host environment
12-
log: function(level, message) {
13-
if (typeof console !== 'undefined' && logger.level <= level) {
20+
log: function(level, ...message) {
21+
level = logger.lookupLevel(level);
22+
23+
if (typeof console !== 'undefined' && logger.lookupLevel(logger.level) <= level) {
1424
let method = logger.methodMap[level];
15-
(console[method] || console.log).call(console, message); // eslint-disable-line no-console
25+
if (!console[method]) { // eslint-disable-line no-console
26+
method = 'log';
27+
}
28+
console[method](...message); // eslint-disable-line no-console
1629
}
1730
}
1831
};

Diff for: spec/builtins.js

+61-1
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ describe('builtin helpers', function() {
286286
};
287287

288288
shouldCompileTo(string, [hash,,,, {level: '03'}], '');
289-
equals(3, levelArg);
289+
equals('03', levelArg);
290290
equals('whee', logArg);
291291
});
292292
it('should output to info', function() {
@@ -327,6 +327,66 @@ describe('builtin helpers', function() {
327327

328328
shouldCompileTo(string, [hash,,,, {level: '03'}], '');
329329
});
330+
331+
it('should handle string log levels', function() {
332+
var string = '{{log blah}}';
333+
var hash = { blah: 'whee' };
334+
var called;
335+
336+
console.error = function(log) {
337+
equals('whee', log);
338+
called = true;
339+
};
340+
341+
shouldCompileTo(string, [hash,,,, {level: 'error'}], '');
342+
equals(true, called);
343+
344+
called = false;
345+
346+
shouldCompileTo(string, [hash,,,, {level: 'ERROR'}], '');
347+
equals(true, called);
348+
});
349+
it('should handle hash log levels', function() {
350+
var string = '{{log blah level="error"}}';
351+
var hash = { blah: 'whee' };
352+
var called;
353+
354+
console.error = function(log) {
355+
equals('whee', log);
356+
called = true;
357+
};
358+
359+
shouldCompileTo(string, hash, '');
360+
equals(true, called);
361+
});
362+
it('should handle hash log levels', function() {
363+
var string = '{{log blah level="debug"}}';
364+
var hash = { blah: 'whee' };
365+
var called = false;
366+
367+
console.info = console.log = console.error = console.debug = function(log) {
368+
equals('whee', log);
369+
called = true;
370+
};
371+
372+
shouldCompileTo(string, hash, '');
373+
equals(false, called);
374+
});
375+
it('should pass multiple log arguments', function() {
376+
var string = '{{log blah "foo" 1}}';
377+
var hash = { blah: 'whee' };
378+
var called;
379+
380+
console.info = console.log = function(log1, log2, log3) {
381+
equals('whee', log1);
382+
equals('foo', log2);
383+
equals(1, log3);
384+
called = true;
385+
};
386+
387+
shouldCompileTo(string, hash, '');
388+
equals(true, called);
389+
});
330390
/*eslint-enable no-console */
331391
});
332392

0 commit comments

Comments
 (0)