1 | const streams = require('streamroller');
|
---|
2 | const os = require('os');
|
---|
3 |
|
---|
4 | const eol = os.EOL;
|
---|
5 |
|
---|
6 | /**
|
---|
7 | * File appender that rolls files according to a date pattern.
|
---|
8 | * @filename base filename.
|
---|
9 | * @pattern the format that will be added to the end of filename when rolling,
|
---|
10 | * also used to check when to roll files - defaults to '.yyyy-MM-dd'
|
---|
11 | * @layout layout function for log messages - defaults to basicLayout
|
---|
12 | * @timezoneOffset optional timezone offset in minutes - defaults to system local
|
---|
13 | */
|
---|
14 | function appender(
|
---|
15 | filename,
|
---|
16 | pattern,
|
---|
17 | layout,
|
---|
18 | options,
|
---|
19 | timezoneOffset
|
---|
20 | ) {
|
---|
21 | // the options for file appender use maxLogSize, but the docs say any file appender
|
---|
22 | // options should work for dateFile as well.
|
---|
23 | options.maxSize = options.maxLogSize;
|
---|
24 |
|
---|
25 | const logFile = new streams.DateRollingFileStream(
|
---|
26 | filename,
|
---|
27 | pattern,
|
---|
28 | options
|
---|
29 | );
|
---|
30 |
|
---|
31 | logFile.on("drain", () => {
|
---|
32 | process.emit("log4js:pause", false);
|
---|
33 | });
|
---|
34 |
|
---|
35 | const app = function (logEvent) {
|
---|
36 | if (!logFile.write(layout(logEvent, timezoneOffset) + eol, "utf8")) {
|
---|
37 | process.emit("log4js:pause", true);
|
---|
38 | }
|
---|
39 | };
|
---|
40 |
|
---|
41 | app.shutdown = function (complete) {
|
---|
42 | logFile.write('', 'utf-8', () => {
|
---|
43 | logFile.end(complete);
|
---|
44 | });
|
---|
45 | };
|
---|
46 |
|
---|
47 | return app;
|
---|
48 | }
|
---|
49 |
|
---|
50 | function configure(config, layouts) {
|
---|
51 | let layout = layouts.basicLayout;
|
---|
52 |
|
---|
53 | if (config.layout) {
|
---|
54 | layout = layouts.layout(config.layout.type, config.layout);
|
---|
55 | }
|
---|
56 |
|
---|
57 | if (!config.alwaysIncludePattern) {
|
---|
58 | config.alwaysIncludePattern = false;
|
---|
59 | }
|
---|
60 |
|
---|
61 | return appender(
|
---|
62 | config.filename,
|
---|
63 | config.pattern,
|
---|
64 | layout,
|
---|
65 | config,
|
---|
66 | config.timezoneOffset
|
---|
67 | );
|
---|
68 | }
|
---|
69 |
|
---|
70 | module.exports.configure = configure;
|
---|