8000 Add close-on-SIGHUP behavior for logrotate. · xcj-coding/log4js-node@b7cf2f1 · GitHub
[go: up one dir, main page]

Skip to content

Commit b7cf2f1

Browse files
committed
Add close-on-SIGHUP behavior for logrotate.
1 parent 176d448 commit b7cf2f1

File tree

1 file changed

+46
-29
lines changed

1 file changed

+46
-29
lines changed

lib/appenders/file.js

Lines changed: 46 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,16 @@ var layouts = require('../layouts')
1111
//close open files on process exit.
1212
process.on('exit', function() {
1313
openFiles.forEach(function (file) {
14-
file.end();
14+
file.stream.end();
15+
});
16+
});
17+
18+
// On SIGHUP, close and reopen all files. This allows this appender to work with
19+
// logrotate. Note that if you are using logrotate, you should not set
20+
// `logSize`.
21+
process.on('SIGHUP', function() {
22+
openFiles.forEach(function(writer) {
23+
writer.reopen();
1524
});
1625
});
1726

@@ -34,38 +43,45 @@ function fileAppender (file, layout, logSize, numBackups) {
3443
//there has to be at least one backup if logSize has been specified
3544
numBackups = numBackups === 0 ? 1 : numBackups;
3645

37-
function openTheStream(file, fileSize, numFiles) {
38-
var stream;
39-
if (fileSize) {
40-
stream = new streams.RollingFileStream(
41-
file,
42-
fileSize,
43-
numFiles
44-
);
45-
} else {
46-
stream = fs.createWriteStream(
47-
file,
48-
{ encoding: "utf8",
49-
mode: parseInt('0644', 8),
50-
flags: 'a' }
51-
);
46+
var writer = {
47+
stream: openTheStream(file, logSize, numBackups),
48+
reopen: function() {
49+
this.stream.end();
50+
this.stream = openTheStream(file, logSize, numBackups);
5251
}
53-
stream.on("error", function (err) {
54-
console.error("log4js.fileAppender - Writing to file %s, error happened ", file, err);
55-
});
56-
return stream;
5752
}
5853

59-
var logFile = openTheStream(file, logSize, numBackups);
60-
6154
// push file to the stack of open handlers
62-
openFiles.push(logFile);
63-
55+
openFiles.push(writer);
56+
6457
return function(loggingEvent) {
65-
logFile.write(layout(loggingEvent) + eol, "utf8");
58+
writer.stream.write(layout(loggingEvent) + eol, "utf8");
6659
};
6760
}
6861

62+
function openTheStream(file, fileSize, numFiles) {
63+
var stream;
64+
if (fileSize) {
65+
stream = new streams.RollingFileStream(
66+
file,
67+
fileSize,
68+
numFiles
69+
);
70+
} else {
71+
stream = fs.createWriteStream(
72+
file,
73+
{ encoding: "utf8",
74+
mode: parseInt('0644', 8),
75+
flags: 'a' }
76+
);
77+
}
78+
stream.on("error", function (err) {
79+
console.error("log4js.fileAppender - Writing to file %s, error happened ", file, err);
80+
});
81+
return stream;
82+
}
83+
84+
6985
function configure(config, options) {
7086
var layout;
7187
if (config.layout) {
@@ -81,12 +97,13 @@ function configure(config, options) {
8197

8298
function shutdown(cb) {
8399
async.each(openFiles, function(file, done) {
84-
if (!file.write(eol, "utf-8")) {
85-
file.once('drain', function() {
86-
file.end(done);
100+
var stream = file.stream;
101+
if (!stream.write(eol, "utf-8")) {
102+
stream.once('drain', function() {
103+
stream.end(done);
87104
});
88105
} else {
89-
file.end(done);
106+
stream.end(done);
90107
}
91108
}, cb);
92109
}

0 commit comments

Comments
 (0)
0