@@ -11,7 +11,16 @@ var layouts = require('../layouts')
11
11
//close open files on process exit.
12
12
process . on ( 'exit' , function ( ) {
13
13
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 ( ) ;
15
24
} ) ;
16
25
} ) ;
17
26
@@ -34,38 +43,45 @@ function fileAppender (file, layout, logSize, numBackups) {
34
43
//there has to be at least one backup if logSize has been specified
35
44
numBackups = numBackups === 0 ? 1 : numBackups ;
36
45
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 ) ;
52
51
}
53
- stream . on ( "error" , function ( err ) {
54
- console . error ( "log4js.fileAppender - Writing to file %s, error happened " , file , err ) ;
55
- } ) ;
56
- return stream ;
57
52
}
58
53
59
- var logFile = openTheStream ( file , logSize , numBackups ) ;
60
-
61
54
// push file to the stack of open handlers
62
- openFiles . push ( logFile ) ;
63
-
55
+ openFiles . push ( writer ) ;
56
+
64
57
return function ( loggingEvent ) {
65
- logFile . write ( layout ( loggingEvent ) + eol , "utf8" ) ;
58
+ writer . stream . write ( layout ( loggingEvent ) + eol , "utf8" ) ;
66
59
} ;
67
60
}
68
61
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
+
69
85
function configure ( config , options ) {
70
86
var layout ;
71
87
if ( config . layout ) {
@@ -81,12 +97,13 @@ function configure(config, options) {
81
97
82
98
function shutdown ( cb ) {
83
99
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 ) ;
87
104
} ) ;
88
105
} else {
89
- file . end ( done ) ;
106
+ stream . end ( done ) ;
90
107
}
91
108
} , cb ) ;
92
109
}
0 commit comments