From d7ffa59434dcc964208624f621c02d334bfaed76 Mon Sep 17 00:00:00 2001 From: Gareth Jones Date: Wed, 13 Jul 2011 18:29:53 +1000 Subject: [PATCH] moved level colours into layouts where they belong, updated README --- README.md | 13 +++++-------- lib/layouts.js | 23 +++++++++++++++++------ lib/levels.js | 19 +++++++++---------- test/layouts.js | 6 ++---- 4 files changed, 33 insertions(+), 28 deletions(-) diff --git a/README.md b/README.md index dcd17e7..820b313 100644 --- a/README.md +++ b/README.md @@ -2,12 +2,9 @@ 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. - -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). - -NOTE: I have disabled the console.log replacement which appeared in earlier versions, as console.log does some fancy formatting of output that log4js cannot handle. I don't want log4js breaking people's code mysteriously. +and tidied up some of the javascript. It includes a basic file logger, with log rolling based on file size, and also replaces node's console.log functions. +NOTE: in v0.2.x require('log4js') returned a function, and you needed to call that function in your code before you could use it. This was to make testing easier. v0.3.x make use of [felixge's sandbox-module](https://github.com/felixge/sandbox-module), so we don't need to return a function. ## installation @@ -21,7 +18,7 @@ Tests now use [vows](http://vowsjs.org), run with `vows test/*.js`. Minimalist version: - var log4js = require('log4js')(); + var log4js = require('log4js'); var logger = log4js.getLogger(); logger.debug("Some debug messages"); @@ -31,7 +28,7 @@ By default, log4js outputs to stdout with the coloured layout (thanks to [masylu See example.js: - var log4js = require('log4js')(); //note the need to call the function + var log4js = require('log4js'); //note the need to call the function log4js.addAppender(log4js.consoleAppender()); log4js.addAppender(log4js.fileAppender('logs/cheese.log'), 'cheese'); @@ -68,7 +65,7 @@ the original log4js. A connect/express logger has been added to log4js. This allows connect/express servers to log using log4js. See example-connect-logger.js. - var log4js = require('./lib/log4js')(); + var log4js = require('./lib/log4js'); log4js.addAppender(log4js.consoleAppender()); log4js.addAppender(log4js.fileAppender('cheese.log'), 'cheese'); diff --git a/lib/layouts.js b/lib/layouts.js index 67fb772..d3972e9 100644 --- a/lib/layouts.js +++ b/lib/layouts.js @@ -2,16 +2,27 @@ var dateFormat = require('./date_format') , util = require('util') , replacementRegExp = /%[sdj]/g , layoutMakers = { - "messagePassThrough": function() { return messagePassThroughLayout; }, - "basic": function() { return basicLayout; }, - "colored": function() { return colouredLayout; }, - "coloured": function() { return colouredLayout; }, - "pattern": function (config) { + "messagePassThrough": function() { return messagePassThroughLayout; } + , "basic": function() { return basicLayout; } + , "colored": function() { return colouredLayout; } + , "coloured": function() { return colouredLayout; } + , "pattern": function (config) { var pattern = config.pattern || undefined; return patternLayout(pattern); } +} +, colours = { + ALL: "grey" + , TRACE: "blue" + , DEBUG: "cyan" + , INFO: "green" + , WARN: "yellow" + , ERROR: "red" + , FATAL: "magenta" + , OFF: "grey" }; + function formatLogData(logData) { var output = "" , data = Array.isArray(logData) ? logData.slice() : Array.prototype.slice.call(arguments) @@ -100,7 +111,7 @@ function basicLayout (loggingEvent) { * same as basicLayout, but with colours. */ function colouredLayout (loggingEvent) { - return timestampLevelAndCategory(loggingEvent, loggingEvent.level.colour) + formatLogData(loggingEvent.data); + return timestampLevelAndCategory(loggingEvent, colours[loggingEvent.level.toString()]) + formatLogData(loggingEvent.data); } function messagePassThroughLayout (loggingEvent) { diff --git a/lib/levels.js b/lib/levels.js index ea21de1..d210209 100644 --- a/lib/levels.js +++ b/lib/levels.js @@ -1,7 +1,6 @@ -function Level(level, levelStr, colour) { +function Level(level, levelStr) { this.level = level; this.levelStr = levelStr; - this.colour = colour; } /** @@ -45,13 +44,13 @@ Level.prototype.isGreaterThanOrEqualTo = function(otherLevel) { }; module.exports = { - ALL: new Level(Number.MIN_VALUE, "ALL", "grey") - , TRACE: new Level(5000, "TRACE", "blue") - , DEBUG: new Level(10000, "DEBUG", "cyan") - , INFO: new Level(20000, "INFO", "green") - , WARN: new Level(30000, "WARN", "yellow") - , ERROR: new Level(40000, "ERROR", "red") - , FATAL: new Level(50000, "FATAL", "magenta") - , OFF: new Level(Number.MAX_VALUE, "OFF", "grey") + ALL: new Level(Number.MIN_VALUE, "ALL") + , TRACE: new Level(5000, "TRACE") + , DEBUG: new Level(10000, "DEBUG") + , INFO: new Level(20000, "INFO") + , WARN: new Level(30000, "WARN") + , ERROR: new Level(40000, "ERROR") + , FATAL: new Level(50000, "FATAL") + , OFF: new Level(Number.MAX_VALUE, "OFF") , toLevel: toLevel }; diff --git a/test/layouts.js b/test/layouts.js index 198cae8..7a75e04 100644 --- a/test/layouts.js +++ b/test/layouts.js @@ -13,11 +13,10 @@ vows.describe('log4js layouts').addBatch({ startTime: new Date(2010, 11, 5, 14, 18, 30, 45), categoryName: "cheese", level: { - colour: "green", toString: function() { return "ERROR"; } } }); - assert.equal(output, '\033[32m[2010-12-05 14:18:30.045] [ERROR] cheese - \033[39mnonsense'); + assert.equal(output, '\033[31m[2010-12-05 14:18:30.045] [ERROR] cheese - \033[39mnonsense'); }, 'should support the console.log format for the message': function(layout) { @@ -26,11 +25,10 @@ vows.describe('log4js layouts').addBatch({ startTime: new Date(2010, 11, 5, 14, 18, 30, 45), categoryName: "cheese", level: { - colour: "green", toString: function() { return "ERROR"; } } }); - assert.equal(output, '\033[32m[2010-12-05 14:18:30.045] [ERROR] cheese - \033[39mthing 2'); + assert.equal(output, '\033[31m[2010-12-05 14:18:30.045] [ERROR] cheese - \033[39mthing 2'); } },