8000 Merge pull request #459 from nomiddlename/version-2 · mKoder/log4js-node@406084a · GitHub
[go: up one dir, main page]

Skip to content

Commit 406084a

Browse files
authored
Merge pull request log4js-node#459 from nomiddlename/version-2
Version 2
2 parents 552be9a + fd9f138 commit 406084a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

70 files changed

+2510
-3576
lines changed

lib/appenders/categoryFilter.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
'use strict';
22

3-
const log4js = require('../log4js');
4-
53
function categoryFilter(excludes, appender) {
64
if (typeof excludes === 'string') excludes = [excludes];
75
return (logEvent) => {
@@ -11,11 +9,9 @@ function categoryFilter(excludes, appender) {
119
};
1210
}
1311

14-
function configure(config, options) {
15-
log4js.loadAppender(config.appender.type);
16-
const appender = log4js.appenderMakers[config.appender.type](config.appender, options);
12+
function configure(config, layouts, findAppender) {
13+
const appender = findAppender(config.appender);
1714
return categoryFilter(config.exclude, appender);
1815
}
1916

20-
module.exports.appender = categoryFilter;
2117
module.exports.configure = configure;

lib/appenders/clustered.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ function deserializeLoggingEvent(loggingEventString) {
3939
try {
4040
loggingEvent = JSON.parse(loggingEventString);
4141
loggingEvent.startTime = new Date(loggingEvent.startTime);
42-
loggingEvent.level = log4js.levels.toLevel(loggingEvent.level.levelStr);
42+
loggingEvent.level = log4js.levels.getLevel(loggingEvent.level.levelStr);
4343
// Unwrap serialized errors
4444
for (let i = 0; i < loggingEvent.data.length; i++) {
4545
const item = loggingEvent.data[i];

lib/appenders/console.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,19 @@
11
'use strict';
22

3-
const layouts = require('../layouts');
4-
53
const consoleLog = console.log.bind(console);
64

75
function consoleAppender(layout, timezoneOffset) {
8-
layout = layout || layouts.colouredLayout;
96
return (loggingEvent) => {
107
consoleLog(layout(loggingEvent, timezoneOffset));
118
};
129
}
1310

14-
function configure(config) {
15-
let layout;
11+
function configure(config, layouts) {
12+
let layout = layouts.colouredLayout;
1613
if (config.layout) {
1714
layout = layouts.layout(config.layout.type, config.layout);
1815
}
1916
return consoleAppender(layout, config.timezoneOffset);
2017
}
2118

22-
module.exports.appender = consoleAppender;
2319
module.exports.configure = configure;

lib/appenders/dateFile.js

Lines changed: 11 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,9 @@
11
'use strict';
22

33
const streams = require('streamroller');
4-
const layouts = require('../layouts');
5-
const path = require('path');
64
const os = require('os');
75

86
const eol = os.EOL || '\n';
9-
const openFiles = [];
10-
11-
// close open files on process exit.
12-
process.on('exit', () => {
13-
openFiles.forEach((file) => {
14-
file.end();
15-
});
16-
});
177

188
/**
199
* File appender that rolls files according to a date pattern.
@@ -30,21 +20,27 @@ function appender(
3020
options,
3121
timezoneOffset
3222
) {
33-
layout = layout || layouts.basicLayout;
3423
const logFile = new streams.DateRollingFileStream(
3524
filename,
3625
pattern,
3726
options
3827
);
39-
openFiles.push(logFile);
4028

41-
return (logEvent) => {
29+
const app = function (logEvent) {
4230
logFile.write(layout(logEvent, timezoneOffset) + eol, 'utf8');
4331
};
32+
33+
app.shutdown = function (complete) {
34+
logFile.write('', 'utf-8', () => {
35+
logFile.end(complete);
36+
});
37+
};
38+
39+
return app;
4440
}
4541

46-
function configure(config, options) {
47-
let layout;
42+
function configure(config, layouts) {
43+
let layout = layouts.basicLayout;
4844

4945
if (config.layout) {
5046
layout = layouts.layout(config.layout.type, config.layout);
@@ -54,10 +50,6 @@ function configure(config, options) {
5450
config.alwaysIncludePattern = false;
5551
}
5652

57-
if (options && options.cwd && !config.absolute) {
58-
config.filename = path.join(options.cwd, config.filename);
59-
}
60-
6153
return appender(
6254
config.filename,
6355
config.pattern,
@@ -67,27 +59,4 @@ function configure(config, options) {
6759
);
6860
}
6961

70-
function shutdown(cb) {
71-
let completed = 0;
72-
let error;
73-
const complete = (err) => {
74-
error = error || err;
75-
completed++; // eslint-disable-line no-plusplus
76-
if (completed >= openFiles.length) {
77-
cb(error);
78-
}
79-
};
80-
if (!openFiles.length) {
81-
return cb();
82-
}
83-
84-
return openFiles.forEach((file) => {
85-
file.write('', 'utf-8', () => {
86-
file.end(complete);
87-
});
88-
});
89-
}
90-
91-
module.exports.appender = appender;
9262
module.exports.configure = configure;
93-
module.exports.shutdown = shutdown;

lib/appenders/file.js

Lines changed: 33 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,25 @@
11
'use strict';
22

33
const debug = require('debug')('log4js:file');
4-
const layouts = require('../layouts');
54
const path = require('path');
65
const streams = require('streamroller');
76
const os = require('os');
87

98
const eol = os.EOL || '\n';
10-
const openFiles = [];
119

12-
// close open files on process exit.
13-
process.on('exit', () => {
14-
debug('Exit handler called.');
15-
openFiles.forEach((file) => {
16-
file.end();
10+
function openTheStream(file, fileSize, numFiles, options) {
11+
const stream = new streams.RollingFileStream(
12+
file,
13+
fileSize,
14+
numFiles,
15+
options
16+
);
17+
stream.on('error', (err) => {
18+
console.error('log4js.fileAppender - Writing to file %s, error happened ', file, err); //eslint-disable-line
1719
});
18-
});
20+
return stream;
21+
}
1922

20-
// On SIGHUP, close and reopen all files. This allows this appender to work with
21-
// logrotate. Note that if you are using logrotate, you should not set
22-
// `logSize`.
23-
process.on('SIGHUP', () => {
24-
debug('SIGHUP handler called.');
25-
openFiles.forEach((writer) => {
26-
writer.closeTheStream(writer.openTheStream.bind(writer));
27-
});
28-
});
2923

3024
/**
3125
* File Appender writing the logs to a text file. Supports rolling of logs by size.
@@ -42,7 +36,6 @@ process.on('SIGHUP', () => {
4236
*/
4337
function fileAppender(file, layout, logSize, numBackups, options, timezoneOffset) {
4438
file = path.normalize(file);
45-
layout = layout || layouts.basicLayout;
4639
numBackups = numBackups === undefined ? 5 : numBackups;
4740
// there has to be at least one backup if logSize has been specified
4841
numBackups = numBackups === 0 ? 1 : numBackups;
@@ -54,40 +47,40 @@ function fileAppender(file, layout, logSize, numBackups, options, timezoneOffset
5447
options, ', ',
5548
timezoneOffset, ')'
5649
);
57-
const writer = openTheStream(file, logSize, numBackups, options);
5850

59-
// push file to the stack of open handlers
60-
openFiles.push(writer);
51+
const writer = openTheStream(file, logSize, numBackups, options);
6152

62-
return function (loggingEvent) {
53+
const app = function (loggingEvent) {
6354
writer.write(layout(loggingEvent, timezoneOffset) + eol, 'utf8');
6455
};
65-
}
6656

67-
function openTheStream(file, fileSize, numFiles, options) {
68-
const stream = new streams.RollingFileStream(
69-
file,
70-
fileSize,
71-
numFiles,
72-
options
73-
);
74-
stream.on('error', (err) => {
75-
console.error('log4js.fileAppender - Writing to file %s, error happened ', file, err);
57+
app.reopen = function () {
58+
writer.closeTheStream(writer.openTheStream.bind(writer));
59+
};
60+
61+
app.shutdown = function (complete) {
62+
writer.write('', 'utf-8', () => {
63+
writer.end(complete);
64+
});
65+
};
66+
67+
// On SIGHUP, close and reopen all files. This allows this appender to work with
68+
// logrotate. Note that if you are using logrotate, you should not set
69+
// `logSize`.
70+
process.on('SIGHUP', () => {
71+
debug('SIGHUP handler called.');
72+
app.reopen();
7673
});
77-
return stream;
78-
}
7974

75+
return app;
76+
}
8077

81-
function configure(config, options) {
82-
let layout;
78+
function configure(config, layouts) {
79+
let layout = layouts.basicLayout;
8380
if (config.layout) {
8481
layout = layouts.layout(config.layout.type, config.layout);
8582
}
8683

87-
if (options && options.cwd && !config.absolute) {
88-
config.filename = path.join(options.cwd, config.filename);
89-
}
90-
9184
return fileAppender(
9285
config.filename,
9386
layout,
@@ -98,27 +91,4 @@ function configure(config, options) {
9891
);
9992
}
10093

101-
function shutdown(cb) {
102-
let completed = 0;
103-
let error;
104-
const complete = (err) => {
105-
error = error || err;
106-
completed++; // eslint-disable-line no-plusplus
107-
if (completed >= openFiles.length) {
108-
cb(error);
109-
}
110-
};
111-
if (!openFiles.length) {
112-
return cb();
113-
}
114-
115-
return openFiles.forEach((file) => {
116-
file.write('', 'utf-8', () => {
117-
file.end(complete);
118-
});
119-
});
120-
}
121-
122-
module.exports.appender = fileAppender;
12394
module.exports.configure = configure;
124-
module.exports.shutdown = shutdown;

lib/appenders/fileSync.js

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
'use strict';
22

33
const debug = require('debug')('log4js:fileSync');
4-
const layouts = require('../layouts');
54
const path = require('path');
65
const fs = require('fs');
76
const os = require('os');
@@ -135,7 +134,6 @@ class RollingFileSync {
135134
function fileAppender(file, layout, logSize, numBackups, 4E34 timezoneOffset) {
136135
debug('fileSync appender created');
137136
file = path.normalize(file);
138-
layout = layout || layouts.basicLayout;
139137
numBackups = numBackups === undefined ? 5 : numBackups;
140138
// there has to be at least one backup if logSize has been specified
141139
numBackups = numBackups === 0 ? 1 : numBackups;
@@ -174,16 +172,12 @@ function fileAppender(file, layout, logSize, numBackups, timezoneOffset) {
174172
};
175173
}
176174

177-
function configure(config, options) {
178-
let layout;
175+
function configure(config, layouts) {
176+
let layout = layouts.basicLayout;
179177
if (config.layout) {
180178
layout = layouts.layout(config.layout.type, config.layout);
181179
}
182180

183-
if (options && options.cwd && !config.absolute) {
184-
config.filename = path.join(options.cwd, config.filename);
185-
}
186-
187181
return fileAppender(
188182
config.filename,
189183
layout,
@@ -193,5 +187,4 @@ function configure(config, options) {
193187
);
194188
}
195189

196-
module.exports.appender = fileAppender;
197190
module.exports.configure = configure;

0 commit comments

Comments
 (0)
0