2013-05-30 06:26:26 +08:00
|
|
|
"use strict";
|
|
|
|
var levels = require('./levels')
|
|
|
|
, util = require('util')
|
|
|
|
, events = require('events')
|
|
|
|
, DEFAULT_CATEGORY = '[default]';
|
2012-06-01 09:11:07 +08:00
|
|
|
|
2014-04-08 10:06:29 +08:00
|
|
|
var logWritesEnabled = true;
|
|
|
|
|
2012-06-01 09:11:07 +08:00
|
|
|
/**
|
|
|
|
* Models a logging event.
|
|
|
|
* @constructor
|
|
|
|
* @param {String} categoryName name of category
|
|
|
|
* @param {Log4js.Level} level level of message
|
|
|
|
* @param {Array} data objects to log
|
|
|
|
* @param {Log4js.Logger} logger the associated logger
|
|
|
|
* @author Seth Chisamore
|
|
|
|
*/
|
|
|
|
function LoggingEvent (categoryName, level, data, logger) {
|
2013-05-30 06:26:26 +08:00
|
|
|
this.startTime = new Date();
|
|
|
|
this.categoryName = categoryName;
|
|
|
|
this.data = data;
|
|
|
|
this.level = level;
|
|
|
|
this.logger = logger;
|
2012-06-01 09:11:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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) {
|
2013-05-30 06:26:26 +08:00
|
|
|
this.category = name || DEFAULT_CATEGORY;
|
|
|
|
|
|
|
|
if (level) {
|
|
|
|
this.setLevel(level);
|
|
|
|
}
|
2012-06-01 09:11:07 +08:00
|
|
|
}
|
|
|
|
util.inherits(Logger, events.EventEmitter);
|
|
|
|
Logger.DEFAULT_CATEGORY = DEFAULT_CATEGORY;
|
2013-05-30 06:26:26 +08:00
|
|
|
Logger.prototype.level = levels.TRACE;
|
2012-06-01 09:11:07 +08:00
|
|
|
|
|
|
|
Logger.prototype.setLevel = function(level) {
|
2013-05-30 06:26:26 +08:00
|
|
|
this.level = levels.toLevel(level, this.level || levels.TRACE);
|
2012-06-01 09:11:07 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
Logger.prototype.removeLevel = function() {
|
2013-05-30 06:26:26 +08:00
|
|
|
delete this.level;
|
2012-06-01 09:11:07 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
Logger.prototype.log = function() {
|
2013-05-30 06:26:26 +08:00
|
|
|
var args = Array.prototype.slice.call(arguments)
|
2014-03-04 23:08:27 +08:00
|
|
|
, logLevel = levels.toLevel(args.shift())
|
|
|
|
, loggingEvent;
|
|
|
|
if (this.isLevelEnabled(logLevel)) {
|
|
|
|
loggingEvent = new LoggingEvent(this.category, logLevel, args, this);
|
|
|
|
this.emit("log", loggingEvent);
|
|
|
|
}
|
2012-06-01 09:11:07 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
Logger.prototype.isLevelEnabled = function(otherLevel) {
|
2013-05-30 06:26:26 +08:00
|
|
|
return this.level.isLessThanOrEqualTo(otherLevel);
|
2012-06-01 09:11:07 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
['Trace','Debug','Info','Warn','Error','Fatal'].forEach(
|
2013-05-30 06:26:26 +08:00
|
|
|
function(levelString) {
|
|
|
|
var level = levels.toLevel(levelString);
|
|
|
|
Logger.prototype['is'+levelString+'Enabled'] = function() {
|
|
|
|
return this.isLevelEnabled(level);
|
|
|
|
};
|
|
|
|
|
|
|
|
Logger.prototype[levelString.toLowerCase()] = function () {
|
2014-04-08 10:06:29 +08:00
|
|
|
if (logWritesEnabled && this.isLevelEnabled(level)) {
|
2013-05-30 06:26:26 +08:00
|
|
|
var args = Array.prototype.slice.call(arguments);
|
|
|
|
args.unshift(level);
|
|
|
|
Logger.prototype.log.apply(this, args);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2012-06-01 09:11:07 +08:00
|
|
|
);
|
|
|
|
|
2014-04-08 10:06:29 +08:00
|
|
|
/**
|
|
|
|
* Disable all log writes.
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
function disableAllLogWrites() {
|
|
|
|
logWritesEnabled = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Enable log writes.
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
function enableAllLogWrites() {
|
|
|
|
logWritesEnabled = true;
|
|
|
|
}
|
2012-06-01 09:11:07 +08:00
|
|
|
|
|
|
|
exports.LoggingEvent = LoggingEvent;
|
|
|
|
exports.Logger = Logger;
|
2014-04-08 10:06:29 +08:00
|
|
|
exports.disableAllLogWrites = disableAllLogWrites;
|
|
|
|
exports.enableAllLogWrites = enableAllLogWrites;
|