[6a3a178] | 1 | import * as log4js from './log4js';
|
---|
| 2 |
|
---|
| 3 | log4js.configure('./filename');
|
---|
| 4 | const logger1 = log4js.getLogger();
|
---|
| 5 | logger1.level = 'debug';
|
---|
| 6 | logger1.debug("Some debug messages");
|
---|
| 7 | logger1.fatal({
|
---|
| 8 | whatever: 'foo'
|
---|
| 9 | })
|
---|
| 10 |
|
---|
| 11 | const logger3 = log4js.getLogger('cheese');
|
---|
| 12 | logger3.trace('Entering cheese testing');
|
---|
| 13 | logger3.debug('Got cheese.');
|
---|
| 14 | logger3.info('Cheese is Gouda.');
|
---|
| 15 | logger3.warn('Cheese is quite smelly.');
|
---|
| 16 | logger3.error('Cheese is too ripe!');
|
---|
| 17 | logger3.fatal('Cheese was breeding ground for listeria.');
|
---|
| 18 |
|
---|
| 19 | log4js.configure({
|
---|
| 20 | appenders: { cheese: { type: 'console', filename: 'cheese.log' } },
|
---|
| 21 | categories: { default: { appenders: ['cheese'], level: 'error' } }
|
---|
| 22 | });
|
---|
| 23 |
|
---|
| 24 | log4js.configure({
|
---|
| 25 | appenders: {
|
---|
| 26 | out: { type: 'file', filename: 'pm2logs.log' }
|
---|
| 27 | },
|
---|
| 28 | categories: {
|
---|
| 29 | default: { appenders: ['out'], level: 'info' }
|
---|
| 30 | },
|
---|
| 31 | pm2: true,
|
---|
| 32 | pm2InstanceVar: 'INSTANCE_ID'
|
---|
| 33 | });
|
---|
| 34 |
|
---|
| 35 | log4js.addLayout('json', config => function (logEvent) {
|
---|
| 36 | return JSON.stringify(logEvent) + config.separator;
|
---|
| 37 | });
|
---|
| 38 |
|
---|
| 39 | log4js.configure({
|
---|
| 40 | appenders: {
|
---|
| 41 | out: { type: 'stdout', layout: { type: 'json', separator: ',' } }
|
---|
| 42 | },
|
---|
| 43 | categories: {
|
---|
| 44 | default: { appenders: ['out'], level: 'info' }
|
---|
| 45 | }
|
---|
| 46 | });
|
---|
| 47 |
|
---|
| 48 | log4js.configure({
|
---|
| 49 | appenders: {
|
---|
| 50 | file: { type: 'dateFile', filename: 'thing.log', pattern: '.mm' }
|
---|
| 51 | },
|
---|
| 52 | categories: {
|
---|
| 53 | default: { appenders: ['file'], level: 'debug' }
|
---|
| 54 | }
|
---|
| 55 | });
|
---|
| 56 |
|
---|
| 57 | const logger4 = log4js.getLogger('thing');
|
---|
| 58 | logger4.log('logging a thing');
|
---|
| 59 |
|
---|
| 60 | const logger5 = log4js.getLogger('json-test');
|
---|
| 61 | logger5.info('this is just a test');
|
---|
| 62 | logger5.error('of a custom appender');
|
---|
| 63 | logger5.warn('that outputs json');
|
---|
| 64 | log4js.shutdown();
|
---|
| 65 |
|
---|
| 66 | log4js.configure({
|
---|
| 67 | appenders: {
|
---|
| 68 | cheeseLogs: { type: 'file', filename: 'cheese.log' },
|
---|
| 69 | console: { type: 'console' }
|
---|
| 70 | },
|
---|
| 71 | categories: {
|
---|
| 72 | cheese: { appenders: ['cheeseLogs'], level: 'error' },
|
---|
| 73 | another: { appenders: ['console'], level: 'trace' },
|
---|
| 74 | default: { appenders: ['console', 'cheeseLogs'], level: 'trace' }
|
---|
| 75 | }
|
---|
| 76 | });
|
---|
| 77 |
|
---|
| 78 | const logger6 = log4js.getLogger('cheese');
|
---|
| 79 | // only errors and above get logged.
|
---|
| 80 | const otherLogger = log4js.getLogger();
|
---|
| 81 |
|
---|
| 82 | // this will get coloured output on console, and appear in cheese.log
|
---|
| 83 | otherLogger.error('AAArgh! Something went wrong', { some: 'otherObject', useful_for: 'debug purposes' });
|
---|
| 84 | otherLogger.log('This should appear as info output');
|
---|
| 85 |
|
---|
| 86 | // these will not appear (logging level beneath error)
|
---|
| 87 | logger6.trace('Entering cheese testing');
|
---|
| 88 | logger6.debug('Got cheese.');
|
---|
| 89 | logger6.info('Cheese is Gouda.');
|
---|
| 90 | logger6.log('Something funny about cheese.');
|
---|
| 91 | logger6.warn('Cheese is quite smelly.');
|
---|
| 92 | // these end up only in cheese.log
|
---|
| 93 | logger6.error('Cheese %s is too ripe!', 'gouda');
|
---|
| 94 | logger6.fatal('Cheese was breeding ground for listeria.');
|
---|
| 95 |
|
---|
| 96 | // these don't end up in cheese.log, but will appear on the console
|
---|
| 97 | const anotherLogger = log4js.getLogger('another');
|
---|
| 98 | anotherLogger.debug('Just checking');
|
---|
| 99 |
|
---|
| 100 | // will also go to console and cheese.log, since that's configured for all categories
|
---|
| 101 | const pantsLog = log4js.getLogger('pants');
|
---|
| 102 | pantsLog.debug('Something for pants');
|
---|
| 103 |
|
---|
| 104 |
|
---|
| 105 | import { configure, getLogger } from './log4js';
|
---|
| 106 | configure('./filename');
|
---|
| 107 | const logger2 = getLogger();
|
---|
| 108 | logger2.level = 'debug';
|
---|
| 109 | logger2.debug("Some debug messages");
|
---|
| 110 |
|
---|
| 111 | configure({
|
---|
| 112 | appenders: { cheese: { type: 'file', filename: 'cheese.log' } },
|
---|
| 113 | categories: { default: { appenders: ['cheese'], level: 'error' } }
|
---|
| 114 | });
|
---|
| 115 |
|
---|
| 116 | log4js.configure('./filename').getLogger();
|
---|
| 117 | const logger7 = log4js.getLogger();
|
---|
| 118 | logger7.level = 'debug';
|
---|
| 119 | logger7.debug("Some debug messages");
|
---|
| 120 |
|
---|
| 121 | const levels: log4js.Levels = log4js.levels;
|
---|
| 122 | const level: log4js.Level = levels.getLevel('info');
|
---|
| 123 |
|
---|
| 124 | log4js.connectLogger(logger1, {
|
---|
| 125 | format: ':x, :y',
|
---|
| 126 | level: 'info',
|
---|
| 127 | context: true
|
---|
| 128 | });
|
---|
| 129 |
|
---|
| 130 | log4js.connectLogger(logger2, {
|
---|
| 131 | format: (req, _res, format) => format(`:remote-addr - ${req.id} - ":method :url HTTP/:http-version" :status :content-length ":referrer" ":user-agent"`)
|
---|
| 132 | });
|
---|
| 133 |
|
---|
| 134 | //support for passing in an appender module
|
---|
| 135 | log4js.configure({
|
---|
| 136 | appenders: { thing: { type: { configure: () => {} }}},
|
---|
| 137 | categories: { default: { appenders: ['thing'], level: 'debug'}}
|
---|
| 138 | });
|
---|