Persist logging config across invocations
This commit is contained in:
parent
cf7d5f681a
commit
c6dd2398ab
371
lib/log4js.js
371
lib/log4js.js
@ -44,18 +44,14 @@
|
|||||||
* @static
|
* @static
|
||||||
* Website: http://log4js.berlios.de
|
* Website: http://log4js.berlios.de
|
||||||
*/
|
*/
|
||||||
module.exports = function (fileSystem, standardOutput, configPaths) {
|
var events = require('events'),
|
||||||
var events = require('events'),
|
path = require('path'),
|
||||||
path = require('path'),
|
sys = require('sys'),
|
||||||
sys = require('sys'),
|
DEFAULT_CATEGORY = '[default]',
|
||||||
fs = fileSystem || require('fs'),
|
ALL_CATEGORIES = '[all]',
|
||||||
standardOutput = standardOutput || sys.puts,
|
appenders = {},
|
||||||
configPaths = configPaths || require.paths,
|
loggers = {},
|
||||||
DEFAULT_CATEGORY = '[default]',
|
levels = {
|
||||||
ALL_CATEGORIES = '[all]',
|
|
||||||
loggers = {},
|
|
||||||
appenders = {},
|
|
||||||
levels = {
|
|
||||||
ALL: new Level(Number.MIN_VALUE, "ALL", "grey"),
|
ALL: new Level(Number.MIN_VALUE, "ALL", "grey"),
|
||||||
TRACE: new Level(5000, "TRACE", "blue"),
|
TRACE: new Level(5000, "TRACE", "blue"),
|
||||||
DEBUG: new Level(10000, "DEBUG", "cyan"),
|
DEBUG: new Level(10000, "DEBUG", "cyan"),
|
||||||
@ -64,43 +60,43 @@ module.exports = function (fileSystem, standardOutput, configPaths) {
|
|||||||
ERROR: new Level(40000, "ERROR", "red"),
|
ERROR: new Level(40000, "ERROR", "red"),
|
||||||
FATAL: new Level(50000, "FATAL", "magenta"),
|
FATAL: new Level(50000, "FATAL", "magenta"),
|
||||||
OFF: new Level(Number.MAX_VALUE, "OFF", "grey")
|
OFF: new Level(Number.MAX_VALUE, "OFF", "grey")
|
||||||
},
|
},
|
||||||
appenderMakers = {
|
appenderMakers = {
|
||||||
"file": function(config) {
|
"file": function(config, fileAppender) {
|
||||||
var layout;
|
var layout;
|
||||||
if (config.layout) {
|
if (config.layout) {
|
||||||
layout = layoutMakers[config.layout.type](config.layout);
|
layout = layoutMakers[config.layout.type](config.layout);
|
||||||
}
|
}
|
||||||
return fileAppender(config.filename, layout, config.maxLogSize, config.backups, config.pollInterval);
|
return fileAppender(config.filename, layout, config.maxLogSize, config.backups, config.pollInterval);
|
||||||
},
|
},
|
||||||
"console": function(config) {
|
"console": function(config, fileAppender, consoleAppender) {
|
||||||
var layout;
|
var layout;
|
||||||
if (config.layout) {
|
if (config.layout) {
|
||||||
layout = layoutMakers[config.layout.type](config.layout);
|
layout = layoutMakers[config.layout.type](config.layout);
|
||||||
}
|
}
|
||||||
return consoleAppender(layout);
|
return consoleAppender(layout);
|
||||||
},
|
},
|
||||||
"logLevelFilter": function(config) {
|
"logLevelFilter": function(config, fileAppender, consoleAppender) {
|
||||||
var appender = appenderMakers[config.appender.type](config.appender);
|
var appender = appenderMakers[config.appender.type](config.appender, fileAppender, consoleAppender);
|
||||||
return logLevelFilter(config.level, appender);
|
return logLevelFilter(config.level, appender);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
layoutMakers = {
|
layoutMakers = {
|
||||||
"messagePassThrough": function() { return messagePassThroughLayout; },
|
"messagePassThrough": function() { return messagePassThroughLayout; },
|
||||||
"basic": function() { return basicLayout; },
|
"basic": function() { return basicLayout; },
|
||||||
"pattern": function (config) {
|
"pattern": function (config) {
|
||||||
var pattern = config.pattern || undefined;
|
var pattern = config.pattern || undefined;
|
||||||
return patternLayout(pattern);
|
return patternLayout(pattern);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get a logger instance. Instance is cached on categoryName level.
|
* Get a logger instance. Instance is cached on categoryName level.
|
||||||
* @param {String} categoryName name of category to log to.
|
* @param {String} categoryName name of category to log to.
|
||||||
* @return {Logger} instance of logger for the category
|
* @return {Logger} instance of logger for the category
|
||||||
* @static
|
* @static
|
||||||
*/
|
*/
|
||||||
function getLogger (categoryName) {
|
function getLogger (categoryName) {
|
||||||
|
|
||||||
// Use default logger if categoryName is not specified or invalid
|
// Use default logger if categoryName is not specified or invalid
|
||||||
if (!(typeof categoryName == "string")) {
|
if (!(typeof categoryName == "string")) {
|
||||||
@ -126,12 +122,12 @@ module.exports = function (fileSystem, standardOutput, configPaths) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return loggers[categoryName];
|
return loggers[categoryName];
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* args are appender, then zero or more categories
|
* args are appender, then zero or more categories
|
||||||
*/
|
*/
|
||||||
function addAppender () {
|
function addAppender () {
|
||||||
var args = Array.prototype.slice.call(arguments);
|
var args = Array.prototype.slice.call(arguments);
|
||||||
var appender = args.shift();
|
var appender = args.shift();
|
||||||
if (args.length == 0 || args[0] === undefined) {
|
if (args.length == 0 || args[0] === undefined) {
|
||||||
@ -158,53 +154,23 @@ module.exports = function (fileSystem, standardOutput, configPaths) {
|
|||||||
loggers[category].addListener("log", appender);
|
loggers[category].addListener("log", appender);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
appenders.count = appenders.count ? appenders.count++ : 1;
|
||||||
|
}
|
||||||
|
|
||||||
function clearAppenders () {
|
function clearAppenders () {
|
||||||
appenders = [];
|
appenders = {};
|
||||||
for (var logger in loggers) {
|
for (var logger in loggers) {
|
||||||
if (loggers.hasOwnProperty(logger)) {
|
if (loggers.hasOwnProperty(logger)) {
|
||||||
loggers[logger].removeAllListeners("log");
|
loggers[logger].removeAllListeners("log");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function configure (configurationFile) {
|
function configureAppenders(appenderList, fileAppender, consoleAppender) {
|
||||||
if (configurationFile) {
|
|
||||||
try {
|
|
||||||
var config = JSON.parse(fs.readFileSync(configurationFile, "utf8"));
|
|
||||||
configureAppenders(config.appenders);
|
|
||||||
configureLevels(config.levels);
|
|
||||||
} catch (e) {
|
|
||||||
throw new Error("Problem reading log4js config file " + configurationFile + ". Error was " + e.message);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function findConfiguration() {
|
|
||||||
//add current directory onto the list of configPaths
|
|
||||||
var paths = ['.'].concat(configPaths);
|
|
||||||
//add this module's directory to the end of the list, so that we pick up the default config
|
|
||||||
paths.push(__dirname);
|
|
||||||
var pathsWithConfig = paths.filter( function (pathToCheck) {
|
|
||||||
try {
|
|
||||||
fs.statSync(path.join(pathToCheck, "log4js.json"));
|
|
||||||
return true;
|
|
||||||
} catch (e) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
if (pathsWithConfig.length > 0) {
|
|
||||||
return path.join(pathsWithConfig[0], 'log4js.json');
|
|
||||||
}
|
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
|
|
||||||
function configureAppenders(appenderList) {
|
|
||||||
clearAppenders();
|
clearAppenders();
|
||||||
if (appenderList) {
|
if (appenderList) {
|
||||||
appenderList.forEach(function(appenderConfig) {
|
appenderList.forEach(function(appenderConfig) {
|
||||||
var appender = appenderMakers[appenderConfig.type](appenderConfig);
|
var appender = appenderMakers[appenderConfig.type](appenderConfig, fileAppender, consoleAppender);
|
||||||
if (appender) {
|
if (appender) {
|
||||||
addAppender(appender, appenderConfig.category);
|
addAppender(appender, appenderConfig.category);
|
||||||
} else {
|
} else {
|
||||||
@ -214,9 +180,9 @@ module.exports = function (fileSystem, standardOutput, configPaths) {
|
|||||||
} else {
|
} else {
|
||||||
addAppender(consoleAppender);
|
addAppender(consoleAppender);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function configureLevels(levels) {
|
function configureLevels(levels) {
|
||||||
if (levels) {
|
if (levels) {
|
||||||
for (var category in levels) {
|
for (var category in levels) {
|
||||||
if (levels.hasOwnProperty(category)) {
|
if (levels.hasOwnProperty(category)) {
|
||||||
@ -224,22 +190,22 @@ module.exports = function (fileSystem, standardOutput, configPaths) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function Level(level, levelStr, colour) {
|
function Level(level, levelStr, colour) {
|
||||||
this.level = level;
|
this.level = level;
|
||||||
this.levelStr = levelStr;
|
this.levelStr = levelStr;
|
||||||
this.colour = colour;
|
this.colour = colour;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* converts given String to corresponding Level
|
* converts given String to corresponding Level
|
||||||
* @param {String} sArg String value of Level
|
* @param {String} sArg String value of Level
|
||||||
* @param {Log4js.Level} defaultLevel default Level, if no String representation
|
* @param {Log4js.Level} defaultLevel default Level, if no String representation
|
||||||
* @return Level object
|
* @return Level object
|
||||||
* @type Log4js.Level
|
* @type Log4js.Level
|
||||||
*/
|
*/
|
||||||
Level.toLevel = function(sArg, defaultLevel) {
|
Level.toLevel = function(sArg, defaultLevel) {
|
||||||
|
|
||||||
if (sArg === null) {
|
if (sArg === null) {
|
||||||
return defaultLevel;
|
return defaultLevel;
|
||||||
@ -252,21 +218,21 @@ module.exports = function (fileSystem, standardOutput, configPaths) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
return defaultLevel;
|
return defaultLevel;
|
||||||
};
|
};
|
||||||
|
|
||||||
Level.prototype.toString = function() {
|
Level.prototype.toString = function() {
|
||||||
return this.levelStr;
|
return this.levelStr;
|
||||||
};
|
};
|
||||||
|
|
||||||
Level.prototype.isLessThanOrEqualTo = function(otherLevel) {
|
Level.prototype.isLessThanOrEqualTo = function(otherLevel) {
|
||||||
return this.level <= otherLevel.level;
|
return this.level <= otherLevel.level;
|
||||||
};
|
};
|
||||||
|
|
||||||
Level.prototype.isGreaterThanOrEqualTo = function(otherLevel) {
|
Level.prototype.isGreaterThanOrEqualTo = function(otherLevel) {
|
||||||
return this.level >= otherLevel.level;
|
return this.level >= otherLevel.level;
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Models a logging event.
|
* Models a logging event.
|
||||||
* @constructor
|
* @constructor
|
||||||
* @param {String} categoryName name of category
|
* @param {String} categoryName name of category
|
||||||
@ -275,7 +241,7 @@ module.exports = function (fileSystem, standardOutput, configPaths) {
|
|||||||
* @param {Log4js.Logger} logger the associated logger
|
* @param {Log4js.Logger} logger the associated logger
|
||||||
* @author Seth Chisamore
|
* @author Seth Chisamore
|
||||||
*/
|
*/
|
||||||
function LoggingEvent (categoryName, level, message, exception, logger) {
|
function LoggingEvent (categoryName, level, message, exception, logger) {
|
||||||
this.startTime = new Date();
|
this.startTime = new Date();
|
||||||
this.categoryName = categoryName;
|
this.categoryName = categoryName;
|
||||||
this.message = message;
|
this.message = message;
|
||||||
@ -286,35 +252,35 @@ module.exports = function (fileSystem, standardOutput, configPaths) {
|
|||||||
} else if (exception) {
|
} else if (exception) {
|
||||||
this.exception = new Error(sys.inspect(exception));
|
this.exception = new Error(sys.inspect(exception));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Logger to log messages.
|
* Logger to log messages.
|
||||||
* use {@see Log4js#getLogger(String)} to get an instance.
|
* use {@see Log4js#getLogger(String)} to get an instance.
|
||||||
* @constructor
|
* @constructor
|
||||||
* @param name name of category to log to
|
* @param name name of category to log to
|
||||||
* @author Stephan Strittmatter
|
* @author Stephan Strittmatter
|
||||||
*/
|
*/
|
||||||
function Logger (name, level) {
|
function Logger (name, level) {
|
||||||
this.category = name || DEFAULT_CATEGORY;
|
this.category = name || DEFAULT_CATEGORY;
|
||||||
this.level = Level.toLevel(level, levels.TRACE);
|
this.level = Level.toLevel(level, levels.TRACE);
|
||||||
}
|
}
|
||||||
sys.inherits(Logger, events.EventEmitter);
|
sys.inherits(Logger, events.EventEmitter);
|
||||||
|
|
||||||
Logger.prototype.setLevel = function(level) {
|
Logger.prototype.setLevel = function(level) {
|
||||||
this.level = Level.toLevel(level, levels.TRACE);
|
this.level = Level.toLevel(level, levels.TRACE);
|
||||||
};
|
};
|
||||||
|
|
||||||
Logger.prototype.log = function(logLevel, message, exception) {
|
Logger.prototype.log = function(logLevel, message, exception) {
|
||||||
var loggingEvent = new LoggingEvent(this.category, logLevel, message, exception, this);
|
var loggingEvent = new LoggingEvent(this.category, logLevel, message, exception, this);
|
||||||
this.emit("log", loggingEvent);
|
this.emit("log", loggingEvent);
|
||||||
};
|
};
|
||||||
|
|
||||||
Logger.prototype.isLevelEnabled = function(otherLevel) {
|
Logger.prototype.isLevelEnabled = function(otherLevel) {
|
||||||
return this.level.isLessThanOrEqualTo(otherLevel);
|
return this.level.isLessThanOrEqualTo(otherLevel);
|
||||||
};
|
};
|
||||||
|
|
||||||
['Trace','Debug','Info','Warn','Error','Fatal'].forEach(
|
['Trace','Debug','Info','Warn','Error','Fatal'].forEach(
|
||||||
function(levelString) {
|
function(levelString) {
|
||||||
var level = Level.toLevel(levelString);
|
var level = Level.toLevel(levelString);
|
||||||
Logger.prototype['is'+levelString+'Enabled'] = function() {
|
Logger.prototype['is'+levelString+'Enabled'] = function() {
|
||||||
@ -327,99 +293,27 @@ module.exports = function (fileSystem, standardOutput, configPaths) {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the default logger instance.
|
* Get the default logger instance.
|
||||||
* @return {Logger} instance of default logger
|
* @return {Logger} instance of default logger
|
||||||
* @static
|
* @static
|
||||||
*/
|
*/
|
||||||
function getDefaultLogger () {
|
function getDefaultLogger () {
|
||||||
return getLogger(DEFAULT_CATEGORY);
|
return getLogger(DEFAULT_CATEGORY);
|
||||||
}
|
}
|
||||||
|
|
||||||
function consoleAppender (layout) {
|
function logLevelFilter (levelString, appender) {
|
||||||
layout = layout || colouredLayout;
|
|
||||||
return function(loggingEvent) {
|
|
||||||
standardOutput(layout(loggingEvent));
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* File Appender writing the logs to a text file. Supports rolling of logs by size.
|
|
||||||
*
|
|
||||||
* @param file file log messages will be written to
|
|
||||||
* @param layout a function that takes a logevent and returns a string (defaults to basicLayout).
|
|
||||||
* @param logSize - the maximum size (in bytes) for a log file, if not provided then logs won't be rotated.
|
|
||||||
* @param numBackups - the number of log files to keep after logSize has been reached (default 5)
|
|
||||||
* @param filePollInterval - the time in seconds between file size checks (default 30s)
|
|
||||||
*/
|
|
||||||
function fileAppender (file, layout, logSize, numBackups, filePollInterval) {
|
|
||||||
layout = layout || basicLayout;
|
|
||||||
//syncs are generally bad, but we need
|
|
||||||
//the file to be open before we start doing any writing.
|
|
||||||
var logFile = fs.openSync(file, 'a', 0644);
|
|
||||||
|
|
||||||
if (logSize > 0) {
|
|
||||||
setupLogRolling(logFile, file, logSize, numBackups || 5, (filePollInterval * 1000) || 30000);
|
|
||||||
}
|
|
||||||
|
|
||||||
return function(loggingEvent) {
|
|
||||||
fs.write(logFile, layout(loggingEvent)+'\n', null, "utf8");
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
function setupLogRolling (logFile, filename, logSize, numBackups, filePollInterval) {
|
|
||||||
fs.watchFile(filename,
|
|
||||||
{
|
|
||||||
persistent: false,
|
|
||||||
interval: filePollInterval
|
|
||||||
},
|
|
||||||
function (curr, prev) {
|
|
||||||
if (curr.size >= logSize) {
|
|
||||||
rollThatLog(logFile, filename, numBackups);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
function rollThatLog (logFile, filename, numBackups) {
|
|
||||||
//doing all of this fs stuff sync, because I don't want to lose any log events.
|
|
||||||
//first close the current one.
|
|
||||||
fs.closeSync(logFile);
|
|
||||||
//roll the backups (rename file.n-1 to file.n, where n <= numBackups)
|
|
||||||
for (var i=numBackups; i > 0; i--) {
|
|
||||||
if (i > 1) {
|
|
||||||
if (fileExists(filename + '.' + (i-1))) {
|
|
||||||
fs.renameSync(filename+'.'+(i-1), filename+'.'+i);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
fs.renameSync(filename, filename+'.1');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
//open it up again
|
|
||||||
logFile = fs.openSync(filename, 'a', 0644);
|
|
||||||
}
|
|
||||||
|
|
||||||
function fileExists (filename) {
|
|
||||||
try {
|
|
||||||
fs.statSync(filename);
|
|
||||||
return true;
|
|
||||||
} catch (e) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function logLevelFilter (levelString, appender) {
|
|
||||||
var level = Level.toLevel(levelString);
|
var level = Level.toLevel(levelString);
|
||||||
return function(logEvent) {
|
return function(logEvent) {
|
||||||
if (logEvent.level.isGreaterThanOrEqualTo(level)) {
|
if (logEvent.level.isGreaterThanOrEqualTo(level)) {
|
||||||
appender(logEvent);
|
appender(logEvent);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* BasicLayout is a simple layout for storing the logs. The logs are stored
|
* BasicLayout is a simple layout for storing the logs. The logs are stored
|
||||||
* in following format:
|
* in following format:
|
||||||
* <pre>
|
* <pre>
|
||||||
@ -428,7 +322,7 @@ module.exports = function (fileSystem, standardOutput, configPaths) {
|
|||||||
*
|
*
|
||||||
* @author Stephan Strittmatter
|
* @author Stephan Strittmatter
|
||||||
*/
|
*/
|
||||||
function basicLayout (loggingEvent) {
|
function basicLayout (loggingEvent) {
|
||||||
var timestampLevelAndCategory = '[' + loggingEvent.startTime.toFormattedString() + '] ';
|
var timestampLevelAndCategory = '[' + loggingEvent.startTime.toFormattedString() + '] ';
|
||||||
timestampLevelAndCategory += '[' + loggingEvent.level.toString() + '] ';
|
timestampLevelAndCategory += '[' + loggingEvent.level.toString() + '] ';
|
||||||
timestampLevelAndCategory += loggingEvent.categoryName + ' - ';
|
timestampLevelAndCategory += loggingEvent.categoryName + ' - ';
|
||||||
@ -445,12 +339,12 @@ module.exports = function (fileSystem, standardOutput, configPaths) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
return output;
|
return output;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Taken from masylum's fork (https://github.com/masylum/log4js-node)
|
* Taken from masylum's fork (https://github.com/masylum/log4js-node)
|
||||||
*/
|
*/
|
||||||
function colorize (str, style) {
|
function colorize (str, style) {
|
||||||
var styles = {
|
var styles = {
|
||||||
//styles
|
//styles
|
||||||
'bold' : [1, 22],
|
'bold' : [1, 22],
|
||||||
@ -471,13 +365,13 @@ module.exports = function (fileSystem, standardOutput, configPaths) {
|
|||||||
};
|
};
|
||||||
return '\033[' + styles[style][0] + 'm' + str +
|
return '\033[' + styles[style][0] + 'm' + str +
|
||||||
'\033[' + styles[style][1] + 'm';
|
'\033[' + styles[style][1] + 'm';
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* colouredLayout - taken from masylum's fork.
|
* colouredLayout - taken from masylum's fork.
|
||||||
* same as basicLayout, but with colours.
|
* same as basicLayout, but with colours.
|
||||||
*/
|
*/
|
||||||
function colouredLayout (loggingEvent) {
|
function colouredLayout (loggingEvent) {
|
||||||
var timestampLevelAndCategory = colorize('[' + loggingEvent.startTime.toFormattedString() + '] ', 'grey');
|
var timestampLevelAndCategory = colorize('[' + loggingEvent.startTime.toFormattedString() + '] ', 'grey');
|
||||||
timestampLevelAndCategory += colorize(
|
timestampLevelAndCategory += colorize(
|
||||||
'[' + loggingEvent.level.toString() + '] ', loggingEvent.level.colour
|
'[' + loggingEvent.level.toString() + '] ', loggingEvent.level.colour
|
||||||
@ -496,18 +390,18 @@ module.exports = function (fileSystem, standardOutput, configPaths) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
return output;
|
return output;
|
||||||
}
|
}
|
||||||
|
|
||||||
function messagePassThroughLayout (loggingEvent) {
|
function messagePassThroughLayout (loggingEvent) {
|
||||||
return loggingEvent.message;
|
return loggingEvent.message;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* PatternLayout
|
* PatternLayout
|
||||||
* Takes a pattern string and returns a layout function.
|
* Takes a pattern string and returns a layout function.
|
||||||
* @author Stephan Strittmatter
|
* @author Stephan Strittmatter
|
||||||
*/
|
*/
|
||||||
function patternLayout (pattern) {
|
function patternLayout (pattern) {
|
||||||
var TTCC_CONVERSION_PATTERN = "%r %p %c - %m%n";
|
var TTCC_CONVERSION_PATTERN = "%r %p %c - %m%n";
|
||||||
var regex = /%(-?[0-9]+)?(\.?[0-9]+)?([cdmnpr%])(\{([^\}]+)\})?|([^%]+)/;
|
var regex = /%(-?[0-9]+)?(\.?[0-9]+)?([cdmnpr%])(\{([^\}]+)\})?|([^%]+)/;
|
||||||
|
|
||||||
@ -616,7 +510,117 @@ module.exports = function (fileSystem, standardOutput, configPaths) {
|
|||||||
return formattedString;
|
return formattedString;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
module.exports = function (fileSystem, standardOutput, configPaths) {
|
||||||
|
var fs = fileSystem || require('fs'),
|
||||||
|
standardOutput = standardOutput || sys.puts,
|
||||||
|
configPaths = configPaths || require.paths;
|
||||||
|
|
||||||
|
|
||||||
|
function consoleAppender (layout) {
|
||||||
|
layout = layout || colouredLayout;
|
||||||
|
return function(loggingEvent) {
|
||||||
|
standardOutput(layout(loggingEvent));
|
||||||
};
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* File Appender writing the logs to a text file. Supports rolling of logs by size.
|
||||||
|
*
|
||||||
|
* @param file file log messages will be written to
|
||||||
|
* @param layout a function that takes a logevent and returns a string (defaults to basicLayout).
|
||||||
|
* @param logSize - the maximum size (in bytes) for a log file, if not provided then logs won't be rotated.
|
||||||
|
* @param numBackups - the number of log files to keep after logSize has been reached (default 5)
|
||||||
|
* @param filePollInterval - the time in seconds between file size checks (default 30s)
|
||||||
|
*/
|
||||||
|
function fileAppender (file, layout, logSize, numBackups, filePollInterval) {
|
||||||
|
layout = layout || basicLayout;
|
||||||
|
//syncs are generally bad, but we need
|
||||||
|
//the file to be open before we start doing any writing.
|
||||||
|
var logFile = fs.openSync(file, 'a', 0644);
|
||||||
|
|
||||||
|
if (logSize > 0) {
|
||||||
|
setupLogRolling(logFile, file, logSize, numBackups || 5, (filePollInterval * 1000) || 30000);
|
||||||
|
}
|
||||||
|
|
||||||
|
return function(loggingEvent) {
|
||||||
|
fs.write(logFile, layout(loggingEvent)+'\n', null, "utf8");
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
function setupLogRolling (logFile, filename, logSize, numBackups, filePollInterval) {
|
||||||
|
fs.watchFile(filename,
|
||||||
|
{
|
||||||
|
persistent: false,
|
||||||
|
interval: filePollInterval
|
||||||
|
},
|
||||||
|
function (curr, prev) {
|
||||||
|
if (curr.size >= logSize) {
|
||||||
|
rollThatLog(logFile, filename, numBackups);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
function rollThatLog (logFile, filename, numBackups) {
|
||||||
|
//doing all of this fs stuff sync, because I don't want to lose any log events.
|
||||||
|
//first close the current one.
|
||||||
|
fs.closeSync(logFile);
|
||||||
|
//roll the backups (rename file.n-1 to file.n, where n <= numBackups)
|
||||||
|
for (var i=numBackups; i > 0; i--) {
|
||||||
|
if (i > 1) {
|
||||||
|
if (fileExists(filename + '.' + (i-1))) {
|
||||||
|
fs.renameSync(filename+'.'+(i-1), filename+'.'+i);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
fs.renameSync(filename, filename+'.1');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//open it up again
|
||||||
|
logFile = fs.openSync(filename, 'a', 0644);
|
||||||
|
}
|
||||||
|
|
||||||
|
function fileExists (filename) {
|
||||||
|
try {
|
||||||
|
fs.statSync(filename);
|
||||||
|
return true;
|
||||||
|
} catch (e) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function configure (configurationFile) {
|
||||||
|
if (configurationFile) {
|
||||||
|
try {
|
||||||
|
var config = JSON.parse(fs.readFileSync(configurationFile, "utf8"));
|
||||||
|
configureAppenders(config.appenders, fileAppender, consoleAppender);
|
||||||
|
configureLevels(config.levels);
|
||||||
|
} catch (e) {
|
||||||
|
throw new Error("Problem reading log4js config file " + configurationFile + ". Error was \"" + e.message + "\" ("+e.stack+")");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function findConfiguration() {
|
||||||
|
//add current directory onto the list of configPaths
|
||||||
|
var paths = ['.'].concat(configPaths);
|
||||||
|
//add this module's directory to the end of the list, so that we pick up the default config
|
||||||
|
paths.push(__dirname);
|
||||||
|
var pathsWithConfig = paths.filter( function (pathToCheck) {
|
||||||
|
try {
|
||||||
|
fs.statSync(path.join(pathToCheck, "log4js.json"));
|
||||||
|
return true;
|
||||||
|
} catch (e) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
if (pathsWithConfig.length > 0) {
|
||||||
|
return path.join(pathsWithConfig[0], 'log4js.json');
|
||||||
|
}
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
|
||||||
function replaceConsole(logger) {
|
function replaceConsole(logger) {
|
||||||
function replaceWith (fn) {
|
function replaceWith (fn) {
|
||||||
@ -634,10 +638,13 @@ module.exports = function (fileSystem, standardOutput, configPaths) {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//do we already have appenders?
|
||||||
|
if (!appenders.count) {
|
||||||
//set ourselves up if we can find a default log4js.json
|
//set ourselves up if we can find a default log4js.json
|
||||||
configure(findConfiguration());
|
configure(findConfiguration());
|
||||||
//replace console.log, etc with log4js versions
|
//replace console.log, etc with log4js versions
|
||||||
replaceConsole(getLogger("console"));
|
replaceConsole(getLogger("console"));
|
||||||
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
getLogger: getLogger,
|
getLogger: getLogger,
|
||||||
|
@ -247,7 +247,9 @@ vows.describe('log4js').addBatch({
|
|||||||
|
|
||||||
'with no appenders defined' : {
|
'with no appenders defined' : {
|
||||||
topic: function() {
|
topic: function() {
|
||||||
var logger, message, log4js = require('../lib/log4js')(null, function (msg) { message = msg; } );
|
var logger, message, log4jsFn = require('../lib/log4js'), log4js;
|
||||||
|
log4jsFn().clearAppenders();
|
||||||
|
log4js = log4jsFn(null, function (msg) { message = msg; } );
|
||||||
logger = log4js.getLogger("some-logger");
|
logger = log4js.getLogger("some-logger");
|
||||||
logger.debug("This is a test");
|
logger.debug("This is a test");
|
||||||
return message;
|
return message;
|
||||||
@ -364,10 +366,15 @@ vows.describe('log4js').addBatch({
|
|||||||
},
|
},
|
||||||
fakeConsoleLog = function (msg) { message = msg; },
|
fakeConsoleLog = function (msg) { message = msg; },
|
||||||
fakeRequirePath = [ '/a/b/c', '/some/other/path', '/path/to/config', '/some/later/directory' ],
|
fakeRequirePath = [ '/a/b/c', '/some/other/path', '/path/to/config', '/some/later/directory' ],
|
||||||
log4js = require('../lib/log4js')(fakeFS, fakeConsoleLog, fakeRequirePath),
|
log4jsFn = require('../lib/log4js'),
|
||||||
|
log4js, logger;
|
||||||
|
|
||||||
|
log4jsFn().clearAppenders();
|
||||||
|
log4js = log4jsFn(fakeFS, fakeConsoleLog, fakeRequirePath);
|
||||||
logger = log4js.getLogger('a-test');
|
logger = log4js.getLogger('a-test');
|
||||||
logger.debug("this is a test");
|
logger.debug("this is a test");
|
||||||
|
|
||||||
|
|
||||||
return [ pathsChecked, message ];
|
return [ pathsChecked, message ];
|
||||||
},
|
},
|
||||||
|
|
||||||
@ -478,8 +485,8 @@ vows.describe('log4js').addBatch({
|
|||||||
topic: function() {
|
topic: function() {
|
||||||
var log4js = require('../lib/log4js')(), logEvents = [], logger;
|
var log4js = require('../lib/log4js')(), logEvents = [], logger;
|
||||||
log4js.clearAppenders();
|
log4js.clearAppenders();
|
||||||
log4js.addAppender(log4js.logLevelFilter('ERROR', function(evt) { logEvents.push(evt); }));
|
log4js.addAppender(log4js.logLevelFilter('ERROR', function(evt) { logEvents.push(evt); }), "logLevelTest");
|
||||||
logger = log4js.getLogger();
|
logger = log4js.getLogger("logLevelTest");
|
||||||
logger.debug('this should not trigger an event');
|
logger.debug('this should not trigger an event');
|
||||||
logger.warn('neither should this');
|
logger.warn('neither should this');
|
||||||
logger.error('this should, though');
|
logger.error('this should, though');
|
||||||
@ -535,6 +542,19 @@ vows.describe('log4js').addBatch({
|
|||||||
assert.equal(logEvent.message, "tracing");
|
assert.equal(logEvent.message, "tracing");
|
||||||
assert.equal(logEvent.level.toString(), "TRACE");
|
assert.equal(logEvent.level.toString(), "TRACE");
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
'configuration persistence' : {
|
||||||
|
'should maintain appenders between requires': function () {
|
||||||
|
var logEvent, firstLog4js = require('../lib/log4js')(), secondLog4js;
|
||||||
|
firstLog4js.clearAppenders();
|
||||||
|
firstLog4js.addAppender(function(evt) { logEvent = evt; });
|
||||||
|
|
||||||
|
secondLog4js = require('../lib/log4js')();
|
||||||
|
secondLog4js.getLogger().info("This should go to the appender defined in firstLog4js");
|
||||||
|
|
||||||
|
assert.equal(logEvent.message, "This should go to the appender defined in firstLog4js");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}).export(module);
|
}).export(module);
|
||||||
|
Loading…
Reference in New Issue
Block a user