added mocha, simplified logger by removing levels and making immutable
This commit is contained in:
+34
-65
@@ -1,80 +1,49 @@
|
|||||||
"use strict";
|
"use strict";
|
||||||
var debug = require('./debug')('logger')
|
var debug = require('./debug')('logger')
|
||||||
, levels = require('./levels')
|
, util = require('util');
|
||||||
, util = require('util')
|
|
||||||
, DEFAULT_CATEGORY = '[default]';
|
module.exports = function Logger(dispatch, category) {
|
||||||
|
if (typeof dispatch !== 'function') {
|
||||||
|
throw new Error("Logger must have a dispatch delegate.");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!category) {
|
||||||
|
throw new Error("Logger must have a category.");
|
||||||
|
}
|
||||||
|
|
||||||
|
function log() {
|
||||||
|
var args = Array.prototype.slice.call(arguments)
|
||||||
|
, logLevel = args.shift()
|
||||||
|
, loggingEvent = new LoggingEvent(category, logLevel, args);
|
||||||
|
debug("Logging event " + loggingEvent + " to dispatch = " + util.inspect(dispatch));
|
||||||
|
dispatch(loggingEvent);
|
||||||
|
}
|
||||||
|
|
||||||
|
var self = this;
|
||||||
|
['trace','debug','info','warn','error','fatal'].forEach(
|
||||||
|
function(level) {
|
||||||
|
self[level] = function() {
|
||||||
|
var args = Array.prototype.slice.call(arguments);
|
||||||
|
args.unshift(level);
|
||||||
|
log.apply(this, args);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Models a logging event.
|
* Models a logging event.
|
||||||
* @constructor
|
* @constructor
|
||||||
* @param {String} categoryName name of category
|
* @param {String} category name of category
|
||||||
* @param {Log4js.Level} level level of message
|
* @param {Log4js.Level} level level of message
|
||||||
* @param {Array} data objects to log
|
* @param {Array} data objects to log
|
||||||
* @author Seth Chisamore
|
* @author Seth Chisamore
|
||||||
*/
|
*/
|
||||||
function LoggingEvent (categoryName, level, data) {
|
function LoggingEvent (category, level, data) {
|
||||||
this.startTime = new Date();
|
this.startTime = new Date();
|
||||||
this.categoryName = categoryName;
|
this.category = category;
|
||||||
this.data = data;
|
this.data = data;
|
||||||
this.level = level;
|
this.level = level;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Logger to log messages.
|
|
||||||
* use {@see Log4js#getLogger(String)} to get an instance.
|
|
||||||
* @constructor
|
|
||||||
* @param name name of category to log to
|
|
||||||
* @author Stephan Strittmatter
|
|
||||||
*/
|
|
||||||
function Logger (name, level, dispatch) {
|
|
||||||
this.category = name || DEFAULT_CATEGORY;
|
|
||||||
|
|
||||||
if (level) {
|
|
||||||
this.setLevel(level);
|
|
||||||
}
|
|
||||||
|
|
||||||
this.dispatch = dispatch;
|
|
||||||
}
|
|
||||||
Logger.DEFAULT_CATEGORY = DEFAULT_CATEGORY;
|
|
||||||
Logger.prototype.level = levels.TRACE;
|
|
||||||
|
|
||||||
Logger.prototype.setLevel = function(level) {
|
|
||||||
debug("setting level to " + level);
|
|
||||||
this.level = levels.toLevel(level, this.level || levels.TRACE);
|
|
||||||
};
|
|
||||||
|
|
||||||
Logger.prototype.removeLevel = function() {
|
|
||||||
delete this.level;
|
|
||||||
};
|
|
||||||
|
|
||||||
Logger.prototype.log = function() {
|
|
||||||
var args = Array.prototype.slice.call(arguments)
|
|
||||||
, logLevel = args.shift()
|
|
||||||
, loggingEvent = new LoggingEvent(this.category, logLevel, args);
|
|
||||||
debug("Logging event " + loggingEvent + " to dispatch = " + util.inspect(this.dispatch));
|
|
||||||
this.dispatch(loggingEvent);
|
|
||||||
};
|
|
||||||
|
|
||||||
Logger.prototype.isLevelEnabled = function(otherLevel) {
|
|
||||||
return this.level.isLessThanOrEqualTo(otherLevel);
|
|
||||||
};
|
|
||||||
|
|
||||||
['Trace','Debug','Info','Warn','Error','Fatal'].forEach(
|
|
||||||
function(levelString) {
|
|
||||||
var level = levels.toLevel(levelString);
|
|
||||||
Logger.prototype['is'+levelString+'Enabled'] = function() {
|
|
||||||
return this.isLevelEnabled(level);
|
|
||||||
};
|
|
||||||
|
|
||||||
Logger.prototype[levelString.toLowerCase()] = function () {
|
|
||||||
if (this.isLevelEnabled(level)) {
|
|
||||||
var args = Array.prototype.slice.call(arguments);
|
|
||||||
args.unshift(level);
|
|
||||||
Logger.prototype.log.apply(this, args);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
exports.LoggingEvent = LoggingEvent;
|
|
||||||
exports.Logger = Logger;
|
|
||||||
|
|||||||
+3
-1
@@ -38,6 +38,8 @@
|
|||||||
"vows": "0.7.0",
|
"vows": "0.7.0",
|
||||||
"sandboxed-module": "0.1.3",
|
"sandboxed-module": "0.1.3",
|
||||||
"hook.io": "0.8.10",
|
"hook.io": "0.8.10",
|
||||||
"underscore": "1.2.1"
|
"underscore": "1.2.1",
|
||||||
|
"mocha": "~1.12.0",
|
||||||
|
"should": "~1.2.2"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+46
-66
@@ -1,72 +1,52 @@
|
|||||||
"use strict";
|
"use strict";
|
||||||
var vows = require('vows')
|
var should = require('should')
|
||||||
, assert = require('assert')
|
, Logger = require('../lib/logger');
|
||||||
, levels = require('../lib/levels')
|
|
||||||
, Logger = require('../lib/logger').Logger
|
|
||||||
, log4js = require('../lib/log4js');
|
|
||||||
|
|
||||||
vows.describe('../lib/logger').addBatch({
|
describe('../lib/logger', function() {
|
||||||
'constructor with no parameters': {
|
describe('Logger constructor', function() {
|
||||||
topic: new Logger(),
|
it('must be passed a dispatch delegate and a category', function() {
|
||||||
'should use default category': function(logger) {
|
(function() { new Logger(); }).should.throw(
|
||||||
assert.equal(logger.category, Logger.DEFAULT_CATEGORY);
|
"Logger must have a dispatch delegate."
|
||||||
},
|
);
|
||||||
'should use TRACE log level': function(logger) {
|
(function() { new Logger(function() {}); }).should.throw(
|
||||||
assert.equal(logger.level, levels.TRACE);
|
"Logger must have a category."
|
||||||
}
|
);
|
||||||
},
|
});
|
||||||
|
|
||||||
'constructor with category': {
|
});
|
||||||
topic: new Logger('cheese'),
|
|
||||||
'should use category': function(logger) {
|
|
||||||
assert.equal(logger.category, 'cheese');
|
|
||||||
},
|
|
||||||
'should use TRACE log level': function(logger) {
|
|
||||||
assert.equal(logger.level, levels.TRACE);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
|
|
||||||
'constructor with category and level': {
|
describe('Logger instance', function() {
|
||||||
topic: new Logger('cheese', 'debug'),
|
var event
|
||||||
'should use category': function(logger) {
|
, logger = new Logger(
|
||||||
assert.equal(logger.category, 'cheese');
|
function(evt) { event = evt; },
|
||||||
},
|
"exciting category"
|
||||||
'should use level': function(logger) {
|
);
|
||||||
assert.equal(logger.level, levels.DEBUG);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
|
|
||||||
'isLevelEnabled': {
|
beforeEach(function() {
|
||||||
topic: new Logger('cheese', 'info'),
|
event = null;
|
||||||
'should provide a level enabled function for all levels': function(logger) {
|
});
|
||||||
assert.isFunction(logger.isTraceEnabled);
|
|
||||||
assert.isFunction(logger.isDebugEnabled);
|
it('should be immutable', function() {
|
||||||
assert.isFunction(logger.isInfoEnabled);
|
logger.category = "rubbish";
|
||||||
assert.isFunction(logger.isWarnEnabled);
|
logger.debug("thing");
|
||||||
assert.isFunction(logger.isErrorEnabled);
|
|
||||||
assert.isFunction(logger.isFatalEnabled);
|
event.category.should.equal("exciting category");
|
||||||
},
|
});
|
||||||
'should return the right values': function(logger) {
|
|
||||||
assert.isFalse(logger.isTraceEnabled());
|
['trace', 'debug', 'info', 'warn', 'error', 'fatal'].forEach(function(level) {
|
||||||
assert.isFalse(logger.isDebugEnabled());
|
it('should have a ' + level + ' function', function() {
|
||||||
assert.isTrue(logger.isInfoEnabled());
|
logger[level].should.be.a('function');
|
||||||
assert.isTrue(logger.isWarnEnabled());
|
});
|
||||||
assert.isTrue(logger.isErrorEnabled());
|
});
|
||||||
assert.isTrue(logger.isFatalEnabled());
|
|
||||||
}
|
it('should send log events to the dispatch delegate', function() {
|
||||||
},
|
logger.debug("interesting thing");
|
||||||
|
event.should.have.property('category').equal('exciting category');
|
||||||
|
event.should.have.property('level').equal('debug');
|
||||||
|
event.should.have.property('data').eql(["interesting thing"]);
|
||||||
|
event.should.have.property('startTime');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
'log': {
|
|
||||||
topic: function() {
|
|
||||||
var evt
|
|
||||||
, logger = new Logger('testing', null, function(event) { evt = event; });
|
|
||||||
logger.log(levels.DEBUG, "cheese");
|
|
||||||
return evt;
|
|
||||||
},
|
|
||||||
'should send log events to log4js': function(evt) {
|
|
||||||
assert.equal(evt.categoryName, 'testing');
|
|
||||||
assert.equal(evt.level, levels.DEBUG);
|
|
||||||
assert.equal(evt.data[0], "cheese");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}).exportTo(module);
|
|
||||||
|
|||||||
Reference in New Issue
Block a user