added methods and config to turn off console.log replacement (issue #34)
This commit is contained in:
@@ -279,6 +279,12 @@ function configureOnceOff(config) {
|
|||||||
try {
|
try {
|
||||||
configureAppenders(config.appenders);
|
configureAppenders(config.appenders);
|
||||||
configureLevels(config.levels);
|
configureLevels(config.levels);
|
||||||
|
|
||||||
|
if (config.doNotReplaceConsole) {
|
||||||
|
restoreConsole();
|
||||||
|
} else {
|
||||||
|
replaceConsole();
|
||||||
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
throw new Error("Problem reading log4js config " + util.inspect(config) + ". Error was \"" + e.message + "\" ("+e.stack+")");
|
throw new Error("Problem reading log4js config " + util.inspect(config) + ". Error was \"" + e.message + "\" ("+e.stack+")");
|
||||||
}
|
}
|
||||||
@@ -339,13 +345,22 @@ function replaceConsole(logger) {
|
|||||||
fn.apply(logger, arguments);
|
fn.apply(logger, arguments);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (console['_preLog4js_log'] === undefined) {
|
||||||
['log','debug','info','warn','error'].forEach(function (item) {
|
logger = logger || getLogger("console");
|
||||||
if (console['_preLog4js_log'] === undefined) {
|
['log','debug','info','warn','error'].forEach(function (item) {
|
||||||
console['_preLog4js_'+item] = console[item];
|
console['_preLog4js_'+item] = console[item];
|
||||||
}
|
console[item] = replaceWith(item === 'log' ? logger.info : logger[item]);
|
||||||
console[item] = replaceWith(item === 'log' ? logger.info : logger[item]);
|
});
|
||||||
});
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function restoreConsole() {
|
||||||
|
if (console['_preLog4js_log']) {
|
||||||
|
['log', 'debug', 'info', 'warn', 'error'].forEach(function (item) {
|
||||||
|
console[item] = console['_preLog4js_'+item];
|
||||||
|
delete console['_preLog4js_'+item];
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function loadAppender(appender) {
|
function loadAppender(appender) {
|
||||||
@@ -363,6 +378,9 @@ module.exports = {
|
|||||||
clearAppenders: clearAppenders,
|
clearAppenders: clearAppenders,
|
||||||
configure: configure,
|
configure: configure,
|
||||||
|
|
||||||
|
replaceConsole: replaceConsole,
|
||||||
|
restoreConsole: restoreConsole,
|
||||||
|
|
||||||
levels: levels,
|
levels: levels,
|
||||||
setGlobalLogLevel: setGlobalLogLevel,
|
setGlobalLogLevel: setGlobalLogLevel,
|
||||||
|
|
||||||
@@ -379,8 +397,6 @@ module.exports = {
|
|||||||
|
|
||||||
//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
|
|
||||||
replaceConsole(getLogger("console"));
|
|
||||||
|
|
||||||
//keep the old-style layouts
|
//keep the old-style layouts
|
||||||
['basicLayout','messagePassThroughLayout','colouredLayout','coloredLayout'].forEach(function(item) {
|
['basicLayout','messagePassThroughLayout','colouredLayout','coloredLayout'].forEach(function(item) {
|
||||||
|
|||||||
152
test/logging.js
152
test/logging.js
@@ -2,6 +2,34 @@ var vows = require('vows')
|
|||||||
, assert = require('assert')
|
, assert = require('assert')
|
||||||
, sandbox = require('sandboxed-module');
|
, sandbox = require('sandboxed-module');
|
||||||
|
|
||||||
|
function setupConsoleTest() {
|
||||||
|
var fakeConsole = {}
|
||||||
|
, logEvents = []
|
||||||
|
, log4js;
|
||||||
|
|
||||||
|
['trace','debug','log','info','warn','error'].forEach(function(fn) {
|
||||||
|
fakeConsole[fn] = function() {
|
||||||
|
throw new Error("this should not be called.");
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
|
log4js = sandbox.require(
|
||||||
|
'../lib/log4js'
|
||||||
|
, {
|
||||||
|
globals: {
|
||||||
|
console: fakeConsole
|
||||||
|
}
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
log4js.clearAppenders();
|
||||||
|
log4js.addAppender(function(evt) {
|
||||||
|
logEvents.push(evt);
|
||||||
|
});
|
||||||
|
|
||||||
|
return { log4js: log4js, logEvents: logEvents, fakeConsole: fakeConsole };
|
||||||
|
}
|
||||||
|
|
||||||
vows.describe('log4js').addBatch({
|
vows.describe('log4js').addBatch({
|
||||||
'getLogger': {
|
'getLogger': {
|
||||||
topic: function() {
|
topic: function() {
|
||||||
@@ -268,7 +296,6 @@ vows.describe('log4js').addBatch({
|
|||||||
'default setup': {
|
'default setup': {
|
||||||
topic: function() {
|
topic: function() {
|
||||||
var pathLoaded,
|
var pathLoaded,
|
||||||
appenderEvent,
|
|
||||||
logger,
|
logger,
|
||||||
modulePath = require('path').normalize(__dirname + '/../lib/log4js.json'),
|
modulePath = require('path').normalize(__dirname + '/../lib/log4js.json'),
|
||||||
mtime = Date.now(),
|
mtime = Date.now(),
|
||||||
@@ -289,28 +316,36 @@ vows.describe('log4js').addBatch({
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
appenderEvents = [],
|
||||||
fakeConsole = {
|
fakeConsole = {
|
||||||
'name': 'console'
|
'name': 'console'
|
||||||
, 'appender': function () {
|
, 'appender': function () {
|
||||||
return function(evt) { appenderEvent = evt; }
|
return function(evt) { appenderEvents.push(evt); }
|
||||||
}
|
}
|
||||||
, 'configure': function (config) {
|
, 'configure': function (config) {
|
||||||
return fakeConsole.appender();
|
return fakeConsole.appender();
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
globalConsole = {
|
||||||
|
log: function() { throw new Error("I should not be called."); }
|
||||||
|
},
|
||||||
log4js = sandbox.require(
|
log4js = sandbox.require(
|
||||||
'../lib/log4js',
|
'../lib/log4js',
|
||||||
{
|
{
|
||||||
requires: {
|
requires: {
|
||||||
'fs': fakeFS
|
'fs': fakeFS
|
||||||
, './appenders/console': fakeConsole
|
, './appenders/console': fakeConsole
|
||||||
|
},
|
||||||
|
globals: {
|
||||||
|
console: globalConsole
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
logger = log4js.getLogger('a-test');
|
logger = log4js.getLogger('a-test');
|
||||||
logger.debug("this is a test");
|
logger.debug("this is a test");
|
||||||
return [ pathLoaded, appenderEvent, modulePath ];
|
globalConsole.log("this should be logged");
|
||||||
|
return [ pathLoaded, appenderEvents, modulePath ];
|
||||||
},
|
},
|
||||||
|
|
||||||
'should use require.resolve to find log4js.json': function(args) {
|
'should use require.resolve to find log4js.json': function(args) {
|
||||||
@@ -319,61 +354,82 @@ vows.describe('log4js').addBatch({
|
|||||||
},
|
},
|
||||||
|
|
||||||
'should configure log4js from first log4js.json found': function(args) {
|
'should configure log4js from first log4js.json found': function(args) {
|
||||||
var appenderEvent = args[1];
|
var appenderEvents = args[1];
|
||||||
assert.equal(appenderEvent.data[0], 'this is a test');
|
assert.equal(appenderEvents[0].data[0], 'this is a test');
|
||||||
|
},
|
||||||
|
|
||||||
|
'should replace console.log with log4js version': function(args) {
|
||||||
|
var appenderEvents = args[1];
|
||||||
|
assert.equal(appenderEvents[1].data[0], 'this should be logged');
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
'console' : {
|
'console' : {
|
||||||
topic: function() {
|
topic: setupConsoleTest,
|
||||||
var fakeConsole = {}
|
|
||||||
, logEvents = []
|
|
||||||
, log4js;
|
|
||||||
|
|
||||||
['trace','debug','log','info','warn','error'].forEach(function(fn) {
|
'when replaceConsole called': {
|
||||||
fakeConsole[fn] = function() {
|
topic: function(test) {
|
||||||
throw new Error("this should not be called.");
|
test.log4js.replaceConsole();
|
||||||
};
|
|
||||||
});
|
|
||||||
|
|
||||||
log4js = sandbox.require(
|
test.fakeConsole.log("Some debug message someone put in a module");
|
||||||
'../lib/log4js'
|
test.fakeConsole.debug("Some debug");
|
||||||
, {
|
test.fakeConsole.error("An error");
|
||||||
globals: {
|
test.fakeConsole.info("some info");
|
||||||
console: fakeConsole
|
test.fakeConsole.warn("a warning");
|
||||||
}
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
log4js.clearAppenders();
|
test.fakeConsole.log("cheese (%s) and biscuits (%s)", "gouda", "garibaldis");
|
||||||
log4js.addAppender(function(evt) {
|
test.fakeConsole.log({ lumpy: "tapioca" });
|
||||||
logEvents.push(evt);
|
test.fakeConsole.log("count %d", 123);
|
||||||
});
|
test.fakeConsole.log("stringify %j", { lumpy: "tapioca" });
|
||||||
|
|
||||||
fakeConsole.log("Some debug message someone put in a module");
|
return test.logEvents;
|
||||||
fakeConsole.debug("Some debug");
|
},
|
||||||
fakeConsole.error("An error");
|
|
||||||
fakeConsole.info("some info");
|
|
||||||
fakeConsole.warn("a warning");
|
|
||||||
|
|
||||||
fakeConsole.log("cheese (%s) and biscuits (%s)", "gouda", "garibaldis");
|
'should replace console.log methods with log4js ones': function(logEvents) {
|
||||||
fakeConsole.log({ lumpy: "tapioca" });
|
assert.equal(logEvents.length, 9);
|
||||||
fakeConsole.log("count %d", 123);
|
assert.equal(logEvents[0].data[0], "Some debug message someone put in a module");
|
||||||
fakeConsole.log("stringify %j", { lumpy: "tapioca" });
|
assert.equal(logEvents[0].level.toString(), "INFO");
|
||||||
|
assert.equal(logEvents[1].data[0], "Some debug");
|
||||||
return logEvents;
|
assert.equal(logEvents[1].level.toString(), "DEBUG");
|
||||||
|
assert.equal(logEvents[2].data[0], "An error");
|
||||||
|
assert.equal(logEvents[2].level.toString(), "ERROR");
|
||||||
|
assert.equal(logEvents[3].data[0], "some info");
|
||||||
|
assert.equal(logEvents[3].level.toString(), "INFO");
|
||||||
|
assert.equal(logEvents[4].data[0], "a warning");
|
||||||
|
assert.equal(logEvents[4].level.toString(), "WARN");
|
||||||
|
assert.equal(logEvents[5].data[0], "cheese (%s) and biscuits (%s)");
|
||||||
|
assert.equal(logEvents[5].data[1], "gouda");
|
||||||
|
assert.equal(logEvents[5].data[2], "garibaldis");
|
||||||
|
}
|
||||||
},
|
},
|
||||||
'should replace console.log methods with log4js ones': function(logEvents) {
|
'when turned off': {
|
||||||
assert.equal(logEvents[0].data[0], "Some debug message someone put in a module");
|
topic: function(test) {
|
||||||
assert.equal(logEvents[0].level.toString(), "INFO");
|
test.log4js.restoreConsole();
|
||||||
assert.equal(logEvents[1].data[0], "Some debug");
|
try {
|
||||||
assert.equal(logEvents[1].level.toString(), "DEBUG");
|
test.fakeConsole.log("This should cause the error described in the setup");
|
||||||
assert.equal(logEvents[2].data[0], "An error");
|
} catch (e) {
|
||||||
assert.equal(logEvents[2].level.toString(), "ERROR");
|
return e;
|
||||||
assert.equal(logEvents[3].data[0], "some info");
|
}
|
||||||
assert.equal(logEvents[3].level.toString(), "INFO");
|
},
|
||||||
assert.equal(logEvents[4].data[0], "a warning");
|
'should call the original console methods': function (err) {
|
||||||
assert.equal(logEvents[4].level.toString(), "WARN");
|
assert.instanceOf(err, Error);
|
||||||
|
assert.equal(err.message, "this should not be called.");
|
||||||
|
}
|
||||||
|
},
|
||||||
|
'configuration': {
|
||||||
|
topic: function(test) {
|
||||||
|
test.log4js.replaceConsole();
|
||||||
|
test.log4js.configure({ doNotReplaceConsole: true });
|
||||||
|
try {
|
||||||
|
test.fakeConsole.log("This should cause the error described in the setup");
|
||||||
|
} catch (e) {
|
||||||
|
return e;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
'should allow for turning off console replacement': function (err) {
|
||||||
|
assert.instanceOf(err, Error);
|
||||||
|
assert.equal(err.message, 'this should not be called.');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
'configuration persistence' : {
|
'configuration persistence' : {
|
||||||
|
|||||||
Reference in New Issue
Block a user