UNPKG

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