2010-08-10 19:56:10 +08:00
|
|
|
/*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*jsl:option explicit*/
|
|
|
|
|
|
|
|
/**
|
2011-07-12 11:02:48 +08:00
|
|
|
* @fileoverview log4js is a library to log in JavaScript in similar manner
|
2010-08-10 19:56:10 +08:00
|
|
|
* than in log4j for Java. The API should be nearly the same.
|
2011-07-12 11:02:48 +08:00
|
|
|
*
|
2010-08-10 19:56:10 +08:00
|
|
|
* <h3>Example:</h3>
|
|
|
|
* <pre>
|
2011-07-12 11:02:48 +08:00
|
|
|
* var logging = require('log4js');
|
2010-08-10 19:56:10 +08:00
|
|
|
* //add an appender that logs all messages to stdout.
|
|
|
|
* logging.addAppender(logging.consoleAppender());
|
|
|
|
* //add an appender that logs "some-category" to a file
|
|
|
|
* logging.addAppender(logging.fileAppender("file.log"), "some-category");
|
|
|
|
* //get a logger
|
2011-07-12 11:02:48 +08:00
|
|
|
* var log = logging.getLogger("some-category");
|
2010-08-10 19:56:10 +08:00
|
|
|
* log.setLevel(logging.levels.TRACE); //set the Level
|
2011-07-12 11:02:48 +08:00
|
|
|
*
|
2010-08-10 19:56:10 +08:00
|
|
|
* ...
|
2011-07-12 11:02:48 +08:00
|
|
|
*
|
2010-08-10 19:56:10 +08:00
|
|
|
* //call the log
|
|
|
|
* log.trace("trace me" );
|
|
|
|
* </pre>
|
|
|
|
*
|
2011-07-26 06:52:40 +08:00
|
|
|
* NOTE: the authors below are the original browser-based log4js authors
|
|
|
|
* don't try to contact them about bugs in this version :)
|
2010-08-10 19:56:10 +08:00
|
|
|
* @version 1.0
|
|
|
|
* @author Stephan Strittmatter - http://jroller.com/page/stritti
|
|
|
|
* @author Seth Chisamore - http://www.chisamore.com
|
|
|
|
* @since 2005-05-20
|
|
|
|
* @static
|
|
|
|
* Website: http://log4js.berlios.de
|
|
|
|
*/
|
2011-07-12 11:02:48 +08:00
|
|
|
var events = require('events')
|
|
|
|
, fs = require('fs')
|
|
|
|
, path = require('path')
|
|
|
|
, sys = require('sys')
|
|
|
|
, layouts = require('./layouts')
|
|
|
|
, levels = require('./levels')
|
2011-07-24 19:58:02 +08:00
|
|
|
, consoleAppender = require('./appenders/console').appender
|
2011-07-12 11:02:48 +08:00
|
|
|
, DEFAULT_CATEGORY = '[default]'
|
|
|
|
, ALL_CATEGORIES = '[all]'
|
|
|
|
, appenders = {}
|
|
|
|
, loggers = {}
|
2011-07-24 19:58:02 +08:00
|
|
|
, appenderMakers = {};
|
2010-08-10 19:56:10 +08:00
|
|
|
|
2011-01-16 10:05:13 +08:00
|
|
|
/**
|
|
|
|
* Get a logger instance. Instance is cached on categoryName level.
|
|
|
|
* @param {String} categoryName name of category to log to.
|
|
|
|
* @return {Logger} instance of logger for the category
|
|
|
|
* @static
|
|
|
|
*/
|
|
|
|
function getLogger (categoryName) {
|
2011-07-12 11:02:48 +08:00
|
|
|
|
2011-01-16 10:05:13 +08:00
|
|
|
// Use default logger if categoryName is not specified or invalid
|
|
|
|
if (!(typeof categoryName == "string")) {
|
2011-07-22 12:43:33 +08:00
|
|
|
categoryName = DEFAULT_CATEGORY;
|
2011-01-16 10:05:13 +08:00
|
|
|
}
|
2010-08-10 19:56:10 +08:00
|
|
|
|
2011-01-16 10:05:13 +08:00
|
|
|
var appenderList;
|
|
|
|
if (!loggers[categoryName]) {
|
2011-07-22 12:43:33 +08:00
|
|
|
// Create the logger for this name if it doesn't already exist
|
|
|
|
loggers[categoryName] = new Logger(categoryName);
|
|
|
|
if (appenders[categoryName]) {
|
|
|
|
appenderList = appenders[categoryName];
|
|
|
|
appenderList.forEach(function(appender) {
|
|
|
|
loggers[categoryName].addListener("log", appender);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if (appenders[ALL_CATEGORIES]) {
|
|
|
|
appenderList = appenders[ALL_CATEGORIES];
|
|
|
|
appenderList.forEach(function(appender) {
|
|
|
|
loggers[categoryName].addListener("log", appender);
|
|
|
|
});
|
|
|
|
}
|
2010-08-10 19:56:10 +08:00
|
|
|
}
|
2011-07-12 11:02:48 +08:00
|
|
|
|
2011-01-16 10:05:13 +08:00
|
|
|
return loggers[categoryName];
|
|
|
|
}
|
2010-08-10 19:56:10 +08:00
|
|
|
|
2011-01-16 10:05:13 +08:00
|
|
|
/**
|
|
|
|
* args are appender, then zero or more categories
|
|
|
|
*/
|
|
|
|
function addAppender () {
|
|
|
|
var args = Array.prototype.slice.call(arguments);
|
|
|
|
var appender = args.shift();
|
|
|
|
if (args.length == 0 || args[0] === undefined) {
|
2011-07-22 12:43:33 +08:00
|
|
|
args = [ ALL_CATEGORIES ];
|
2011-01-16 10:05:13 +08:00
|
|
|
}
|
|
|
|
//argument may already be an array
|
2011-03-04 16:49:43 +08:00
|
|
|
if (Array.isArray(args[0])) {
|
2011-07-22 12:43:33 +08:00
|
|
|
args = args[0];
|
2011-01-16 10:05:13 +08:00
|
|
|
}
|
2011-07-12 11:02:48 +08:00
|
|
|
|
2011-01-16 10:05:13 +08:00
|
|
|
args.forEach(function(category) {
|
2011-07-22 12:43:33 +08:00
|
|
|
if (!appenders[category]) {
|
|
|
|
appenders[category] = [];
|
|
|
|
}
|
|
|
|
appenders[category].push(appender);
|
|
|
|
|
|
|
|
if (category === ALL_CATEGORIES) {
|
|
|
|
for (var logger in loggers) {
|
|
|
|
if (loggers.hasOwnProperty(logger)) {
|
|
|
|
loggers[logger].addListener("log", appender);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (loggers[category]) {
|
|
|
|
loggers[category].addListener("log", appender);
|
|
|
|
}
|
2011-01-16 10:05:13 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function clearAppenders () {
|
|
|
|
appenders = {};
|
|
|
|
for (var logger in loggers) {
|
2011-07-22 12:43:33 +08:00
|
|
|
if (loggers.hasOwnProperty(logger)) {
|
|
|
|
loggers[logger].removeAllListeners("log");
|
|
|
|
}
|
2011-01-16 10:05:13 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-24 19:58:02 +08:00
|
|
|
function configureAppenders(appenderList) {
|
2011-01-16 10:05:13 +08:00
|
|
|
clearAppenders();
|
|
|
|
if (appenderList) {
|
2011-07-22 12:43:33 +08:00
|
|
|
appenderList.forEach(function(appenderConfig) {
|
2011-07-26 06:52:40 +08:00
|
|
|
var appender;
|
|
|
|
appenderConfig.makers = appenderMakers;
|
2011-07-26 09:11:27 +08:00
|
|
|
appender = appenderMakers[appenderConfig.type](appenderConfig);
|
2011-07-22 12:43:33 +08:00
|
|
|
if (appender) {
|
|
|
|
addAppender(appender, appenderConfig.category);
|
|
|
|
} else {
|
|
|
|
throw new Error("log4js configuration problem for "+sys.inspect(appenderConfig));
|
|
|
|
}
|
|
|
|
});
|
2011-01-16 10:05:13 +08:00
|
|
|
} else {
|
2011-07-26 09:11:27 +08:00
|
|
|
addAppender(consoleAppender());
|
2010-08-10 19:56:10 +08:00
|
|
|
}
|
2011-01-16 10:05:13 +08:00
|
|
|
}
|
2010-08-10 19:56:10 +08:00
|
|
|
|
2011-01-16 10:05:13 +08:00
|
|
|
function configureLevels(levels) {
|
|
|
|
if (levels) {
|
2011-07-22 12:43:33 +08:00
|
|
|
for (var category in levels) {
|
|
|
|
if (levels.hasOwnProperty(category)) {
|
|
|
|
getLogger(category).setLevel(levels[category]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (l in loggers) {
|
2011-07-26 16:40:41 +08:00
|
|
|
if (loggers.hasOwnProperty(l)) {
|
|
|
|
loggers[l].setLevel();
|
|
|
|
}
|
2011-07-22 12:43:33 +08:00
|
|
|
}
|
2010-12-04 17:49:39 +08:00
|
|
|
}
|
2011-01-16 10:05:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Models a logging event.
|
|
|
|
* @constructor
|
|
|
|
* @param {String} categoryName name of category
|
|
|
|
* @param {Log4js.Level} level level of message
|
2011-07-13 16:12:29 +08:00
|
|
|
* @param {Array} data objects to log
|
2011-01-16 10:05:13 +08:00
|
|
|
* @param {Log4js.Logger} logger the associated logger
|
|
|
|
* @author Seth Chisamore
|
|
|
|
*/
|
2011-07-13 16:12:29 +08:00
|
|
|
function LoggingEvent (categoryName, level, data, logger) {
|
2011-01-16 10:05:13 +08:00
|
|
|
this.startTime = new Date();
|
|
|
|
this.categoryName = categoryName;
|
2011-07-13 16:12:29 +08:00
|
|
|
this.data = data;
|
2011-01-16 10:05:13 +08:00
|
|
|
this.level = level;
|
|
|
|
this.logger = logger;
|
|
|
|
}
|
2010-08-10 19:56:10 +08:00
|
|
|
|
2011-01-16 10:05:13 +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) {
|
|
|
|
this.category = name || DEFAULT_CATEGORY;
|
2011-05-19 21:25:13 +08:00
|
|
|
|
|
|
|
if (! this.level) {
|
2011-05-30 19:26:30 +08:00
|
|
|
this.__proto__.level = levels.TRACE;
|
2011-05-19 21:25:13 +08:00
|
|
|
}
|
2011-01-16 10:05:13 +08:00
|
|
|
}
|
|
|
|
sys.inherits(Logger, events.EventEmitter);
|
|
|
|
|
|
|
|
Logger.prototype.setLevel = function(level) {
|
2011-07-12 11:02:48 +08:00
|
|
|
this.level = levels.toLevel(level, levels.TRACE);
|
2011-01-16 10:05:13 +08:00
|
|
|
};
|
|
|
|
|
2011-05-30 19:26:30 +08:00
|
|
|
Logger.prototype.removeLevel = function() {
|
2011-05-19 21:25:13 +08:00
|
|
|
delete this.level;
|
|
|
|
};
|
|
|
|
|
2011-07-19 07:08:15 +08:00
|
|
|
Logger.prototype.log = function() {
|
|
|
|
var args = Array.prototype.slice.call(arguments)
|
|
|
|
, logLevel = args.shift()
|
|
|
|
, loggingEvent = new LoggingEvent(this.category, logLevel, args, this);
|
2011-01-16 10:05:13 +08:00
|
|
|
this.emit("log", loggingEvent);
|
|
|
|
};
|
|
|
|
|
|
|
|
Logger.prototype.isLevelEnabled = function(otherLevel) {
|
|
|
|
return this.level.isLessThanOrEqualTo(otherLevel);
|
|
|
|
};
|
|
|
|
|
|
|
|
['Trace','Debug','Info','Warn','Error','Fatal'].forEach(
|
|
|
|
function(levelString) {
|
2011-07-22 12:43:33 +08:00
|
|
|
var level = levels.toLevel(levelString);
|
|
|
|
Logger.prototype['is'+levelString+'Enabled'] = function() {
|
|
|
|
return this.isLevelEnabled(level);
|
|
|
|
};
|
2011-07-12 11:02:48 +08:00
|
|
|
|
2011-07-22 12:43:33 +08:00
|
|
|
Logger.prototype[levelString.toLowerCase()] = function () {
|
|
|
|
if (this.isLevelEnabled(level)) {
|
2011-07-19 07:08:15 +08:00
|
|
|
var args = Array.prototype.slice.call(arguments);
|
|
|
|
args.unshift(level);
|
|
|
|
Logger.prototype.log.apply(this, args);
|
2011-07-22 12:43:33 +08:00
|
|
|
}
|
|
|
|
};
|
2011-01-16 10:05:13 +08:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2011-05-30 19:26:30 +08:00
|
|
|
function setGlobalLogLevel(level) {
|
2011-07-12 11:02:48 +08:00
|
|
|
Logger.prototype.level = levels.toLevel(level, levels.TRACE);
|
2011-05-30 19:26:30 +08:00
|
|
|
}
|
|
|
|
|
2011-01-16 10:05:13 +08:00
|
|
|
/**
|
|
|
|
* Get the default logger instance.
|
|
|
|
* @return {Logger} instance of default logger
|
|
|
|
* @static
|
|
|
|
*/
|
|
|
|
function getDefaultLogger () {
|
2011-07-12 11:02:48 +08:00
|
|
|
return getLogger(DEFAULT_CATEGORY);
|
2011-01-16 10:05:13 +08:00
|
|
|
}
|
|
|
|
|
2011-07-12 11:02:48 +08:00
|
|
|
function findConfiguration() {
|
2011-09-15 06:13:04 +08:00
|
|
|
var path;
|
|
|
|
try {
|
|
|
|
path = require.resolve('log4js.json');
|
|
|
|
} catch (e) {
|
|
|
|
//file not found. default to the one in the log4js module.
|
|
|
|
path = __dirname + '/log4js.json';
|
2010-12-06 06:14:20 +08:00
|
|
|
}
|
2011-09-15 06:13:04 +08:00
|
|
|
|
|
|
|
return path;
|
2011-07-12 11:02:48 +08:00
|
|
|
}
|
2010-12-06 06:14:20 +08:00
|
|
|
|
2011-07-22 12:43:33 +08:00
|
|
|
function loadConfigurationFile(filename) {
|
|
|
|
filename = filename || findConfiguration();
|
|
|
|
if (filename) {
|
|
|
|
return JSON.parse(fs.readFileSync(filename, "utf8"));
|
|
|
|
}
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
function configureOnceOff(config) {
|
|
|
|
if (config) {
|
|
|
|
try {
|
2011-07-26 09:11:27 +08:00
|
|
|
configureAppenders(config.appenders);
|
2011-07-22 12:43:33 +08:00
|
|
|
configureLevels(config.levels);
|
|
|
|
} catch (e) {
|
|
|
|
throw new Error("Problem reading log4js config " + sys.inspect(config) + ". Error was \"" + e.message + "\" ("+e.stack+")");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var configState = {};
|
|
|
|
|
|
|
|
function reloadConfiguration() {
|
|
|
|
var filename = configState.filename || findConfiguration(),
|
|
|
|
mtime;
|
|
|
|
if (!filename) {
|
|
|
|
// can't find anything to reload
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
mtime = fs.statSync(filename).mtime;
|
|
|
|
} catch (e) {
|
|
|
|
getLogger('log4js').warn('Failed to load configuration file ' + filename);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (configState.lastFilename && configState.lastFilename === filename) {
|
|
|
|
if (mtime.getTime() > configState.lastMTime.getTime()) {
|
|
|
|
configState.lastMTime = mtime;
|
|
|
|
} else {
|
|
|
|
filename = null;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
configState.lastFilename = filename;
|
|
|
|
configState.lastMTime = mtime;
|
|
|
|
}
|
|
|
|
configureOnceOff(loadConfigurationFile(filename));
|
|
|
|
}
|
|
|
|
|
|
|
|
function initReloadConfiguration(filename, options) {
|
|
|
|
if (configState.timerId) {
|
|
|
|
clearInterval(configState.timerId);
|
|
|
|
delete configState.timerId;
|
|
|
|
}
|
|
|
|
configState.filename = filename;
|
|
|
|
configState.timerId = setInterval(reloadConfiguration, options.reloadSecs*1000);
|
|
|
|
}
|
|
|
|
|
|
|
|
function configure (configurationFileOrObject, options) {
|
|
|
|
var config = configurationFileOrObject;
|
|
|
|
if (config === undefined || config === null || typeof(config) === 'string') {
|
2011-07-27 19:21:43 +08:00
|
|
|
options = options || { };
|
2011-07-22 12:43:33 +08:00
|
|
|
if (options.reloadSecs) {
|
|
|
|
initReloadConfiguration(config, options);
|
|
|
|
}
|
|
|
|
configureOnceOff(loadConfigurationFile(config));
|
|
|
|
} else {
|
|
|
|
options = options || {};
|
|
|
|
if (options.reloadSecs) {
|
|
|
|
getLogger('log4js').warn('Ignoring configuration reload parameter for "object" configuration.');
|
|
|
|
}
|
|
|
|
configureOnceOff(config);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-12 11:02:48 +08:00
|
|
|
function replaceConsole(logger) {
|
|
|
|
function replaceWith(fn) {
|
|
|
|
return function() {
|
|
|
|
fn.apply(logger, arguments);
|
2011-01-16 10:21:37 +08:00
|
|
|
}
|
2010-12-05 11:36:35 +08:00
|
|
|
}
|
|
|
|
|
2011-07-12 11:02:48 +08:00
|
|
|
['log','debug','info','warn','error'].forEach(function (item) {
|
2011-07-26 06:52:40 +08:00
|
|
|
if (console['_preLog4js_log'] === undefined) {
|
|
|
|
console['_preLog4js_'+item] = console[item];
|
|
|
|
}
|
2011-07-12 11:02:48 +08:00
|
|
|
console[item] = replaceWith(item === 'log' ? logger.info : logger[item]);
|
|
|
|
});
|
|
|
|
}
|
2010-12-08 05:53:59 +08:00
|
|
|
|
2011-07-24 19:58:02 +08:00
|
|
|
function loadAppenders() {
|
|
|
|
var appenderList = fs.readdirSync(__dirname + '/appenders');
|
|
|
|
appenderList.forEach(function(file) {
|
2011-08-11 14:27:37 +08:00
|
|
|
if (/\.js$/.test(file)) {
|
|
|
|
var appenderModule = require('./appenders/' + file);
|
|
|
|
module.exports.appenders[appenderModule.name] = appenderModule.appender;
|
|
|
|
appenderMakers[appenderModule.name] = appenderModule.configure;
|
|
|
|
}
|
2011-07-24 19:58:02 +08:00
|
|
|
});
|
|
|
|
}
|
2010-12-08 05:53:59 +08:00
|
|
|
|
2011-07-12 11:02:48 +08:00
|
|
|
module.exports = {
|
|
|
|
getLogger: getLogger,
|
|
|
|
getDefaultLogger: getDefaultLogger,
|
2010-12-05 07:56:09 +08:00
|
|
|
|
2011-07-12 11:02:48 +08:00
|
|
|
addAppender: addAppender,
|
|
|
|
clearAppenders: clearAppenders,
|
|
|
|
configure: configure,
|
2010-08-10 19:56:10 +08:00
|
|
|
|
2011-07-12 11:02:48 +08:00
|
|
|
levels: levels,
|
|
|
|
setGlobalLogLevel: setGlobalLogLevel,
|
2010-08-10 19:56:10 +08:00
|
|
|
|
2011-07-12 11:02:48 +08:00
|
|
|
layouts: layouts,
|
2011-07-24 19:58:02 +08:00
|
|
|
appenders: {},
|
2011-07-19 07:08:15 +08:00
|
|
|
connectLogger: require('./connect-logger').connectLogger
|
2010-08-10 19:56:10 +08:00
|
|
|
};
|
|
|
|
|
2011-07-24 19:58:02 +08:00
|
|
|
loadAppenders();
|
|
|
|
//set ourselves up if we can find a default log4js.json
|
|
|
|
configure(findConfiguration());
|
|
|
|
//replace console.log, etc with log4js versions
|
|
|
|
replaceConsole(getLogger("console"));
|
|
|
|
|
2011-07-12 11:02:48 +08:00
|
|
|
//keep the old-style layouts
|
|
|
|
['basicLayout','messagePassThroughLayout','colouredLayout','coloredLayout'].forEach(function(item) {
|
|
|
|
module.exports[item] = layouts[item];
|
|
|
|
});
|
|
|
|
|
2011-07-24 19:58:02 +08:00
|
|
|
//and the old-style appenders
|
|
|
|
module.exports.consoleAppender = module.exports.appenders.console;
|
|
|
|
module.exports.fileAppender = module.exports.appenders.file;
|
|
|
|
module.exports.logLevelFilter = module.exports.appenders.logLevelFilter;
|