Comparing version 1.1.0 to 1.2.0
{ | ||
"name": "errorlog", | ||
"description": "Yet another logger for Node", | ||
"version": "1.1.0", | ||
"version": "1.2.0", | ||
"author": { | ||
@@ -6,0 +6,0 @@ "name": "Pier Fumagalli", |
@@ -12,2 +12,3 @@ Simple Error Log | ||
* [Extras](#extras) | ||
* [Colorization](#colorization) | ||
* [Log rotation](#log-rotation) | ||
@@ -107,3 +108,3 @@ * [License (MIT)](#license-mit-) | ||
```javascript | ||
log('I have %d %s, and an object %j', 3, 'apples', { foo: 'bar' }); | ||
log.debug('I have %d %s, and an object %j', 3, 'apples', { foo: 'bar' }); | ||
``` | ||
@@ -114,5 +115,6 @@ | ||
```text | ||
2015-03-31T13:58:06.601Z - I have 3 apples and an object {"foo":"bar"} | ||
2015-03-31T13:58:06.601Z - DEBUG - I have 3 apples and an object {"foo":"bar"} | ||
``` | ||
#### Levels | ||
@@ -126,3 +128,3 @@ | ||
log('Default, logged as ERROR'); | ||
log('Default, logged unless everything is OFF'); | ||
log.debug('A debug message'); | ||
@@ -143,3 +145,3 @@ log.info('Informational message'); | ||
var error = new Error('This is an error'); | ||
log('I have %d %s', 3, 'apples', extra, error); | ||
log.error('I have %d %s', 3, 'apples', extra, error); | ||
``` | ||
@@ -150,3 +152,3 @@ | ||
```text | ||
2015-03-30T16:45:01.718Z - I have 3 apples | ||
2015-03-30T16:45:01.718Z - ERROR - I have 3 apples | ||
>>> {"key":"a simple value"} | ||
@@ -158,2 +160,23 @@ Error: This is an error | ||
#### Colorization | ||
If the configured `log` is either `process.stderr` (the default) or | ||
`process.stderr`, the output to the console will be colorized. | ||
> And now I wish GitHub didn't filter the `style` attribute... | ||
This behavior can be changed by setting the `errorlog.defaultColorize` property | ||
to `false`. | ||
```javascript | ||
var errorlog = require('errorlog'); | ||
errorlog.defaultColorize = false; | ||
log('There you go, some bland output...'); | ||
``` | ||
To get an idea of the output, simply run the `sample.js` included here. | ||
#### Log rotation | ||
@@ -160,0 +183,0 @@ |
'use strict'; | ||
var util = require('util'); | ||
function format() { | ||
@@ -9,3 +11,3 @@ var arg = arguments; | ||
// Process the format string and parameters | ||
if (typeof(arg[0]) === 'string') { | ||
if (util.isString(arg[0])) { | ||
var len = arg.length - 1; | ||
@@ -49,14 +51,2 @@ msg += arg[0].replace(/%[sdj%]/g, function(val) { | ||
function wrap(stream) { | ||
if (typeof(stream.write) === 'function') { | ||
return function log(message) { | ||
stream.write(new Date().toISOString() + ' - ' + message + '\n'); | ||
} | ||
} else if (typeof(stream) === 'function') { | ||
return stream; | ||
} else { | ||
throw new Error('The "logger" must be a function or Writable stream'); | ||
} | ||
}; | ||
// Logging levels | ||
@@ -69,2 +59,3 @@ var ALL = -1; | ||
var OFF = Number.MAX_SAFE_INTEGER; | ||
var LOG = OFF - 1; // internal only | ||
@@ -74,3 +65,24 @@ // Our default log function, shared where not overridden | ||
var defaultLevel = INFO; | ||
var defaultColorize = true; | ||
function wrap(stream) { | ||
if (util.isFunction(stream.write)) { | ||
var colorize = ((stream == process.stderr) || (stream == process.stdout)); | ||
var log = function log(message) { | ||
if (colorize && defaultColorize) { | ||
stream.write('\x1B[38;5;240m' + new Date().toISOString() + '\x1B[0m - ' + message + '\n'); | ||
} else { | ||
stream.write(new Date().toISOString() + ' - ' + message + '\n'); | ||
} | ||
} | ||
log.colorize = true; | ||
return log; | ||
} else if (util.isFunction(stream)) { | ||
return stream; | ||
} else { | ||
throw new Error('The "logger" must be a function or Writable stream'); | ||
} | ||
}; | ||
// Simple log emitter | ||
@@ -81,7 +93,7 @@ function simplelog(options) { | ||
var category = null; | ||
var log = defaultLog; | ||
var level = defaultLevel; | ||
var level = null; | ||
var log = null; | ||
// Looks like a stream, just bind it | ||
if (typeof(options.write) === 'function') { | ||
if (util.isFunction(options.write)) { | ||
log = wrap(options); | ||
@@ -91,3 +103,3 @@ } | ||
// Simple function, use for logging | ||
else if (typeof(options) === 'function') { | ||
else if (util.isFunction(options)) { | ||
log = options; | ||
@@ -97,3 +109,3 @@ } | ||
// String only, must be a category name | ||
else if (typeof(options) === 'string') { | ||
else if (util.isString(options)) { | ||
category = options || null; | ||
@@ -103,5 +115,5 @@ } | ||
// Object: may contain "logger" and "category" | ||
else if (typeof(options) === 'object') { | ||
else if (util.isObject(options)) { | ||
if (options.category) category = String(options.category) || null; | ||
if (options.level) level = Number(options.level) || defaultLevel; | ||
if (options.level) level = Number(options.level) || null; | ||
if (options.logger) log = wrap(options.logger); | ||
@@ -114,12 +126,50 @@ } | ||
// Our emitter function with or without categories | ||
var emit = category ? | ||
function emit() { log(category + ' - ' + format.apply(null, arguments)) } : | ||
function emit() { log(format.apply(null, arguments)) } ; | ||
function emit(logLevel, args) { | ||
if (logLevel < (level || defaultLevel)) return; | ||
var currentLog = log || defaultLog; | ||
var colorize = (currentLog.colorize || false) && defaultColorize; | ||
var data = [ format.apply(null, args) ]; | ||
if (category) { | ||
data.unshift(': '); | ||
if (colorize) data.unshift('\x1B[0m'); | ||
data.unshift(category); | ||
if (colorize) { | ||
if (logLevel <= DEBUG) data.unshift('\x1B[38;5;25;4m'); | ||
else if (logLevel <= INFO) data.unshift('\x1B[38;5;70;4m'); | ||
else if (logLevel <= WARN) data.unshift('\x1B[38;5;100;4m'); | ||
else if (logLevel <= ERROR) data.unshift('\x1B[38;5;131;4m'); | ||
else data.unshift('\x1B[38;5;37;4m'); | ||
} | ||
} | ||
data.unshift(' - '); | ||
if (colorize) data.unshift('\x1B[0m'); | ||
if (logLevel <= DEBUG) data.unshift('DEBUG'); | ||
else if (logLevel <= INFO) data.unshift(' INFO'); | ||
else if (logLevel <= WARN) data.unshift(' WARN'); | ||
else if (logLevel <= ERROR) data.unshift('ERROR'); | ||
else data.unshift(' LOG'); | ||
if (colorize === true) { | ||
if (logLevel <= DEBUG) data.unshift('\x1B[38;5;33m'); | ||
else if (logLevel <= INFO) data.unshift('\x1B[38;5;76m'); | ||
else if (logLevel <= WARN) data.unshift('\x1B[38;5;142m'); | ||
else if (logLevel <= ERROR) data.unshift('\x1B[38;5;167m'); | ||
else data.unshift('\x1B[38;5;44m'); | ||
} | ||
currentLog(data.join('')); | ||
} | ||
// Return our logging function | ||
var logger = function log() { if (level <= ERROR) emit.apply(null, arguments) } | ||
logger.debug = function debug() { if (level <= DEBUG) emit.apply(null, arguments) } | ||
logger.info = function info() { if (level <= INFO) emit.apply(null, arguments) } | ||
logger.warn = function warn() { if (level <= WARN) emit.apply(null, arguments) } | ||
logger.error = function error() { if (level <= ERROR) emit.apply(null, arguments) } | ||
var logger = function log() { emit(LOG, arguments) } | ||
logger.debug = function debug() { emit(DEBUG, arguments) } | ||
logger.info = function info() { emit(INFO, arguments) } | ||
logger.warn = function warn() { emit(WARN, arguments) } | ||
logger.error = function error() { emit(ERROR, arguments) } | ||
return logger; | ||
@@ -153,3 +203,9 @@ | ||
}, | ||
'defaultColorize': { | ||
configurable: false, | ||
enumerable: true, | ||
get: function() { return defaultColorize }, | ||
set: function(level) { defaultColorize = Boolean(level) } | ||
}, | ||
}); |
@@ -46,3 +46,3 @@ 'use strict'; | ||
log('a simple message to log'); | ||
expect(stream.check()).to.equal('a simple message to log'); | ||
expect(stream.check()).to.equal(' LOG - a simple message to log'); | ||
}); | ||
@@ -52,3 +52,3 @@ | ||
log('number %d string %s json %j percent % and %% borked %x nan %d end', 123.456, 'foobar', { hello: 'world' }, 'foo' ); | ||
expect(stream.check()).to.equal('number 123.456 string foobar json {"hello":"world"} percent % and % borked %x nan NaN end'); | ||
expect(stream.check()).to.equal(' LOG - number 123.456 string foobar json {"hello":"world"} percent % and % borked %x nan NaN end'); | ||
}); | ||
@@ -58,3 +58,3 @@ | ||
log('%d %d %d %d', 1, 2, 3); | ||
expect(stream.check()).to.equal('1 2 3 %d'); | ||
expect(stream.check()).to.equal(' LOG - 1 2 3 %d'); | ||
}); | ||
@@ -64,3 +64,3 @@ | ||
log('%d %d %d %d', 1, 2, 3, 4, 5, 'hello', { foo: 'bar' }); | ||
expect(stream.check()).to.equal('1 2 3 4\n >>> 5\n >>> "hello"\n >>> {"foo":"bar"}'); | ||
expect(stream.check()).to.equal(' LOG - 1 2 3 4\n >>> 5\n >>> "hello"\n >>> {"foo":"bar"}'); | ||
}); | ||
@@ -70,3 +70,3 @@ | ||
log('test message', new Error('error title')); | ||
expect(stream.check()).to.match(/^test message\n Error: error title\n at/m); | ||
expect(stream.check()).to.match(/^ LOG - test message\n Error: error title\n at/m); | ||
}); | ||
@@ -76,3 +76,3 @@ | ||
log('test %s message', 'nice', { foo: 'bar' }, new Error('error title')); | ||
expect(stream.check()).to.match(/^test nice message\n >>> {"foo":"bar"}\n Error: error title\n at/m); | ||
expect(stream.check()).to.match(/^ LOG - test nice message\n >>> {"foo":"bar"}\n Error: error title\n at/m); | ||
}); | ||
@@ -82,3 +82,3 @@ | ||
simplelog('my own category')('A categorized message'); | ||
expect(stream.check()).to.equal('my own category - A categorized message'); | ||
expect(stream.check()).to.equal(' LOG - my own category: A categorized message'); | ||
}); | ||
@@ -92,4 +92,4 @@ | ||
expect(stream.check()).to.equal('this should go to the shared logger'); | ||
expect(logger.check()).to.equal('different logger'); | ||
expect(stream.check()).to.equal(' LOG - this should go to the shared logger'); | ||
expect(logger.check()).to.equal(' LOG - different logger'); | ||
}) | ||
@@ -103,4 +103,4 @@ | ||
expect(stream.check()).to.equal('this should go to the shared logger'); | ||
expect(logger.check()).to.equal('foobar - different logger'); | ||
expect(stream.check()).to.equal(' LOG - this should go to the shared logger'); | ||
expect(logger.check()).to.equal(' LOG - foobar: different logger'); | ||
}) | ||
@@ -115,4 +115,4 @@ | ||
expect(stream.check()).to.equal('this should go to the shared logger'); | ||
expect(data).to.equal('different logger'); | ||
expect(stream.check()).to.equal(' LOG - this should go to the shared logger'); | ||
expect(data).to.equal(' LOG - different logger'); | ||
}) | ||
@@ -127,4 +127,4 @@ | ||
expect(stream.check()).to.equal('this should go to the shared logger'); | ||
expect(data).to.equal('foobar - different logger'); | ||
expect(stream.check()).to.equal(' LOG - this should go to the shared logger'); | ||
expect(data).to.equal(' LOG - foobar: different logger'); | ||
}) | ||
@@ -136,8 +136,8 @@ | ||
var logger = simplelog({level: simplelog.ALL, logger: append}); | ||
logger.debug('DEBUG'); | ||
logger.info ('INFO'); | ||
logger.warn ('WARN'); | ||
logger.error('ERROR'); | ||
logger('DEFAULT'); | ||
expect(data).to.eql(['DEBUG','INFO','WARN','ERROR','DEFAULT']); | ||
logger.debug('debug'); | ||
logger.info ('info'); | ||
logger.warn ('warn'); | ||
logger.error('error'); | ||
logger('log'); | ||
expect(data).to.eql(['DEBUG - debug',' INFO - info',' WARN - warn','ERROR - error',' LOG - log']); | ||
}) | ||
@@ -149,8 +149,8 @@ | ||
var logger = simplelog({level: simplelog.DEBUG, logger: append}); | ||
logger.debug('DEBUG'); | ||
logger.info ('INFO'); | ||
logger.warn ('WARN'); | ||
logger.error('ERROR'); | ||
logger('DEFAULT'); | ||
expect(data).to.eql(['DEBUG','INFO','WARN','ERROR','DEFAULT']); | ||
logger.debug('debug'); | ||
logger.info ('info'); | ||
logger.warn ('warn'); | ||
logger.error('error'); | ||
logger('log'); | ||
expect(data).to.eql(['DEBUG - debug',' INFO - info',' WARN - warn','ERROR - error',' LOG - log']); | ||
}) | ||
@@ -162,8 +162,8 @@ | ||
var logger = simplelog({level: simplelog.INFO, logger: append}); | ||
logger.debug('DEBUG'); | ||
logger.info ('INFO'); | ||
logger.warn ('WARN'); | ||
logger.error('ERROR'); | ||
logger('DEFAULT'); | ||
expect(data).to.eql(['INFO','WARN','ERROR','DEFAULT']); | ||
logger.debug('debug'); | ||
logger.info ('info'); | ||
logger.warn ('warn'); | ||
logger.error('error'); | ||
logger('log'); | ||
expect(data).to.eql([' INFO - info',' WARN - warn','ERROR - error',' LOG - log']); | ||
}) | ||
@@ -175,8 +175,8 @@ | ||
var logger = simplelog({level: simplelog.WARN, logger: append}); | ||
logger.debug('DEBUG'); | ||
logger.info ('INFO'); | ||
logger.warn ('WARN'); | ||
logger.error('ERROR'); | ||
logger('DEFAULT'); | ||
expect(data).to.eql(['WARN','ERROR','DEFAULT']); | ||
logger.debug('debug'); | ||
logger.info ('info'); | ||
logger.warn ('warn'); | ||
logger.error('error'); | ||
logger('log'); | ||
expect(data).to.eql([' WARN - warn','ERROR - error',' LOG - log']); | ||
}) | ||
@@ -188,8 +188,8 @@ | ||
var logger = simplelog({level: simplelog.ERROR, logger: append}); | ||
logger.debug('DEBUG'); | ||
logger.info ('INFO'); | ||
logger.warn ('WARN'); | ||
logger.error('ERROR'); | ||
logger('DEFAULT'); | ||
expect(data).to.eql(['ERROR','DEFAULT']); | ||
logger.debug('debug'); | ||
logger.info ('info'); | ||
logger.warn ('warn'); | ||
logger.error('error'); | ||
logger('log'); | ||
expect(data).to.eql(['ERROR - error',' LOG - log']); | ||
}) | ||
@@ -201,10 +201,59 @@ | ||
var logger = simplelog({level: simplelog.OFF, logger: append}); | ||
logger.debug('DEBUG'); | ||
logger.info ('INFO'); | ||
logger.warn ('WARN'); | ||
logger.error('ERROR'); | ||
logger('DEFAULT'); | ||
logger.debug('debug'); | ||
logger.info ('info'); | ||
logger.warn ('warn'); | ||
logger.error('error'); | ||
logger('log'); | ||
expect(data).to.eql([]); | ||
}) | ||
it('should honor switches in the default level', function() { | ||
var data = []; | ||
var append = function(more) { data.push(more) }; | ||
var oldLevel = simplelog.defaultLevel; | ||
try { | ||
simplelog.defaultLevel = simplelog.OFF; | ||
var logger = simplelog({logger: append}); | ||
logger('this should not show up'); | ||
expect(data).to.eql([]); | ||
simplelog.defaultLevel = simplelog.ALL; | ||
logger('this must show up'); | ||
expect(data).to.eql([' LOG - this must show up']); | ||
} finally { | ||
simplelog.defaultLevel = oldLevel; | ||
} | ||
}) | ||
it('should honor switches in the default log', function() { | ||
var data1 = []; | ||
var data2 = []; | ||
var append1 = function(more) { data1.push(more) }; | ||
var append2 = function(more) { data2.push(more) }; | ||
var oldLog = simplelog.defaultLog; | ||
try { | ||
simplelog.defaultLog = append1; | ||
var logger = simplelog(); | ||
logger('logged to data 1'); | ||
simplelog.defaultLog = append2; | ||
logger('logged to data 2'); | ||
expect(data1).to.eql([' LOG - logged to data 1']); | ||
expect(data2).to.eql([' LOG - logged to data 2']); | ||
} finally { | ||
simplelog.defaultLog = oldLog; | ||
} | ||
}) | ||
}); | ||
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
21686
6
404
204