source: trip-planner-front/node_modules/log4js/lib/appenders/file.js@ 188ee53

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

initial commit

  • Property mode set to 100644
File size: 3.0 KB
Line 
1const debug = require('debug')('log4js:file');
2const path = require('path');
3const streams = require('streamroller');
4const os = require('os');
5
6const eol = os.EOL;
7
8function openTheStream(file, fileSize, numFiles, options) {
9 const stream = new streams.RollingFileStream(
10 file,
11 fileSize,
12 numFiles,
13 options
14 );
15 stream.on('error', (err) => {
16 console.error('log4js.fileAppender - Writing to file %s, error happened ', file, err); //eslint-disable-line
17 });
18 stream.on('drain', () => {
19 process.emit("log4js:pause", false);
20 });
21 return stream;
22}
23
24
25/**
26 * File Appender writing the logs to a text file. Supports rolling of logs by size.
27 *
28 * @param file file log messages will be written to
29 * @param layout a function that takes a logEvent and returns a string
30 * (defaults to basicLayout).
31 * @param logSize - the maximum size (in bytes) for a log file,
32 * if not provided then logs won't be rotated.
33 * @param numBackups - the number of log files to keep after logSize
34 * has been reached (default 5)
35 * @param options - options to be passed to the underlying stream
36 * @param timezoneOffset - optional timezone offset in minutes (default system local)
37 */
38function fileAppender(file, layout, logSize, numBackups, options, timezoneOffset) {
39 file = path.normalize(file);
40 numBackups = numBackups === undefined ? 5 : numBackups;
41 // there has to be at least one backup if logSize has been specified
42 numBackups = numBackups === 0 ? 1 : numBackups;
43
44 debug(
45 'Creating file appender (',
46 file, ', ',
47 logSize, ', ',
48 numBackups, ', ',
49 options, ', ',
50 timezoneOffset, ')'
51 );
52
53 let writer = openTheStream(file, logSize, numBackups, options);
54
55 const app = function (loggingEvent) {
56 if (options.removeColor === true) {
57 // eslint-disable-next-line no-control-regex
58 const regex = /\x1b[[0-9;]*m/g;
59 loggingEvent.data = loggingEvent.data.map(d => {
60 if (typeof d === 'string') return d.replace(regex, '')
61 return d
62 })
63 }
64 if (!writer.write(layout(loggingEvent, timezoneOffset) + eol, "utf8")) {
65 process.emit('log4js:pause', true);
66 }
67 };
68
69 app.reopen = function () {
70 writer.end(() => { writer = openTheStream(file, logSize, numBackups, options); });
71 };
72
73 app.sighupHandler = function () {
74 debug('SIGHUP handler called.');
75 app.reopen();
76 };
77
78 app.shutdown = function (complete) {
79 process.removeListener('SIGHUP', app.sighupHandler);
80 writer.end('', 'utf-8', complete);
81 };
82
83 // On SIGHUP, close and reopen all files. This allows this appender to work with
84 // logrotate. Note that if you are using logrotate, you should not set
85 // `logSize`.
86 process.on('SIGHUP', app.sighupHandler);
87
88 return app;
89}
90
91function configure(config, layouts) {
92 let layout = layouts.basicLayout;
93 if (config.layout) {
94 layout = layouts.layout(config.layout.type, config.layout);
95 }
96
97 return fileAppender(
98 config.filename,
99 layout,
100 config.maxLogSize,
101 config.backups,
102 config,
103 config.timezoneOffset
104 );
105}
106
107module.exports.configure = configure;
Note: See TracBrowser for help on using the repository browser.