changed the way appenders are loaded, so that they don't need to include log4js as a direct dependency

This commit is contained in:
Gareth Jones
2013-09-15 14:37:01 +10:00
parent 245b2e3b1a
commit 491c2709e7
12 changed files with 350 additions and 185 deletions
+17 -16
View File
@@ -1,20 +1,21 @@
"use strict";
var layouts = require('../layouts')
, consoleLog = console.log.bind(console);
var consoleLog = console.log.bind(console);
function consoleAppender (layout) {
layout = layout || layouts.colouredLayout;
return function(loggingEvent) {
consoleLog(layout(loggingEvent));
};
}
function configure(config) {
var layout;
if (config.layout) {
layout = layouts.layout(config.layout.type, config.layout);
module.exports = function(layouts, levels) {
function consoleAppender (layout) {
layout = layout || layouts.colouredLayout;
return function(loggingEvent) {
consoleLog(layout(loggingEvent));
};
}
return consoleAppender(layout);
}
exports.configure = configure;
return function configure(config) {
var layout;
if (config.layout) {
layout = layouts.layout(config.layout.type, config.layout);
}
return consoleAppender(layout);
};
};
+35 -36
View File
@@ -1,6 +1,5 @@
"use strict";
var streams = require('streamroller')
, layouts = require('../layouts')
, path = require('path')
, os = require('os')
, eol = os.EOL || '\n'
@@ -13,42 +12,42 @@ process.on('exit', function() {
});
});
/**
* File appender that rolls files according to a date pattern.
* @filename base filename.
* @pattern the format that will be added to the end of filename when rolling,
* also used to check when to roll files - defaults to '.yyyy-MM-dd'
* @layout layout function for log messages - defaults to basicLayout
*/
function appender(filename, pattern, alwaysIncludePattern, layout) {
layout = layout || layouts.basicLayout;
module.exports = function(layouts, levels) {
/**
* File appender that rolls files according to a date pattern.
* @filename base filename.
* @pattern the format that will be added to the end of filename when rolling,
* also used to check when to roll files - defaults to '.yyyy-MM-dd'
* @layout layout function for log messages - defaults to basicLayout
*/
function appender(filename, pattern, alwaysIncludePattern, layout) {
layout = layout || layouts.basicLayout;
var logFile = new streams.DateRollingFileStream(
filename,
pattern,
{ alwaysIncludePattern: alwaysIncludePattern }
);
openFiles.push(logFile);
return function(logEvent) {
logFile.write(layout(logEvent) + eol, "utf8");
};
var logFile = new streams.DateRollingFileStream(
filename,
pattern,
{ alwaysIncludePattern: alwaysIncludePattern }
);
openFiles.push(logFile);
}
return function configure(config) {
var layout;
return function(logEvent) {
logFile.write(layout(logEvent) + eol, "utf8");
if (config.layout) {
layout = layouts.layout(config.layout.type, config.layout);
}
if (!config.alwaysIncludePattern) {
config.alwaysIncludePattern = false;
}
return appender(config.filename, config.pattern, config.alwaysIncludePattern, layout);
};
}
function configure(config, options) {
var layout;
if (config.layout) {
layout = layouts.layout(config.layout.type, config.layout);
}
if (!config.alwaysIncludePattern) {
config.alwaysIncludePattern = false;
}
return appender(config.filename, config.pattern, config.alwaysIncludePattern, layout);
}
exports.appender = appender;
exports.configure = configure;
};
+58 -58
View File
@@ -1,6 +1,5 @@
"use strict";
var layouts = require('../layouts')
, path = require('path')
var path = require('path')
, fs = require('fs')
, streams = require('streamroller')
, os = require('os')
@@ -14,65 +13,66 @@ process.on('exit', function() {
});
});
/**
* 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)
*/
function fileAppender (file, layout, logSize, numBackups) {
var bytesWritten = 0;
file = path.normalize(file);
layout = layout || layouts.basicLayout;
numBackups = numBackups === undefined ? 5 : numBackups;
//there has to be at least one backup if logSize has been specified
numBackups = numBackups === 0 ? 1 : numBackups;
module.exports = function(layouts, levels) {
function openTheStream(file, fileSize, numFiles) {
var stream;
if (fileSize) {
stream = new streams.RollingFileStream(
file,
fileSize,
numFiles
);
} else {
stream = fs.createWriteStream(
file,
{ encoding: "utf8",
mode: parseInt('0644', 8),
flags: 'a' }
);
/**
* 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)
*/
function fileAppender (file, layout, logSize, numBackups) {
var bytesWritten = 0;
file = path.normalize(file);
layout = layout || layouts.basicLayout;
numBackups = numBackups === undefined ? 5 : numBackups;
//there has to be at least one backup if logSize has been specified
numBackups = numBackups === 0 ? 1 : numBackups;
function openTheStream(file, fileSize, numFiles) {
var stream;
if (fileSize) {
stream = new streams.RollingFileStream(
file,
fileSize,
numFiles
);
} else {
stream = fs.createWriteStream(
file,
{ encoding: "utf8",
mode: parseInt('0644', 8),
flags: 'a' }
);
}
stream.on("error", function (err) {
console.error("log4js.fileAppender - Writing to file %s, error happened ", file, err);
});
return stream;
}
stream.on("error", function (err) {
console.error("log4js.fileAppender - Writing to file %s, error happened ", file, err);
});
return stream;
var logFile = openTheStream(file, logSize, numBackups);
// push file to the stack of open handlers
openFiles.push(logFile);
return function(loggingEvent) {
logFile.write(layout(loggingEvent) + eol, "utf8");
};
}
var logFile = openTheStream(file, logSize, numBackups);
// push file to the stack of open handlers
openFiles.push(logFile);
return function(loggingEvent) {
logFile.write(layout(loggingEvent) + eol, "utf8");
return function configure(config) {
var layout;
if (config.layout) {
layout = layouts.layout(config.layout.type, config.layout);
}
return fileAppender(config.filename, layout, config.maxLogSize, config.backups);
};
}
function configure(config) {
var layout;
if (config.layout) {
layout = layouts.layout(config.layout.type, config.layout);
}
return fileAppender(config.filename, layout, config.maxLogSize, config.backups);
}
exports.appender = fileAppender;
exports.configure = configure;
};
+35 -35
View File
@@ -1,40 +1,40 @@
"use strict";
var levels = require('../levels')
, debug = require('debug')('log4js:logLevelFilter')
, log4js = require('../log4js');
var debug = require('debug')('log4js:logLevelFilter');
function logLevelFilter(allowedLevels, appender) {
return function(logEvent) {
debug("Checking ", logEvent.level, " against ", allowedLevels);
if (allowedLevels.some(function(item) { return item.level === logEvent.level.level; })) {
debug("Sending ", logEvent, " to appender ", appender);
appender(logEvent);
module.exports = function(layouts, levels) {
function logLevelFilter(allowedLevels, appender) {
return function(logEvent) {
debug("Checking ", logEvent.level, " against ", allowedLevels);
if (allowedLevels.some(function(item) { return item.level === logEvent.level.level; })) {
debug("Sending ", logEvent, " to appender ", appender);
appender(logEvent);
}
};
}
return function configure(config, appenderByName) {
if (!Array.isArray(config.allow)) {
throw new Error("No allowed log levels specified.");
}
var allowedLevels = config.allow.map(function(allowed) {
var level = levels.toLevel(allowed);
if (!level) {
throw new Error("Unrecognised log level '" + allowed + "'.");
}
return level;
});
if (allowedLevels.length === 0) {
throw new Error("No allowed log levels specified.");
}
if (!config.appender) {
throw new Error("Missing an appender.");
}
return logLevelFilter(allowedLevels, appenderByName(config.appender));
};
}
function configure(config, appenderByName) {
if (!Array.isArray(config.allow)) {
throw new Error("No allowed log levels specified.");
}
var allowedLevels = config.allow.map(function(allowed) {
var level = levels.toLevel(allowed);
if (!level) {
throw new Error("Unrecognised log level '" + allowed + "'.");
}
return level;
});
if (allowedLevels.length === 0) {
throw new Error("No allowed log levels specified.");
}
if (!config.appender) {
throw new Error("Missing an appender.");
}
return logLevelFilter(allowedLevels, appenderByName(config.appender));
}
exports.configure = configure;
};