changed logger to not use events. everything is broken

This commit is contained in:
Gareth Jones 2013-08-02 11:36:05 +10:00
parent d25e1abd48
commit 3b55aefe6f
4 changed files with 59 additions and 59 deletions

View File

@ -43,7 +43,7 @@
* @static
* Website: http://log4js.berlios.de
*/
var events = require('events')
var debug = require('./debug')('log4js-core')
, fs = require('fs')
, path = require('path')
, util = require('util')
@ -51,9 +51,9 @@ var events = require('events')
, levels = require('./levels')
, LoggingEvent = require('./logger').LoggingEvent
, Logger = require('./logger').Logger
, categoryLevels = {}
, ALL_CATEGORIES = '[all]'
, appenders = {}
, loggers = {}
, appenderMakers = {}
, defaultConfig = {
appenders: [
@ -75,25 +75,9 @@ function getLogger (categoryName) {
categoryName = Logger.DEFAULT_CATEGORY;
}
var appenderList;
if (!loggers[categoryName]) {
// 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);
});
}
}
return loggers[categoryName];
debug("getLogger(" + categoryName + ")");
return new Logger(categoryName, levels.toLevel(categoryLevels[categoryName]) || null);
}
/**
@ -111,25 +95,12 @@ function addAppender () {
}
args.forEach(function(category) {
addAppenderToCategory(appender, category);
if (category === ALL_CATEGORIES) {
addAppenderToAllLoggers(appender);
} else if (loggers[category]) {
loggers[category].addListener("log", appender);
}
addAppenderToCategory(appender, category);
});
}
function addAppenderToAllLoggers(appender) {
for (var logger in loggers) {
if (loggers.hasOwnProperty(logger)) {
loggers[logger].addListener("log", appender);
}
}
}
function addAppenderToCategory(appender, category) {
debug("adding appender " + appender + " to category " + category);
if (!appenders[category]) {
appenders[category] = [];
}
@ -137,12 +108,8 @@ function addAppenderToCategory(appender, category) {
}
function clearAppenders () {
debug("clearing appenders");
appenders = {};
for (var logger in loggers) {
if (loggers.hasOwnProperty(logger)) {
loggers[logger].removeAllListeners("log");
}
}
}
function configureAppenders(appenderList, options) {
@ -163,13 +130,7 @@ function configureAppenders(appenderList, options) {
}
function configureLevels(levels) {
if (levels) {
for (var category in levels) {
if (levels.hasOwnProperty(category)) {
getLogger(category).setLevel(levels[category]);
}
}
}
categoryLevels = levels || {};
}
function setGlobalLogLevel(level) {
@ -185,6 +146,27 @@ function getDefaultLogger () {
return getLogger(Logger.DEFAULT_CATEGORY);
}
/**
* Log event routing to appenders
* This would be a good place to implement category hierarchies/wildcards, etc
*/
function dispatch(event) {
debug("event is " + util.inspect(event));
if (appenders[event.category]) {
dispatchToCategory(event.category, event);
}
if (appenders[ALL_CATEGORIES]) {
dispatchToCategory(ALL_CATEGORIES, event);
}
}
function dispatchToCategory(category, event) {
appenders[category].forEach(function(appender) {
appender(event);
});
}
var configState = {};
function loadConfigurationFile(filename) {
@ -304,6 +286,7 @@ function loadAppender(appender) {
module.exports = {
getLogger: getLogger,
getDefaultLogger: getDefaultLogger,
dispatch: dispatch,
addAppender: addAppender,
loadAppender: loadAppender,
@ -323,5 +306,6 @@ module.exports = {
};
//set ourselves up
debug("Starting configuration");
configure();

View File

@ -1,7 +1,7 @@
"use strict";
var levels = require('./levels')
, util = require('util')
, events = require('events')
, log4js = require('./log4js')
, DEFAULT_CATEGORY = '[default]';
/**
@ -10,15 +10,13 @@ var levels = require('./levels')
* @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) {
function LoggingEvent (categoryName, level, data) {
this.startTime = new Date();
this.categoryName = categoryName;
this.data = data;
this.level = level;
this.logger = logger;
}
/**
@ -35,7 +33,6 @@ function Logger (name, level) {
this.setLevel(level);
}
}
util.inherits(Logger, events.EventEmitter);
Logger.DEFAULT_CATEGORY = DEFAULT_CATEGORY;
Logger.prototype.level = levels.TRACE;
@ -50,8 +47,9 @@ Logger.prototype.removeLevel = function() {
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);
, loggingEvent = new LoggingEvent(this.category, logLevel, args);
log4js.dispatch(loggingEvent);
};
Logger.prototype.isLevelEnabled = function(otherLevel) {
@ -75,6 +73,5 @@ Logger.prototype.isLevelEnabled = function(otherLevel) {
}
);
exports.LoggingEvent = LoggingEvent;
exports.Logger = Logger;

View File

@ -2,7 +2,8 @@
var vows = require('vows')
, assert = require('assert')
, levels = require('../lib/levels')
, Logger = require('../lib/logger').Logger;
, Logger = require('../lib/logger').Logger
, log4js = require('../lib/log4js');
vows.describe('../lib/logger').addBatch({
'constructor with no parameters': {
@ -53,5 +54,22 @@ vows.describe('../lib/logger').addBatch({
assert.isTrue(logger.isErrorEnabled());
assert.isTrue(logger.isFatalEnabled());
}
},
'log': {
topic: new Logger('testing'),
'should send log events to log4js': function(logger) {
var evt, original = log4js.dispatch;
log4js.dispatch = function(event) {
evt = event;
};
logger.log(levels.DEBUG, "cheese");
log4js.dispatch = original;
assert.equal(evt.categoryName, 'testing');
assert.equal(evt.level, levels.DEBUG);
assert.equal(evt.data[0], "cheese");
}
}
}).exportTo(module);

View File

@ -54,7 +54,7 @@ vows.describe('log4js').addBatch({
'log events' : {
topic: function(logger) {
var events = [];
logger.addListener("log", function (logEvent) { events.push(logEvent); });
log4js.addAppender(function (logEvent) { events.push(logEvent); }, "tests");
logger.debug("Debug event");
logger.trace("Trace event 1");
logger.trace("Trace event 2");
@ -83,7 +83,7 @@ vows.describe('log4js').addBatch({
},
},
/*
'invalid configuration': {
'should throw an exception': function() {
assert.throws(function() {
@ -509,4 +509,5 @@ vows.describe('log4js').addBatch({
assert.ok(logger.error);
}
}
*/
}).export(module);