@@ -79,6 +79,7 @@ module.exports = class File extends TransportStream {
7979 this . maxFiles = options . maxFiles || null ;
8080 this . eol = ( typeof options . eol === 'string' ) ? options . eol : os . EOL ;
8181 this . tailable = options . tailable || false ;
82+ this . lazy = options . lazy || false ;
8283
8384 // Internal state variables representing the number of files this instance
8485 // has created and the current size (in bytes) of the current logfile.
@@ -88,9 +89,10 @@ module.exports = class File extends TransportStream {
8889 this . _drain = false ;
8990 this . _opening = false ;
9091 this . _ending = false ;
92+ this . _fileExist = false ;
9193
9294 if ( this . dirname ) this . _createLogDirIfNotExist ( this . dirname ) ;
93- this . open ( ) ;
95+ if ( ! this . lazy ) this . open ( ) ;
9496 }
9597
9698 finishIfEnding ( ) {
@@ -107,14 +109,13 @@ module.exports = class File extends TransportStream {
107109 }
108110 }
109111
110-
111112 /**
112113 * Core logging method exposed to Winston. Metadata is optional.
113114 * @param {Object } info - TODO: add param description.
114115 * @param {Function } callback - TODO: add param description.
115116 * @returns {undefined }
116117 */
117- log ( info , callback = ( ) => { } ) {
118+ log ( info , callback = ( ) => { } ) {
118119 // Remark: (jcrugzz) What is necessary about this callback(null, true) now
119120 // when thinking about 3.x? Should silent be handled in the base
120121 // TransportStream _write method?
@@ -123,6 +124,7 @@ module.exports = class File extends TransportStream {
123124 return true ;
124125 }
125126
127+
126128 // Output stream buffer is full and has asked us to wait for the drain event
127129 if ( this . _drain ) {
128130 this . _stream . once ( 'drain' , ( ) => {
@@ -138,6 +140,32 @@ module.exports = class File extends TransportStream {
138140 } ) ;
139141 return ;
140142 }
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+ }
141169
142170 // Grab the raw string and append the expected EOL.
143171 const output = `${ info [ MESSAGE ] } ${ this . eol } ` ;
@@ -169,6 +197,10 @@ module.exports = class File extends TransportStream {
169197 if ( ! this . _needsNewFile ( ) ) {
170198 return ;
171199 }
200+ if ( this . lazy ) {
201+ this . _endStream ( ( ) => { this . emit ( 'fileclosed' ) } ) ;
202+ return ;
203+ }
172204
173205 // End the current stream, ensure it flushes and create a new one.
174206 // This could potentially be optimized to not run a stat call but its
@@ -508,7 +540,6 @@ module.exports = class File extends TransportStream {
508540 _cleanupStream ( stream ) {
509541 stream . removeListener ( 'error' , this . _onError ) ;
510542 stream . destroy ( ) ;
511-
512543 return stream ;
513544 }
514545
@@ -526,7 +557,7 @@ module.exports = class File extends TransportStream {
526557 * @param {function } callback - Callback for when the current file has closed.
527558 * @private
528559 */
529- _endStream ( callback = ( ) => { } ) {
560+ _endStream ( callback = ( ) => { } ) {
530561 if ( this . _dest ) {
531562 this . _stream . unpipe ( this . _dest ) ;
532563 this . _dest . end ( ( ) => {
@@ -542,7 +573,7 @@ module.exports = class File extends TransportStream {
542573 * Returns the WritableStream for the active file on this instance. If we
543574 * should gzip the file then a zlib stream is returned.
544575 *
545- * @param {ReadableStream } source – PassThrough to pipe to the file when open.
576+ * @param {ReadableStream } source –PassThrough to pipe to the file when open.
546577 * @returns {WritableStream } Stream that writes to disk for the active file.
547578 */
548579 _createStream ( source ) {
0 commit comments