Permalink
Browse files

Moved Logger into separate file, added support for loading appenders …

…outside log4js, removed 'name' from appender requirements
  • Loading branch information...
Gareth Jones
Gareth Jones committed Jun 1, 2012
1 parent 0ed1a13 commit a046523804638ca99ba5390b6415ff9601d7d171
View
@@ -16,6 +16,5 @@ function configure(config) {
return consoleAppender(layout);
}
-exports.name = "console";
exports.appender = consoleAppender;
exports.configure = configure;
View
@@ -63,6 +63,5 @@ function configure(config, options) {
return fileAppender(config.filename, layout, config.maxLogSize, config.backups);
}
-exports.name = "file";
exports.appender = fileAppender;
exports.configure = configure;
View
@@ -89,6 +89,5 @@ function configure(config) {
return gelfAppender(layout, config.host, config.port, config.hostname, config.facility);
}
-exports.name = "gelf";
exports.appender = gelfAppender;
exports.configure = configure;
View
@@ -71,6 +71,5 @@ function configure(config) {
return createAppender(config);
}
-exports.name = 'hookio';
exports.appender = createAppender;
exports.configure = configure;
@@ -16,6 +16,5 @@ function configure(config) {
return logLevelFilter(config.level, appender);
}
-exports.name = "logLevelFilter";
exports.appender = logLevelFilter;
exports.configure = configure;
@@ -72,6 +72,5 @@ function configure(config) {
return createAppender(config);
}
-exports.name = 'multiprocess';
exports.appender = createAppender;
exports.configure = configure;
View
@@ -2,15 +2,15 @@ var layouts = require("../layouts"),
mailer = require("nodemailer");
/**
-* SMTP Appender. Sends logging events using SMTP protocol.
+* SMTP Appender. Sends logging events using SMTP protocol.
* It can either send an email on each event or group several logging events gathered during specified interval.
*
* @param recipients comma separated list of email recipients
* @param sender sender of all emails (defaults to SMTP user)
-* @param subject subject of all email messages (defaults to first event's message)
+* @param subject subject of all email messages (defaults to first event's message)
* @param layout a function that takes a logevent and returns a string (defaults to basicLayout).
* @param smtpConfig SMTP configuration for 'nodemailer'
-* @param sendInterval the time in seconds between sending attempts (defaults to 0);
+* @param sendInterval the time in seconds between sending attempts (defaults to 0);
* all events are buffered and sent in one email during this time; if 0 than every event sends an email
*/
function smtpAppender(recipients, sender, subject, layout, smtpConfig, sendInterval) {
@@ -19,20 +19,20 @@ function smtpAppender(recipients, sender, subject, layout, smtpConfig, sendInter
subjectLayout = layouts.messagePassThroughLayout;
mailer.SMTP = smtpConfig;
sendInterval = sendInterval*1000 || 0;
-
+
var logEventBuffer = [];
var sendTimer;
-
+
function sendBuffer() {
if (logEventBuffer.length == 0)
return;
-
+
var firstEvent = logEventBuffer[0];
var body = "";
while (logEventBuffer.length > 0) {
body += layout(logEventBuffer.shift()) + "\n";
}
-
+
var msg = {
sender: sender,
to: recipients,
@@ -45,15 +45,15 @@ function smtpAppender(recipients, sender, subject, layout, smtpConfig, sendInter
}
});
}
-
+
function scheduleSend() {
if (!sendTimer)
sendTimer = setTimeout(function() {
- sendTimer = null;
+ sendTimer = null;
sendBuffer();
}, sendInterval);
}
-
+
return function(loggingEvent) {
logEventBuffer.push(loggingEvent);
if (sendInterval > 0)
@@ -71,6 +71,5 @@ function configure(config) {
return smtpAppender(config.recipients, config.sender, config.subject, layout, config.smtp, config.sendInterval);
}
-exports.name = "smtp";
exports.appender = smtpAppender;
exports.configure = configure;
View
@@ -47,6 +47,13 @@ Level.prototype.isGreaterThanOrEqualTo = function(otherLevel) {
return this.level >= otherLevel.level;
};
+Level.prototype.isEqualTo = function(otherLevel) {
+ if (typeof otherLevel == "string") {
+ otherLevel = toLevel(otherLevel);
+ }
+ return this.level === otherLevel.level;
+}
+
module.exports = {
ALL: new Level(Number.MIN_VALUE, "ALL")
, TRACE: new Level(5000, "TRACE")
View
@@ -282,9 +282,14 @@ function restoreConsole() {
}
function loadAppender(appender) {
- var appenderModule = require('./appenders/' + appender);
- module.exports.appenders[appenderModule.name] = appenderModule.appender;
- appenderMakers[appenderModule.name] = appenderModule.configure;
+ var appenderModule;
+ try {
+ appenderModule = require('./appenders/' + appender);
+ } catch (e) {
+ appenderModule = require(appender);
+ }
+ module.exports.appenders[appender] = appenderModule.appender.bind(appenderModule);
+ appenderMakers[appender] = appenderModule.configure.bind(appenderModule);
}
module.exports = {
View
@@ -0,0 +1,78 @@
+var levels = require('./levels'),
+ util = require('util'),
+ events = require('events'),
+ DEFAULT_CATEGORY = '[default]';
+
+/**
+ * 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) {
+ this.startTime = new Date();
+ this.categoryName = categoryName;
+ this.data = data;
+ this.level = level;
+ this.logger = logger;
+}
+
+/**
+ * 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;
+
+ if (! this.level) {
+ this.__proto__.level = levels.TRACE;
+ }
+}
+util.inherits(Logger, events.EventEmitter);
+Logger.DEFAULT_CATEGORY = DEFAULT_CATEGORY;
+
+Logger.prototype.setLevel = function(level) {
+ this.level = levels.toLevel(level, this.level || levels.TRACE);
+};
+
+Logger.prototype.removeLevel = function() {
+ delete this.level;
+};
+
+Logger.prototype.log = function() {
+ var args = Array.prototype.slice.call(arguments)
+ , logLevel = args.shift()
+ , loggingEvent = new LoggingEvent(this.category, logLevel, args, this);
+ this.emit("log", loggingEvent);
+};
+
+Logger.prototype.isLevelEnabled = function(otherLevel) {
+ return this.level.isLessThanOrEqualTo(otherLevel);
+};
+
+['Trace','Debug','Info','Warn','Error','Fatal'].forEach(
+ function(levelString) {
+ var level = levels.toLevel(levelString);
+ Logger.prototype['is'+levelString+'Enabled'] = function() {
+ return this.isLevelEnabled(level);
+ };
+
+ Logger.prototype[levelString.toLowerCase()] = function () {
+ if (this.isLevelEnabled(level)) {
+ var args = Array.prototype.slice.call(arguments);
+ args.unshift(level);
+ Logger.prototype.log.apply(this, args);
+ }
+ };
+ }
+);
+
+
+exports.LoggingEvent = LoggingEvent;
+exports.Logger = Logger;
View
@@ -0,0 +1,89 @@
+var assert = require('assert'),
+ vows = require('vows'),
+ sandbox = require('sandboxed-module');
+
+function makeTestAppender() {
+ return {
+ configure: function(config, options) {
+ this.configureCalled = true;
+ this.config = config;
+ this.options = options;
+ return this.appender();
+ },
+ appender: function() {
+ var self = this;
+ return function(logEvt) { self.logEvt = logEvt; }
+ }
+ };
+}
+
+vows.describe('log4js configure').addBatch({
+ 'appenders': {
+ 'when specified by type': {
+ topic: function() {
+ var testAppender = makeTestAppender(),
+ log4js = sandbox.require(
+ '../lib/log4js',
+ {
+ requires: {
+ './appenders/cheese': testAppender
+ }
+ }
+ );
+ log4js.configure(
+ {
+ appenders: [
+ { type: "cheese", flavour: "gouda" }
+ ]
+ },
+ { pants: "yes" }
+ );
+ return testAppender;
+ },
+ 'should load appender': function(testAppender) {
+ assert.ok(testAppender.configureCalled);
+ },
+ 'should pass config to appender': function(testAppender) {
+ assert.equal(testAppender.config.flavour, 'gouda');
+ },
+ 'should pass log4js options to appender': function(testAppender) {
+ assert.equal(testAppender.options.pants, 'yes');
+ }
+ },
+ 'when core appender loaded via loadAppender': {
+ topic: function() {
+ var testAppender = makeTestAppender(),
+ log4js = sandbox.require(
+ '../lib/log4js',
+ { requires: { './appenders/cheese': testAppender } }
+ );
+
+ log4js.loadAppender('cheese');
+ return log4js;
+ },
+ 'should load appender from ../lib/appenders': function(log4js) {
+ assert.ok(log4js.appenders.cheese);
+ },
+ 'should add appender configure function to appenderMakers' : function(log4js) {
+ assert.isFunction(log4js.appenderMakers.cheese);
+ }
+ },
+ 'when appender in node_modules loaded via loadAppender': {
+ topic: function() {
+ var testAppender = makeTestAppender(),
+ log4js = sandbox.require(
+ '../lib/log4js',
+ { requires: { 'some/other/external': testAppender } }
+ );
+ log4js.loadAppender('some/other/external');
+ return log4js;
+ },
+ 'should load appender via require': function(log4js) {
+ assert.ok(log4js.appenders['some/other/external']);
+ },
+ 'should add appender configure function to appenderMakers': function(log4js) {
+ assert.isFunction(log4js.appenderMakers['some/other/external']);
+ }
+ }
+ }
+}).exportTo(module);
Oops, something went wrong.

0 comments on commit a046523

Please sign in to comment.