7
7
*/
8
8
9
9
/* eslint-disable no-sync */
10
+ const zlib = require ( 'zlib' ) ;
10
11
const assert = require ( 'assert' ) ;
11
12
const rimraf = require ( 'rimraf' ) ;
12
13
const fs = require ( 'fs' ) ;
@@ -25,6 +26,16 @@ function removeFixtures(done) {
25
26
26
27
let archiveTransport = null ;
27
28
29
+ function data ( ch , kb ) {
30
+ return String . fromCharCode ( 65 + ch ) . repeat ( kb * 1024 - 1 ) ;
31
+ }
32
+
33
+ function logKbytes ( kbytes , txt ) {
34
+ const toLog = { } ;
35
+ toLog [ MESSAGE ] = data ( txt , kbytes ) ;
36
+ archiveTransport . log ( toLog ) ;
37
+ }
38
+
28
39
describe ( 'winston/transports/file/zippedArchive' , function ( ) {
29
40
describe ( 'An instance of the File Transport with tailable true' , function ( ) {
30
41
before ( removeFixtures ) ;
@@ -47,16 +58,6 @@ describe('winston/transports/file/zippedArchive', function () {
47
58
let created = 0 ;
48
59
let loggedTotal = 0 ;
49
60
50
- function data ( ch , kb ) {
51
- return String . fromCharCode ( 65 + ch ) . repeat ( kb * 1024 - 1 ) ;
52
- }
53
-
54
- function logKbytes ( kbytes , txt ) {
55
- const toLog = { } ;
56
- toLog [ MESSAGE ] = data ( txt , kbytes ) ;
57
- archiveTransport . log ( toLog ) ;
58
- }
59
-
60
61
archiveTransport . on ( 'logged' , function ( info ) {
61
62
loggedTotal += info [ MESSAGE ] . length + 1 ;
62
63
if ( loggedTotal >= 14 * 1024 ) { // just over 3 x 4kb files
@@ -89,5 +90,107 @@ describe('winston/transports/file/zippedArchive', function () {
89
90
} , Error ) ;
90
91
}
91
92
} ) ;
93
+
94
+ it ( 'testarchive.log should be ascii, testarchive1.log.gz and testarchive2.log.gz should be zipped' , function ( ) {
95
+ const letters = [ 'D' , 'C' , 'B' ] ;
96
+
97
+ for ( var num = 0 ; num < 3 ; num ++ ) {
98
+ let content ;
99
+ const letter = letters [ num ] ;
100
+ const file = ! num ? 'testarchive.log' : 'testarchive' + num + '.log.gz' ;
101
+ const fileContent = fs . readFileSync ( path . join ( __dirname , '..' , 'fixtures' , 'logs' , file ) ) ;
102
+
103
+ if ( num > 0 ) {
104
+ // archives should be zipped
105
+ assert . doesNotThrow ( function ( ) {
106
+ content = zlib . gunzipSync ( fileContent ) . toString ( 'ascii' ) ;
107
+ } ) ;
108
+ } else {
109
+ // current log file should be plain text
110
+ content = fileContent . toString ( 'ascii' ) ;
111
+ }
112
+
113
+ assert ( content . match ( new RegExp ( letter , 'g' ) ) [ 0 ] . length , content . length ) ;
114
+ }
115
+ } ) ;
116
+ } ) ;
117
+
118
+ describe ( 'An instance of the File Transport with tailable false' , function ( ) {
119
+ before ( removeFixtures ) ;
120
+ after ( removeFixtures ) ;
121
+
122
+ it ( 'init logger AFTER cleaning up old files' , function ( ) {
123
+ archiveTransport = new winston . transports . File ( {
124
+ timestamp : true ,
125
+ json : false ,
126
+ zippedArchive : true ,
127
+ tailable : false ,
128
+ filename : 'testarchive.log' ,
129
+ dirname : path . join ( __dirname , '..' , 'fixtures' , 'logs' ) ,
130
+ maxsize : 4096 ,
131
+ maxFiles : 3
132
+ } ) ;
133
+ } ) ;
134
+
135
+ it ( 'when created archived files are rolled' , function ( done ) {
136
+
137
+ let created = 0 ;
138
+ let loggedTotal = 0 ;
139
+
140
+ archiveTransport . on ( 'logged' , function ( info ) {
141
+ loggedTotal += info [ MESSAGE ] . length + 1 ;
142
+ if ( loggedTotal >= 14 * 1024 ) { // just over 3 x 4kb files
143
+ return done ( ) ;
144
+ }
145
+
146
+ if ( loggedTotal % 4096 === 0 ) {
147
+ created ++ ;
148
+ }
149
+ // eslint-disable-next-line max-nested-callbacks
150
+ setTimeout ( ( ) => logKbytes ( 1 , created ) , 1 ) ;
151
+ } ) ;
152
+
153
+ logKbytes ( 1 , created ) ;
154
+ } ) ;
155
+
156
+ it ( 'should be only 3 files called testarchive3.log, testarchive2.log.gz and testarchive1.log.gz' , function ( ) {
157
+ for ( var num = 0 ; num < 4 ; num ++ ) {
158
+ const file = num === 3 ? 'testarchive3.log' : 'testarchive' + ( num > 0 ? num : '' ) + '.log.gz' ;
159
+ const fullpath = path . join ( __dirname , '..' , 'fixtures' , 'logs' , file ) ;
160
+
161
+ const statFile = function ( ) {
162
+ fs . statSync ( fullpath ) ;
163
+ } ;
164
+
165
+ if ( num === 0 ) {
166
+ assert . throws ( statFile , Error ) ;
167
+ } else {
168
+ assert . doesNotThrow ( statFile , Error ) ;
169
+ }
170
+ }
171
+ } ) ;
172
+
173
+ it ( 'testarchive3.log should be ascii, testarchive2.log.gz and testarchive1.log.gz should be zipped' , function ( ) {
174
+ const letters = [ 'B' , 'C' , 'D' ] ;
175
+
176
+ for ( var num = 1 ; num < 4 ; num ++ ) {
177
+ let content ;
178
+ const letter = letters [ num - 1 ] ;
179
+ const file = num === 3 ? 'testarchive3.log' : 'testarchive' + num + '.log.gz' ;
180
+ const fileContent = fs . readFileSync ( path . join ( __dirname , '..' , 'fixtures' , 'logs' , file ) ) ;
181
+
182
+ if ( num !== 3 ) {
183
+ // archives should be zipped
184
+ assert . doesNotThrow ( function ( ) {
185
+ content = zlib . gunzipSync ( fileContent ) . toString ( 'ascii' ) ;
186
+ } ) ;
187
+ } else {
188
+ // current log file should be plain text
189
+ content = fileContent . toString ( 'ascii' ) ;
190
+ }
191
+
192
+ assert ( content . match ( new RegExp ( letter , 'g' ) ) [ 0 ] . length , content . length ) ;
193
+ }
194
+ } ) ;
92
195
} ) ;
93
196
} ) ;
0 commit comments