8000 expanded example to include loading appender programmatically · rboss/log4js-node@68b47dd · GitHub
[go: up one dir, main page]

Skip to content

Commit 68b47dd

Browse files
author
Gareth Jones
committed
expanded example to include loading appender programmatically
1 parent 8f9b444 commit 68b47dd

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

example.js

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,27 @@ log4js.configure({
1313
],
1414
replaceConsole: true
1515
});
16-
//log4js.addAppender(log4js.fileAppender('cheese.log'), 'cheese', 'console');
16+
17+
//to add an appender programmatically, and without clearing other appenders
18+
//loadAppender is only necessary if you haven't already configured an appender of this type
19+
log4js.loadAppender('file');
20+
log4js.addAppender(log4js.appenders.file('pants.log'), 'pants');
21+
//a custom logger outside of the log4js/lib/appenders directory can be accessed like so
22+
//log4js.loadAppender('what/you/would/put/in/require');
23+
//log4js.addAppender(log4js.appenders['what/you/would/put/in/require'](args));
24+
//or through configure as:
25+
//log4js.configure({
26+
// appenders: [ { type: 'what/you/would/put/in/require', otherArgs: 'blah' } ]
27+
//});
1728

1829
var logger = log4js.getLogger('cheese');
1930
//only errors and above get logged.
31+
//you can also set this log level in the config object
32+
//via the levels field.
2033
logger.setLevel('ERROR');
2134

22-
//console logging methds have been replaced with log4js ones.
35+
//console logging methods have been replaced with log4js ones.
36+
//so this will get coloured output on console, and appear in cheese.log
2337
console.error("AAArgh! Something went wrong", { some: "otherObject", useful_for: "debug purposes" });
2438

2539
//these will not appear (logging level beneath error)
@@ -35,5 +49,10 @@ logger.fatal('Cheese was breeding ground for listeria.');
3549
var anotherLogger = log4js.getLogger('another');
3650
anotherLogger.debug("Just checking");
3751

52+
//one for pants.log
53+
//will also go to console, since that's configured for all categories
54+
var pantsLog = log4js.getLogger('pants');
55+
pantsLog.debug("Something for pants");
56+
3857

3958

0 commit comments

Comments
 (0)
0