log4js-node/lib/logger.js

50 lines
1.2 KiB
JavaScript
Raw Normal View History

2013-05-30 06:26:26 +08:00
"use strict";
var debug = require('./debug')('logger')
, util = require('util');
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.
* @constructor
* @param {String} category name of category
* @param {Log4js.Level} level level of message
* @param {Array} data objects to log
* @author Seth Chisamore
*/
function LoggingEvent (category, level, data) {
2013-05-30 06:26:26 +08:00
this.startTime = new Date();
this.category = category;
2013-05-30 06:26:26 +08:00
this.data = data;
this.level = level;
}