8000 enhanced console.log · lalitkapoor/log4js-node@c2f9ccc · GitHub
[go: up one dir, main page]

Skip to content

Commit c2f9ccc

Browse files
author
csausdev
committed
enhanced console.log
1 parent 2e38432 commit c2f9ccc

File tree

4 files changed

+60
-8
lines changed

4 files changed

+60
-8
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
This is a conversion of the [log4js](http://log4js.berlios.de/index.html)
44
framework to work with [node](http://nodejs.org). I've mainly stripped out the browser-specific code
5-
and tidied up some of the javascript. It includes a basic file logger, with log rolling based on file size.
5+
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.
66

77
NOTE: since v0.2.0 require('lo 8000 g4js') 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).
88

@@ -20,6 +20,9 @@ Minimalist version:
2020
var log4js = require('log4js')();
2121
var logger = log4js.getLogger();
2222
logger.debug("Some debug messages");
23+
Even more minimalist version:
24+
require('log4js')();
25+
console.debug("Some debug messages");
2326
By default, log4js outputs to stdout with the coloured layout (thanks to [masylum](http://github.com/masylum)), so for the above you would see:
2427
[2010-01-17 11:43:37.987] [DEBUG] [default] - Some debug messages
2528

lib/log4js.js

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,12 @@
4545
* Website: http://log4js.berlios.de
4646
*/
4747
module.exports = function (fileSystem, standardOutput, configPaths) {
48-
var fs = fileSystem || require('fs'),
49-
standardOutput = standardOutput || console.log,
50-
configPaths = configPaths || require.paths,
51-
sys = require('sys'),
52-
events = require('events'),
48+
var events = require('events'),
5349
path = require('path'),
50+
sys = require('sys'),
51+
fs = fileSystem || require('fs'),
52+
standardOutput = standardOutput || sys.puts,
53+
configPaths = configPaths || require.paths,
5454
DEFAULT_CATEGORY = '[default]',
5555
ALL_CATEGORIES = '[all]',
5656
loggers = {},
@@ -614,8 +614,26 @@ module.exports = function (fileSystem, standardOutput, configPaths) {
614614

615615
};
616616

617+
function replaceConsole(logger) {
618+
function replaceWith (fn) {
619+
return function() {
620+
fn.apply(logger, arguments);
621+
}
622+
}
623+
624+
console.log = replaceWith(logger.info);
625+
console.debug = replaceWith(logger.debug);
626+
console.trace = replaceWith(logger.trace);
627+
console.info = replaceWith(logger.info);
628+
console.warn = replaceWith(logger.warn);
629+
console.error = replaceWith(logger.error);
630+
631+
}
632+
617633
//set ourselves up if we can find a default log4js.json
618634
configure(findConfiguration());
635+
//replace console.log, etc with log4js versions
636+
replaceConsole(getLogger("console"));
619637

620638
return {
621639
getLogger: getLogger,

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "log4js",
3-
"version": "0.2.0",
3+
"version": "0.2.1",
44
"description": "Port of Log4js to work with node.",
55
"keywords": [
66
"logging",

test/logging.js

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -482,7 +482,7 @@ vows.describe('log4js').addBatch({
482482

483483
'Date extensions': {
484484
topic: function() {
485-
require('../lib/log4js');
485+
require('../lib/log4js')();
486486
return new Date(2010, 0, 11, 14, 31, 30, 5);
487487
},
488488
'should add a toFormattedString method to Date': function(date) {
@@ -491,6 +491,37 @@ vows.describe('log4js').addBatch({
491491
'should default to a format': function(date) {
492492
assert.equal(date.toFormattedString(), '2010-01-11 14:31:30.005');
493493
}
494+
},
495+
496+
'console' : {
497+
topic: function() {
498+
return require('../lib/log4js')();
499+
},
500+
'should replace console.log methods with log4js ones': function(log4js) {
501+
var logEvent;
502+
log4js.clearAppenders();
503+
log4js.addAppender(function(evt) { logEvent = evt; });
504+
505+
console.log("Some debug message someone put in a module");
506+
assert.equal(logEvent.message, "Some debug message someone put in a module");
507+
assert.equal(logEvent.level.toString(), "INFO");
508+
logEvent = undefined;
509+
console.debug("Some debug");
510+
assert.equal(logEvent.message, "Some debug");
511+
assert.equal(logEvent.level.toString(), "DEBUG");
512+
logEvent = undefined;
513+
console.error("An error");
514+
assert.equal(logEvent.message, "An error");
515+
assert.equal(logEvent.level.toString(), "ERROR");
516+
logEvent = undefined;
517+
console.info("some info");
518+
assert.equal(logEvent.message, "some info");
519+
assert.equal(logEvent.level.toString(), "INFO");
520+
logEvent = undefined;
521+
console.trace("tracing");
522+
assert.equal(logEvent.message, "tracing");
523+
assert.equal(logEvent.level.toString(), "TRACE");
524+
}
494525
}
495526

496527
}).export(module);

0 commit comments

Comments
 (0)
0