@@ -11,6 +11,7 @@ var events = require('events'),
11
11
path = require ( 'path' ) ,
12
12
util = require ( 'util' ) ,
13
13
async = require ( 'async' ) ,
14
+ zlib = require ( 'zlib' ) ,
14
15
common = require ( '../common' ) ,
15
16
Transport = require ( './transport' ) . Transport ,
16
17
isWritable = require ( 'isstream' ) . isWritable ,
@@ -75,6 +76,7 @@ var File = exports.File = function (options) {
75
76
this . colorize = options . colorize || false ;
76
77
this . maxsize = options . maxsize || null ;
77
78
this . rotationFormat = options . rotationFormat || false ;
79
+ this . zippedArchive = options . zippedArchive || false ;
78
80
this . maxFiles = options . maxFiles || null ;
79
81
this . prettyPrint = options . prettyPrint || false ;
80
82
this . label = options . label || null ;
@@ -100,6 +102,7 @@ var File = exports.File = function (options) {
100
102
this . _draining = false ;
101
103
this . _opening = false ;
102
104
this . _failures = 0 ;
105
+ this . _archive = null ;
103
106
} ;
104
107
105
108
//
@@ -371,6 +374,8 @@ File.prototype.open = function (callback) {
371
374
return this . _createStream ( ) ;
372
375
}
373
376
377
+ this . _archive = this . zippedArchive ? this . _stream . path : null ;
378
+
374
379
//
375
380
// Otherwise we have a valid (and ready) stream.
376
381
//
@@ -496,22 +501,35 @@ File.prototype._createStream = function () {
496
501
self . opening = false ;
497
502
self . emit ( 'open' , fullname ) ;
498
503
} ) ;
499
-
500
504
//
501
505
// Remark: It is possible that in the time it has taken to find the
502
506
// next logfile to be written more data than `maxsize` has been buffered,
503
507
// but for sensible limits (10s - 100s of MB) this seems unlikely in less
504
508
// than one second.
505
509
//
506
510
self . flush ( ) ;
511
+ compressFile ( ) ;
512
+ }
513
+
514
+ function compressFile ( ) {
515
+ if ( self . _archive ) {
516
+ var gzip = zlib . createGzip ( ) ;
517
+
518
+ var inp = fs . createReadStream ( String ( self . _archive ) ) ;
519
+ var out = fs . createWriteStream ( self . _archive + '.gz' ) ;
520
+
521
+ inp . pipe ( gzip ) . pipe ( out ) ;
522
+
523
+ fs . unlink ( String ( self . _archive ) ) ;
524
+ self . _archive = '' ;
525
+ }
507
526
}
508
527
509
528
fs . stat ( fullname , function ( err , stats ) {
510
529
if ( err ) {
511
530
if ( err . code !== 'ENOENT' ) {
512
531
return self . emit ( 'error' , err ) ;
513
532
}
514
-
515
533
return createAndFlush ( 0 ) ;
516
534
}
517
535
@@ -530,6 +548,7 @@ File.prototype._createStream = function () {
530
548
} ) ( this . _getFile ( ) ) ;
531
549
} ;
532
550
551
+
533
552
File . prototype . _incFile = function ( callback ) {
534
553
var ext = path . extname ( this . _basename ) ,
535
554
basename = path . basename ( this . _basename , ext ) ,
@@ -570,15 +589,24 @@ File.prototype._getFile = function () {
570
589
// checked by this instance.
571
590
//
572
591
File . prototype . _checkMaxFilesIncrementing = function ( ext , basename , callback ) {
573
- var oldest , target ;
592
+ var oldest , target ,
593
+ self = this ;
594
+
595
+ if ( self . zippedArchive ) {
596
+ self . _archive = path . join ( self . dirname , basename +
597
+ ( ( self . _created === 1 ) ? '' : self . _created - 1 ) +
598
+ ext ) ;
599
+ }
600
+
574
601
575
602
// Check for maxFiles option and delete file
576
- if ( ! this . maxFiles || this . _created < this . maxFiles ) {
603
+ if ( ! self . maxFiles || self . _created < self . maxFiles ) {
577
604
return callback ( ) ;
578
605
}
579
606
580
- oldest = this . _created - this . maxFiles ;
581
- target = path . join ( this . dirname , basename + ( oldest !== 0 ? oldest : '' ) + ext ) ;
607
+ oldest = self . _created - self . maxFiles ;
608
+ target = path . join ( self . dirname , basename + ( oldest !== 0 ? oldest : '' ) + ext +
609
+ ( self . zippedArchive ? '.gz' : '' ) ) ;
582
610
fs . unlink ( target , callback ) ;
583
611
} ;
584
612
@@ -600,18 +628,23 @@ File.prototype._checkMaxFilesTailable = function (ext, basename, callback) {
600
628
for ( var x = this . maxFiles - 1 ; x > 0 ; x -- ) {
601
629
tasks . push ( function ( i ) {
602
630
return function ( cb ) {
603
- var tmppath = path . join ( self . dirname , basename + ( i - 1 ) + ext ) ;
631
+ var tmppath = path . join ( self . dirname , basename + ( i - 1 ) + ext +
632
+ ( self . zippedArchive ? '.gz' : '' ) ) ;
604
633
fs . exists ( tmppath , function ( exists ) {
605
634
if ( ! exists ) {
606
635
return cb ( null ) ;
607
636
}
608
637
609
- fs . rename ( tmppath , path . join ( self . dirname , basename + i + ext ) , cb ) ;
638
+ fs . rename ( tmppath , path . join ( self . dirname , basename + i + ext +
639
+ ( self . zippedArchive ? '.gz' : '' ) ) , cb ) ;
610
640
} ) ;
611
641
} ;
612
642
} ( x ) ) ;
613
643
}
614
644
645
+ if ( self . zippedArchive ) {
646
+ self . _archive = path . join ( self . dirname , basename + 1 + ext ) ;
647
+ }
615
648
async . series ( tasks , function ( err ) {
616
649
fs . rename (
617
650
path . join ( self . dirname , basename + ext ) ,
0 commit comments