|
1 |
| -"use strict"; |
2 |
| -var log4js = require('../lib/log4js'); |
3 |
| -//log the cheese logger messages to a file, and the console ones as well. |
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const log4js = require('../lib/log4js'); |
| 4 | +// log the cheese logger messages to a file, and the console ones as well. |
4 | 5 | log4js.configure({
|
5 |
| - appenders: [ |
6 |
| - { |
7 |
| - type: "file", |
8 |
| - filename: "cheese.log", |
9 |
| - category: [ 'cheese','console' ] |
10 |
| - }, |
11 |
| - { |
12 |
| - type: "console" |
13 |
| - } |
14 |
| - ], |
15 |
| - replaceConsole: true |
| 6 | + appenders: { |
| 7 | + cheeseLogs: { type: 'file', filename: 'cheese.log' }, |
| 8 | + console: { type: 'console' } |
| 9 | + }, |
| 10 | + categories: { |
| 11 | + cheese: { appenders: ['cheeseLogs'], level: 'error' }, |
| 12 | + another: { appenders: ['console'], level: 'trace' }, |
| 13 | + default: { appenders: ['console', 'cheeseLogs'], level: 'trace' } |
| 14 | + } |
16 | 15 | });
|
17 | 16 |
|
18 |
| -//to add an appender programmatically, and without clearing other appenders |
19 |
| -//loadAppender is only necessary if you haven't already configured an appender of this type |
20 |
| -log4js.loadAppender('file'); |
21 |
| -log4js.addAppender(log4js.appenders.file('pants.log'), 'pants'); |
22 |
| -//a custom logger outside of the log4js/lib/appenders directory can be accessed like so |
23 |
| -//log4js.loadAppender('what/you/would/put/in/require'); |
24 |
| -//log4js.addAppender(log4js.appenders['what/you/would/put/in/require'](args)); |
25 |
| -//or through configure as: |
26 |
| -//log4js.configure({ |
27 |
| -// appenders: [ { type: 'what/you/would/put/in/require', otherArgs: 'blah' } ] |
28 |
| -//}); |
| 17 | +// a custom logger outside of the log4js/lib/appenders directory can be accessed like so |
| 18 | +// log4js.configure({ |
| 19 | +// appenders: { outside: { type: 'what/you/would/put/in/require', otherArgs: 'blah' } } |
| 20 | +// ... |
| 21 | +// }); |
29 | 22 |
|
30 |
| -var logger = log4js.getLogger('cheese'); |
31 |
| -//only errors and above get logged. |
32 |
| -//you can also set this log level in the config object |
33 |
| -//via the levels field. |
34 |
| -logger.setLevel('ERROR'); |
| 23 | +const logger = log4js.getLogger('cheese'); |
| 24 | +// only errors and above get logged. |
| 25 | +const otherLogger = log4js.getLogger(); |
35 | 26 |
|
36 |
| -//console logging methods have been replaced with log4js ones. |
37 |
| -//so this will get coloured output on console, and appear in cheese.log |
38 |
| -console.error("AAArgh! Something went wrong", { some: "otherObject", useful_for: "debug purposes" }); |
39 |
| -console.log("This should appear as info output"); |
| 27 | +// this will get coloured output on console, and appear in cheese.log |
| 28 | +otherLogger.error('AAArgh! Something went wrong', { some: 'otherObject', useful_for: 'debug purposes' }); |
| 29 | +otherLogger.log('This should appear as info output'); |
40 | 30 |
|
41 |
| -//these will not appear (logging level beneath error) |
| 31 | +// these will not appear (logging level beneath error) |
42 | 32 | logger.trace('Entering cheese testing');
|
43 | 33 | logger.debug('Got cheese.');
|
44 | 34 | logger.info('Cheese is Gouda.');
|
45 | 35 | logger.log('Something funny about cheese.');
|
46 | 36 | logger.warn('Cheese is quite smelly.');
|
47 |
| -//these end up on the console and in cheese.log |
48 |
| -logger.error('Cheese %s is too ripe!', "gouda"); |
| 37 | +// these end up only in cheese.log |
| 38 | +logger.error('Cheese %s is too ripe!', 'gouda'); |
49 | 39 | logger.fatal('Cheese was breeding ground for listeria.');
|
50 | 40 |
|
51 |
| -//these don't end up in cheese.log, but will appear on the console |
52 |
| -var anotherLogger = log4js.getLogger('another'); |
53 |
| -anotherLogger.debug("Just checking"); |
| 41 | +// these don't end up in cheese.log, but will appear on the console |
| 42 | +const anotherLogger = log4js.getLogger('another'); |
| 43 | +anotherLogger.debug('Just checking'); |
54 | 44 |
|
55 |
| -//one for pants.log |
56 |
| -//will also go to console, since that's configured for all categories |
57 |
| -var pantsLog = log4js.getLogger('pants'); |
58 |
| -pantsLog.debug("Something for pants"); |
| 45 | +// will also go to console and cheese.log, since that's configured for all categories |
| 46 | +const pantsLog = log4js.getLogger('pants'); |
| 47 | +pantsLog.debug('Something for pants'); |
0 commit comments