enhanced console.log

unstable
csausdev 14 years ago
parent 2e3843205a
commit c2f9ccce73

@ -2,7 +2,7 @@
This is a conversion of the [log4js](http://log4js.berlios.de/index.html)
framework to work with [node](http://nodejs.org). I've mainly stripped out the browser-specific code
and tidied up some of the javascript. It includes a basic file logger, with log rolling based on file size.
and tidied up some of the javascript. It includes a basic file logger, with log rolling based on file size. It also enhances the default console logging functions (console.log, console.debug, etc) so that they use log4js and can be directed to a file, with log rolling etc - which is handy if you have some third party modules that use console.log but want that output included in your application log files.
NOTE: since v0.2.0 require('log4js') returns a function, so you need to call that function in your code before you can use it. I've done this to make testing easier (allows dependency injection).
@ -20,6 +20,9 @@ Minimalist version:
var log4js = require('log4js')();
var logger = log4js.getLogger();
logger.debug("Some debug messages");
Even more minimalist version:
require('log4js')();
console.debug("Some debug messages");
By default, log4js outputs to stdout with the coloured layout (thanks to [masylum](http://github.com/masylum)), so for the above you would see:
[2010-01-17 11:43:37.987] [DEBUG] [default] - Some debug messages

@ -45,12 +45,12 @@
* Website: http://log4js.berlios.de
*/
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'),
var events = require('events'),
path = require('path'),
sys = require('sys'),
fs = fileSystem || require('fs'),
standardOutput = standardOutput || sys.puts,
configPaths = configPaths || require.paths,
DEFAULT_CATEGORY = '[default]',
ALL_CATEGORIES = '[all]',
loggers = {},
@ -614,8 +614,26 @@ module.exports = function (fileSystem, standardOutput, configPaths) {
};
function replaceConsole(logger) {
function replaceWith (fn) {
return function() {
fn.apply(logger, arguments);
}
}
console.log = replaceWith(logger.info);
console.debug = replaceWith(logger.debug);
console.trace = replaceWith(logger.trace);
console.info = replaceWith(logger.info);
console.warn = replaceWith(logger.warn);
console.error = replaceWith(logger.error);
}
//set ourselves up if we can find a default log4js.json
configure(findConfiguration());
//replace console.log, etc with log4js versions
replaceConsole(getLogger("console"));
return {
getLogger: getLogger,

@ -1,6 +1,6 @@
{
"name": "log4js",
"version": "0.2.0",
"version": "0.2.1",
"description": "Port of Log4js to work with node.",
"keywords": [
"logging",

@ -482,7 +482,7 @@ vows.describe('log4js').addBatch({
'Date extensions': {
topic: function() {
require('../lib/log4js');
require('../lib/log4js')();
return new Date(2010, 0, 11, 14, 31, 30, 5);
},
'should add a toFormattedString method to Date': function(date) {
@ -491,6 +491,37 @@ vows.describe('log4js').addBatch({
'should default to a format': function(date) {
assert.equal(date.toFormattedString(), '2010-01-11 14:31:30.005');
}
},
'console' : {
topic: function() {
return require('../lib/log4js')();
},
'should replace console.log methods with log4js ones': function(log4js) {
var logEvent;
log4js.clearAppenders();
log4js.addAppender(function(evt) { logEvent = evt; });
console.log("Some debug message someone put in a module");
assert.equal(logEvent.message, "Some debug message someone put in a module");
assert.equal(logEvent.level.toString(), "INFO");
logEvent = undefined;
console.debug("Some debug");
assert.equal(logEvent.message, "Some debug");
assert.equal(logEvent.level.toString(), "DEBUG");
logEvent = undefined;
console.error("An error");
assert.equal(logEvent.message, "An error");
assert.equal(logEvent.level.toString(), "ERROR");
logEvent = undefined;
console.info("some info");
assert.equal(logEvent.message, "some info");
assert.equal(logEvent.level.toString(), "INFO");
logEvent = undefined;
console.trace("tracing");
assert.equal(logEvent.message, "tracing");
assert.equal(logEvent.level.toString(), "TRACE");
}
}
}).export(module);

Loading…
Cancel
Save