Added loading of config from require paths, and now defaults to console appender with basic layout
This commit is contained in:
@@ -44,10 +44,13 @@
|
||||
* @static
|
||||
* Website: http://log4js.berlios.de
|
||||
*/
|
||||
module.exports = function (fileSystem) {
|
||||
var fs = fileSystem || require('fs'),
|
||||
module.exports = function (fileSystem, standardOutput, configPaths) {
|
||||
var fs = fileSystem || require('fs'),
|
||||
standardOutput = standardOutput || console.log,
|
||||
configPaths = configPaths || require.paths,
|
||||
sys = require('sys'),
|
||||
events = require('events'),
|
||||
path = require('path'),
|
||||
DEFAULT_CATEGORY = '[default]',
|
||||
ALL_CATEGORIES = '[all]',
|
||||
loggers = {},
|
||||
@@ -91,8 +94,6 @@ module.exports = function (fileSystem) {
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Get a logger instance. Instance is cached on categoryName level.
|
||||
* @param {String} categoryName name of category to log to.
|
||||
@@ -133,7 +134,7 @@ module.exports = function (fileSystem) {
|
||||
function addAppender () {
|
||||
var args = Array.prototype.slice.call(arguments);
|
||||
var appender = args.shift();
|
||||
if (args.length == 0) {
|
||||
if (args.length == 0 || args[0] === undefined) {
|
||||
args = [ ALL_CATEGORIES ];
|
||||
}
|
||||
//argument may already be an array
|
||||
@@ -169,9 +170,34 @@ module.exports = function (fileSystem) {
|
||||
}
|
||||
|
||||
function configure (configurationFile) {
|
||||
var config = JSON.parse(fs.readFileSync(configurationFile));
|
||||
configureAppenders(config.appenders);
|
||||
configureLevels(config.levels);
|
||||
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) {
|
||||
@@ -310,7 +336,7 @@ module.exports = function (fileSystem) {
|
||||
function consoleAppender (layout) {
|
||||
layout = layout || basicLayout;
|
||||
return function(loggingEvent) {
|
||||
console.log(layout(loggingEvent));
|
||||
standardOutput(layout(loggingEvent));
|
||||
};
|
||||
}
|
||||
|
||||
@@ -488,6 +514,9 @@ module.exports = function (fileSystem) {
|
||||
|
||||
};
|
||||
|
||||
//set ourselves up if we can find a default log4js.json
|
||||
configure(findConfiguration());
|
||||
|
||||
return {
|
||||
getLogger: getLogger,
|
||||
getDefaultLogger: getDefaultLogger,
|
||||
|
||||
10
lib/log4js.json
Normal file
10
lib/log4js.json
Normal file
@@ -0,0 +1,10 @@
|
||||
{
|
||||
"appenders": [
|
||||
{
|
||||
"type": "console",
|
||||
"layout": {
|
||||
"type": "basic"
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
Reference in New Issue
Block a user