@@ -79,6 +79,7 @@ module.exports = class File extends TransportStream {
79
79
this . maxFiles = options . maxFiles || null ;
80
80
this . eol = ( typeof options . eol === 'string' ) ? options . eol : os . EOL ;
81
81
this . tailable = options . tailable || false ;
82
+ this . lazy = options . lazy || false ;
82
83
83
84
// Internal state variables representing the number of files this instance
84
85
// has created and the current size (in bytes) of the current logfile.
@@ -88,9 +89,10 @@ module.exports = class File extends TransportStream {
88
89
this . _drain = false ;
89
90
this . _opening = false ;
90
91
this . _ending = false ;
92
+ this . _fileExist = false ;
91
93
92
94
if ( this . dirname ) this . _createLogDirIfNotExist ( this . dirname ) ;
93
- this . open ( ) ;
95
+ if ( ! this . lazy ) this . open ( ) ;
94
96
}
95
97
96
98
finishIfEnding ( ) {
@@ -107,14 +109,13 @@ module.exports = class File extends TransportStream {
107
109
}
108
110
}
109
111
110
-
111
112
/**
112
113
* Core logging method exposed to Winston. Metadata is optional.
113
114
* @param {Object } info - TODO: add param description.
114
115
* @param {Function } callback - TODO: add param description.
115
116
* @returns {undefined }
116
117
*/
117
- log ( info , callback = ( ) => { } ) {
118
+ log ( info , callback = ( ) => { } ) {
118
119
// Remark: (jcrugzz) What is necessary about this callback(null, true) now
119
120
// when thinking about 3.x? Should silent be handled in the base
120
121
// TransportStream _write method?
@@ -123,6 +124,7 @@ module.exports = class File extends TransportStream {
123
124
return true ;
124
125
}
125
126
127
+
126
128
// Output stream buffer is full and has asked us to wait for the drain event
127
129
if ( this . _drain ) {
128
130
this . _stream . once ( 'drain' , ( ) => {
@@ -138,6 +140,32 @@ module.exports = class File extends TransportStream {
138
140
} ) ;
139
141
return ;
140
142
}
143
+ if ( this . lazy ) {
144
+ if ( ! this . _fileExist ) {
145
+ if ( ! this . _opening ) {
146
+ this . open ( ) ;
147
+ }
148
+ this . once ( 'open' , ( ) => {
149
+ this . _fileExist = true ;
150
+ this . log ( info , callback ) ;
151
+ return ;
152
+ } ) ;
153
+ return ;
154
+ }
155
+ if ( this . _needsNewFile ( this . _pendingSize ) ) {
156
+ this . _dest . once ( 'close' , ( ) => {
157
+ if ( ! this . _opening ) {
158
+ this . open ( ) ;
159
+ }
160
+ this . once ( 'open' , ( ) => {
161
+ this . log ( info , callback ) ;
162
+ return ;
163
+ } ) ;
164
+ return ;
165
+ } ) ;
166
+ return ;
167
+ }
168
+ }
141
169
142
170
// Grab the raw string and append the expected EOL.
143
171
const output = `${ info [ MESSAGE ] } ${ this . eol } ` ;
@@ -169,6 +197,10 @@ module.exports = class File extends TransportStream {
169
197
if ( ! this . _needsNewFile ( ) ) {
170
198
return ;
171
199
}
200
+ if ( this . lazy ) {
201
+ this . _endStream ( ( ) => { this . emit ( 'fileclosed' ) } ) ;
202
+ return ;
203
+ }
172
204
173
205
// End the current stream, ensure it flushes and create a new one.
174
206
// This could potentially be optimized to not run a stat call but its
@@ -508,7 +540,6 @@ module.exports = class File extends TransportStream {
508
540
_cleanupStream ( stream ) {
509
541
stream . removeListener ( 'error' , this . _onError ) ;
510
542
stream . destroy ( ) ;
511
-
512
543
return stream ;
513
544
}
514
545
@@ -526,7 +557,7 @@ module.exports = class File extends TransportStream {
526
557
* @param {function } callback - Callback for when the current file has closed.
527
558
* @private
528
559
*/
529
- _endStream ( callback = ( ) => { } ) {
560
+ _endStream ( callback = ( ) => { } ) {
530
561
if ( this . _dest ) {
531
562
this . _stream . unpipe ( this . _dest ) ;
532
563
this . _dest . end ( ( ) => {
@@ -542,7 +573,7 @@ module.exports = class File extends TransportStream {
542
573
* Returns the WritableStream for the active file on this instance. If we
543
574
* should gzip the file then a zlib stream is returned.
544
575
*
545
- * @param {ReadableStream } source – PassThrough to pipe to the file when open.
576
+ * @param {ReadableStream } source –PassThrough to pipe to the file when open.
546
577
* @returns {WritableStream } Stream that writes to disk for the active file.
547
578
*/
548
579
_createStream ( source ) {
0 commit comments