Comparing version 1.0.2 to 1.1.0
{ | ||
"name": "errorlog", | ||
"description": "Yet another logger for Node", | ||
"version": "1.0.2", | ||
"version": "1.1.0", | ||
"author": { | ||
@@ -6,0 +6,0 @@ "name": "Pier Fumagalli", |
@@ -8,3 +8,5 @@ Simple Error Log | ||
* [Options and properties](#options-and-properties) | ||
* [Default Logger and Level](#default-logger-and-level) | ||
* [Logging](#logging) | ||
* [Levels](#levels) | ||
* [Extras](#extras) | ||
@@ -49,2 +51,9 @@ * [Log rotation](#log-rotation) | ||
* `category`: a category name that will be inserted in the message to log. | ||
* `level`: the minimum level to log messages at | ||
* `errorlog.DEBUG` or `100`: debugging messages. | ||
* `errorlog.INFO` or `200`: informational messages _(default)_. | ||
* `errorlog.WARN` or `300`: warning messages. | ||
* `errorlog.ERROR` or `400`: error messages. | ||
* `errorlog.ALL` or any number smaller than `100`: everything is logged. | ||
* `errorlog.OFF` or any number greater than `400`: disable logging. | ||
@@ -56,3 +65,4 @@ Options can be specified at construction wrapped in a simple object: | ||
logger: process.stdout, | ||
category: 'my category' | ||
category: 'my category', | ||
level: 200 | ||
}); | ||
@@ -73,3 +83,3 @@ ``` | ||
#### Default Logger | ||
#### Default Logger and Level | ||
@@ -82,9 +92,11 @@ The **default** `Writable` _stream_ or logging `function` can be configured | ||
errorlog.defaultLog = process.stdout; | ||
errorlog.defaultLevel = errorlog.DEBUG; | ||
// All logs will use `process.stdout` unless overridden | ||
var log = errorlog('my category'); | ||
log('A message to log'); | ||
``` | ||
The default for this property is `process.stderr`, and it will affect all | ||
loggers created **after** setting it. | ||
The default for output will be `process.stderr` and minimum level `ERROR`: | ||
these values will be applied only to loggers created **after** setting them. | ||
@@ -108,2 +120,18 @@ | ||
#### Levels | ||
Logging levels are associated to various functions of the logger, for example: | ||
```javascript | ||
var errorlog = require('errorlog'); | ||
var log = errorlog({...}); | ||
log('Default, logged as ERROR'); | ||
log.debug('A debug message'); | ||
log.info('Informational message'); | ||
log.warn('Some sort of warning'); | ||
log.error('Like calling log(...)'); | ||
``` | ||
#### Extras | ||
@@ -110,0 +138,0 @@ |
@@ -60,4 +60,13 @@ 'use strict'; | ||
// Our default stream, shared where not overridden | ||
// Logging levels | ||
var ALL = -1; | ||
var DEBUG = 100; | ||
var INFO = 200; | ||
var WARN = 300; | ||
var ERROR = 400; | ||
var OFF = Number.MAX_SAFE_INTEGER; | ||
// Our default log function, shared where not overridden | ||
var defaultLog = wrap(process.stderr); | ||
var defaultLevel = INFO; | ||
@@ -68,4 +77,5 @@ // Simple log emitter | ||
var category = null; | ||
var log = defaultLog; | ||
var category = null; | ||
var level = defaultLevel; | ||
@@ -89,4 +99,5 @@ // Looks like a stream, just bind it | ||
else if (typeof(options) === 'object') { | ||
if (options.category) category = String(options.category) || null; | ||
if (options.level) level = Number(options.level) || defaultLevel; | ||
if (options.logger) log = wrap(options.logger); | ||
category = options.category || null; | ||
} | ||
@@ -97,11 +108,15 @@ | ||
// Our emitter function wotj categories | ||
if (category) return function emit() { | ||
log(category + ' - ' + format.apply(null, arguments)); | ||
} | ||
// 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)) } ; | ||
// Plain boring emitter function | ||
return function emit() { | ||
log(format.apply(null, arguments)); | ||
} | ||
// 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) } | ||
return logger; | ||
} | ||
@@ -112,7 +127,24 @@ | ||
exports.format = format; | ||
Object.defineProperty(exports, 'defaultLog', { | ||
configurable: false, | ||
enumerable: true, | ||
get: function() { return defaultLog }, | ||
set: function(log) { defaultLog = wrap(log) } | ||
Object.defineProperties(exports, { | ||
// Levels | ||
'ALL': { configurable: false, enumerable: true, writable: false, value: ALL }, | ||
'DEBUG': { configurable: false, enumerable: true, writable: false, value: DEBUG }, | ||
'INFO': { configurable: false, enumerable: true, writable: false, value: INFO }, | ||
'WARN': { configurable: false, enumerable: true, writable: false, value: WARN }, | ||
'ERROR': { configurable: false, enumerable: true, writable: false, value: ERROR }, | ||
'OFF': { configurable: false, enumerable: true, writable: false, value: OFF }, | ||
// Defaults | ||
'defaultLog': { | ||
configurable: false, | ||
enumerable: true, | ||
get: function() { return defaultLog }, | ||
set: function(log) { defaultLog = wrap(log) } | ||
}, | ||
'defaultLevel': { | ||
configurable: false, | ||
enumerable: true, | ||
get: function() { return defaultLevel }, | ||
set: function(level) { defaultLevel = Number(level) || ERROR } | ||
}, | ||
}); |
@@ -32,2 +32,14 @@ 'use strict'; | ||
it('should expose some basic properties', function() { | ||
expect(simplelog).to.be.a('function'); | ||
expect(simplelog.ALL ).to.equal( -1); | ||
expect(simplelog.DEBUG).to.equal(100); | ||
expect(simplelog.INFO ).to.equal(200); | ||
expect(simplelog.WARN ).to.equal(300); | ||
expect(simplelog.ERROR).to.equal(400); | ||
expect(simplelog.OFF ) .to.equal(Number.MAX_SAFE_INTEGER); | ||
expect(simplelog.defaultLog).to.be.a('function'); | ||
expect(simplelog.defaultLevel).to.equal(simplelog.INFO); | ||
}); | ||
it('should log a simple message', function() { | ||
@@ -110,3 +122,74 @@ log('a simple message to log'); | ||
it('should honor the ALL level', function() { | ||
var data = []; | ||
var append = function(more) { data.push(more) }; | ||
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']); | ||
}) | ||
it('should honor the DEBUG level', function() { | ||
var data = []; | ||
var append = function(more) { data.push(more) }; | ||
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']); | ||
}) | ||
it('should honor the INFO level', function() { | ||
var data = []; | ||
var append = function(more) { data.push(more) }; | ||
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']); | ||
}) | ||
it('should honor the WARN level', function() { | ||
var data = []; | ||
var append = function(more) { data.push(more) }; | ||
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']); | ||
}) | ||
it('should honor the ERROR level', function() { | ||
var data = []; | ||
var append = function(more) { data.push(more) }; | ||
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']); | ||
}) | ||
it('should honor the OFF level', function() { | ||
var data = []; | ||
var append = function(more) { data.push(more) }; | ||
var logger = simplelog({level: simplelog.OFF, logger: append}); | ||
logger.debug('DEBUG'); | ||
logger.info ('INFO'); | ||
logger.warn ('WARN'); | ||
logger.error('ERROR'); | ||
logger('DEFAULT'); | ||
expect(data).to.eql([]); | ||
}) | ||
}); | ||
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
16825
299
181