console-log-level
Advanced tools
Comparing version 1.0.0 to 1.1.0
11
index.js
'use strict'; | ||
var util = require('util'); | ||
var levels = ['debug', 'info', 'warn', 'error', 'fatal']; | ||
@@ -18,2 +20,5 @@ | ||
if (!shouldLog(level)) return; | ||
var prefix = opts.prefix; | ||
switch (level) { | ||
@@ -23,2 +28,8 @@ case 'debug': level = 'info'; break; | ||
} | ||
if (prefix) { | ||
if (typeof prefix === 'function') prefix = prefix(); | ||
arguments[0] = util.format(prefix, arguments[0]); | ||
} | ||
console[level].apply(console, arguments); | ||
@@ -25,0 +36,0 @@ }; |
{ | ||
"name": "console-log-level", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "The most simple logger imaginable", | ||
@@ -31,3 +31,7 @@ "main": "index.js", | ||
"tape": "^3.0.3" | ||
} | ||
}, | ||
"coordinates": [ | ||
55.687684, | ||
12.5955911 | ||
] | ||
} |
@@ -7,4 +7,3 @@ # console-log-level | ||
Log levels supported: debug, info, warn, error and fatal (defaults to | ||
'info'). | ||
Log levels supported: debug, info, warn, error and fatal. | ||
@@ -31,4 +30,24 @@ [![build status](https://secure.travis-ci.org/watson/console-log-level.png)](http://travis-ci.org/watson/console-log-level) | ||
## Options | ||
Configure the logger by passing an options object: | ||
```js | ||
var log = require('console-log-level')({ | ||
prefix: function () { return new Date().toISOString(); }, | ||
level: 'info' | ||
}); | ||
``` | ||
### level | ||
A `string` to specify the log level. Defaults to `info`. | ||
### prefix | ||
Specify this option if you want to set a prefix for all log messages. | ||
This must be a `string` or a `function` that returns a string. | ||
## License | ||
MIT |
70
test.js
@@ -25,2 +25,14 @@ 'use strict'; | ||
var spyOn = function (method, spy) { | ||
console['~' + method] = console[method]; | ||
console[method] = function () { | ||
spy.apply(console, arguments); | ||
}; | ||
}; | ||
var spyOff = function (method) { | ||
console[method] = console['~' + method]; | ||
delete console['~' + method]; | ||
}; | ||
test('log all', function (t) { | ||
@@ -106,1 +118,59 @@ var logger = Logger({ level: 'debug' }); | ||
}); | ||
test('set prefix', function (t) { | ||
var now = new Date().toISOString(); | ||
var logger = Logger({ prefix: now }); | ||
var msg = 'bar'; | ||
spyOn('info', function () { | ||
spyOff('info'); | ||
t.equal(arguments[0], now + ' foo %s', 'first arg ok'); | ||
t.equal(arguments[1], msg, 'second arg ok'); | ||
}); | ||
spyOn('warn', function () { | ||
spyOff('warn'); | ||
t.equal(arguments[0], now + ' foo %s', 'first arg ok'); | ||
t.equal(arguments[1], msg, 'second arg ok'); | ||
}); | ||
spyOn('error', function () { | ||
spyOff('error'); | ||
t.equal(arguments[0], now + ' foo %s', 'first arg ok'); | ||
t.equal(arguments[1], msg, 'second arg ok'); | ||
t.end(); | ||
}); | ||
logger.info('foo %s', msg); | ||
logger.warn('foo %s', msg); | ||
logger.error('foo %s', msg); | ||
}); | ||
test('set prefix with function', function (t) { | ||
var now = new Date().toISOString(); | ||
var logger = Logger({ prefix: function () { return now; } }); | ||
var msg = 'bar'; | ||
spyOn('info', function () { | ||
spyOff('info'); | ||
t.equal(arguments[0], now + ' foo %s', 'first arg ok'); | ||
t.equal(arguments[1], msg, 'second arg ok'); | ||
}); | ||
spyOn('warn', function () { | ||
spyOff('warn'); | ||
t.equal(arguments[0], now + ' foo %s', 'first arg ok'); | ||
t.equal(arguments[1], msg, 'second arg ok'); | ||
}); | ||
spyOn('error', function () { | ||
spyOff('error'); | ||
t.equal(arguments[0], now + ' foo %s', 'first arg ok'); | ||
t.equal(arguments[1], msg, 'second arg ok'); | ||
t.end(); | ||
}); | ||
logger.info('foo %s', msg); | ||
logger.warn('foo %s', msg); | ||
logger.error('foo %s', msg); | ||
}); |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
8907
170
52