2013-05-30 06:26:26 +08:00
|
|
|
"use strict";
|
2013-08-25 09:55:26 +08:00
|
|
|
var debug = require('debug')('log4js:logger')
|
|
|
|
, levels = require('./levels');
|
2013-08-08 06:56:09 +08:00
|
|
|
|
|
|
|
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);
|
2013-08-25 09:55:26 +08:00
|
|
|
debug("Logging event ", loggingEvent, " to dispatch = ", dispatch);
|
2013-08-08 06:56:09 +08:00
|
|
|
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);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
};
|
2012-06-01 09:11:07 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Models a logging event.
|
|
|
|
* @constructor
|
2013-08-08 06:56:09 +08:00
|
|
|
* @param {String} category name of category
|
2012-06-01 09:11:07 +08:00
|
|
|
* @param {Log4js.Level} level level of message
|
|
|
|
* @param {Array} data objects to log
|
|
|
|
* @author Seth Chisamore
|
|
|
|
*/
|
2013-08-08 06:56:09 +08:00
|
|
|
function LoggingEvent (category, level, data) {
|
2013-05-30 06:26:26 +08:00
|
|
|
this.startTime = new Date();
|
2013-08-08 06:56:09 +08:00
|
|
|
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);
|
2012-06-01 09:11:07 +08:00
|
|
|
}
|
|
|
|
|