From c2f9ccce730fced579e6e51fb35878b1dfb7ba69 Mon Sep 17 00:00:00 2001 From: csausdev Date: Wed, 8 Dec 2010 08:53:59 +1100 Subject: [PATCH] enhanced console.log --- README.md | 5 ++++- lib/log4js.js | 28 +++++++++++++++++++++++----- package.json | 2 +- test/logging.js | 33 ++++++++++++++++++++++++++++++++- 4 files changed, 60 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index e423020..a5b6ba2 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/lib/log4js.js b/lib/log4js.js index 5ef7f4f..2a0e0b1 100644 --- a/lib/log4js.js +++ b/lib/log4js.js @@ -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, diff --git a/package.json b/package.json index e5d10f8..3ad42ae 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "log4js", - "version": "0.2.0", + "version": "0.2.1", "description": "Port of Log4js to work with node.", "keywords": [ "logging", diff --git a/test/logging.js b/test/logging.js index 3be6d0a..0d48492 100644 --- a/test/logging.js +++ b/test/logging.js @@ -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);