log4js-node/lib/logger.js

50 lines
1.2 KiB
JavaScript
Raw Permalink Normal View History

2013-05-30 06:26:26 +08:00
"use strict";
var debug = require('debug')('log4js:logger')
, levels = require('./levels');
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 = ", 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;
2013-08-21 06:05:28 +08:00
this.level = levels.toLevel(level);
}