source: trip-planner-front/node_modules/log4js/lib/appenders/multiFile.js

Last change on this file was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 2.3 KB
Line 
1
2
3const debug = require('debug')('log4js:multiFile');
4const path = require('path');
5const fileAppender = require('./file');
6
7const findFileKey = (property, event) => event[property] || event.context[property];
8
9module.exports.configure = (config, layouts) => {
10 debug('Creating a multi-file appender');
11 const files = new Map();
12 const timers = new Map();
13
14 function checkForTimeout(fileKey) {
15 const timer = timers.get(fileKey);
16 const app = files.get(fileKey);
17 if (timer && app) {
18 if (Date.now() - timer.lastUsed > timer.timeout) {
19 debug('%s not used for > %d ms => close', fileKey, timer.timeout);
20 clearInterval(timer.interval);
21 timers.delete(fileKey);
22 files.delete(fileKey);
23 app.shutdown((err) => {
24 if (err) {
25 debug('ignore error on file shutdown: %s', err.message);
26 }
27 });
28 }
29 }
30 }
31
32 const appender = (logEvent) => {
33 const fileKey = findFileKey(config.property, logEvent);
34 debug('fileKey for property ', config.property, ' is ', fileKey);
35 if (fileKey) {
36 let file = files.get(fileKey);
37 debug('existing file appender is ', file);
38 if (!file) {
39 debug('creating new file appender');
40 config.filename = path.join(config.base, fileKey + config.extension);
41 file = fileAppender.configure(config, layouts);
42 files.set(fileKey, file);
43 if (config.timeout) {
44 debug('creating new timer');
45 timers.set(fileKey, {
46 timeout: config.timeout,
47 lastUsed: Date.now(),
48 interval: setInterval(checkForTimeout.bind(null, fileKey), config.timeout)
49 });
50 }
51 } else if (config.timeout) {
52 timers.get(fileKey).lastUsed = Date.now();
53 }
54
55 file(logEvent);
56 } else {
57 debug('No fileKey for logEvent, quietly ignoring this log event');
58 }
59 };
60
61 appender.shutdown = (cb) => {
62 let shutdownFunctions = files.size;
63 if (shutdownFunctions <= 0) {
64 cb();
65 }
66 let error;
67 timers.forEach((timer) => {
68 clearInterval(timer.interval);
69 });
70 files.forEach((app, fileKey) => {
71 debug('calling shutdown for ', fileKey);
72 app.shutdown((err) => {
73 error = error || err;
74 shutdownFunctions -= 1;
75 if (shutdownFunctions <= 0) {
76 cb(error);
77 }
78 });
79 });
80 };
81
82 return appender;
83};
Note: See TracBrowser for help on using the repository browser.