8000 added test for SIGHUP handler · mKoder/log4js-node@281386a · GitHub
[go: up one dir, main page]

Skip to content

Commit 281386a

Browse files
author
Gareth Jones
committed
added test for SIGHUP handler
1 parent 7f07816 commit 281386a

File tree

2 files changed

+44
-12
lines changed

2 files changed

+44
-12
lines changed

lib/appenders/file.js

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,19 @@ var debug = require('debug')('log4js:file')
1111

1212
//close open files on process exit.
1313
process.on('exit', function() {
14+
debug('Exit handler called.');
1415
openFiles.forEach(function (file) {
15-
file.stream.end();
16+
file.end();
1617
});
1718
});
1819

1920
// On SIGHUP, close and reopen all files. This allows this appender to work with
2021
// logrotate. Note that if you are using logrotate, you should not set
2122
// `logSize`.
2223
process.on('SIGHUP', function() {
24+
debug('SIGHUP handler called.');
2325
openFiles.forEach(function(writer) {
24-
writer.reopen();
26+
writer.closeTheStream(writer.openTheStream.bind(writer));
2527
});
2628
});
2729

@@ -39,27 +41,20 @@ process.on('SIGHUP', function() {
3941
* @param timezoneOffset - optional timezone offset in minutes (default system local)
4042
*/
4143
function fileAppender (file, layout, logSize, numBackups, options, timezoneOffset) {
42-
var bytesWritten = 0;
4344
file = path.normalize(file);
4445
layout = layout || layouts.basicLayout;
4546
numBackups = numBackups === undefined ? 5 : numBackups;
4647
//there has to be at least one backup if logSize has been specified
4748
numBackups = numBackups === 0 ? 1 : numBackups;
4849

4950
debug("Creating file appender (", file, ", ", logSize, ", ", numBackups, ", ", options, ")");
50-
var writer = {
51-
stream: openTheStream(file, logSize, numBackups, options),
52-
reopen: function() {
53-
this.stream.end();
54-
this.stream = openTheStream(file, logSize, numBackups, options);
55-
}
56-
};
51+
var writer = openTheStream(file, logSize, numBackups, 10000 options);
5752

5853
// push file to the stack of open handlers
5954
openFiles.push(writer);
6055

6156
return function(loggingEvent) {
62-
writer.stream.write(layout(loggingEvent, timezoneOffset) + eol, "utf8");
57+
writer.write(layout(loggingEvent, timezoneOffset) + eol, "utf8");
6358
};
6459

6560
}
@@ -112,7 +107,7 @@ function shutdown(cb) {
112107
return cb();
113108
}
114109
openFiles.forEach(function(file) {
115-
var stream = file.stream;
110+
var stream = file;
116111
if (!stream.write(eol, "utf-8")) {
117112
stream.once('drain', function() {
118113
stream.end(complete);

test/tape/file-sighup-test.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
"use strict";
2+
var test = require('tape')
3+
, sandbox = require('sandboxed-module');
4+
5+
test('file appender SIGHUP', function(t) {
6+
var closeCalled = 0
7+
, openCalled = 0
8+
, appender = sandbox.require(
9+
'../../lib/appenders/file',
10+
{
11+
'requires': {
12+
'streamroller': {
13+
'RollingFileStream': function() {
14+
this.openTheStream = function() {
15+
openCalled++;
16+
};
17+
18+
this.closeTheStream = function(cb) {
19+
closeCalled++;
20+
cb();
21+
};
22+
23+
this.on = function() {};
24+
}
25+
}
26+
}
27+
}
28+
).appender('sighup-test-file');
29+
30+
process.kill(process.pid, 'SIGHUP');
31+
t.plan(2);
32+
setTimeout(function() {
33+
t.equal(openCalled, 1, 'open should be called once');
34+
t.equal(closeCalled, 1, 'close should be called once');
35+
t.end();
36+
}, 10);
37+
});

0 commit comments

Comments
 (0)
0