@@ -4,14 +4,11 @@ var fs = require('fs');
4
4
var os = require ( 'os' ) ;
5
5
var path = require ( 'path' ) ;
6
6
var util = require ( 'util' ) ;
7
- var semver = require ( 'semver' ) ;
8
7
var zlib = require ( 'zlib' ) ;
9
8
var hash = require ( 'object-hash' ) ;
10
- var winston = require ( 'winston' ) ;
11
- var compat = require ( 'winston-compat' ) ;
12
9
var MESSAGE = require ( 'triple-beam' ) . MESSAGE ;
13
10
var PassThrough = require ( 'stream' ) . PassThrough ;
14
- var Transport = semver . major ( winston . version ) === 2 ? compat . Transport : require ( 'winston-transport' ) ;
11
+ var Transport = require ( 'winston-transport' ) ;
15
12
16
13
var loggerDefaults = {
17
14
json : false ,
@@ -127,29 +124,13 @@ util.inherits(DailyRotateFile, Transport);
127
124
DailyRotateFile . prototype . name = 'dailyRotateFile' ;
128
125
129
126
var noop = function ( ) { } ;
130
- if ( semver . major ( winston . version ) === 2 ) {
131
- DailyRotateFile . prototype . log = function ( level , msg , meta , callback ) {
132
- callback = callback || noop ;
133
- var options = Object . assign ( { } , this . options , {
134
- level : level ,
135
- message : msg ,
136
- meta : meta
137
- } ) ;
138
-
139
- var output = compat . log ( options ) + options . eol ;
140
- this . logStream . write ( output ) ;
141
- callback ( null , true ) ;
142
- } ;
143
- } else {
144
- DailyRotateFile . prototype . normalizeQuery = compat . Transport . prototype . normalizeQuery ;
145
- DailyRotateFile . prototype . log = function ( info , callback ) {
146
- callback = callback || noop ;
127
+ DailyRotateFile . prototype . log = function ( info , callback ) {
128
+ callback = callback || noop ;
147
129
148
- this . logStream . write ( info [ MESSAGE ] + this . options . eol ) ;
149
- this . emit ( 'logged' , info ) ;
150
- callback ( null , true ) ;
151
- } ;
152
- }
130
+ this . logStream . write ( info [ MESSAGE ] + this . options . eol ) ;
131
+ this . emit ( 'logged' , info ) ;
132
+ callback ( null , true ) ;
133
+ } ;
153
134
154
135
DailyRotateFile . prototype . close = function ( ) {
155
136
var self = this ;
@@ -159,134 +140,3 @@ DailyRotateFile.prototype.close = function () {
159
140
} ) ;
160
141
}
161
142
} ;
162
-
163
- DailyRotateFile . prototype . query = function ( options , callback ) {
164
- if ( typeof options === 'function' ) {
165
- callback = options ;
166
- options = { } ;
167
- }
168
-
169
- if ( ! this . options . json ) {
170
- throw new Error ( 'query() may not be used without the json option being set to true' ) ;
171
- }
172
-
173
- if ( ! this . filename ) {
174
- throw new Error ( 'query() may not be used when initializing with a stream' ) ;
175
- }
176
-
177
- var self = this ;
178
- var results = [ ] ;
179
- options = self . normalizeQuery ( options ) ;
180
-
181
- var logFiles = ( function ( ) {
182
- var fileRegex = new RegExp ( self . filename . replace ( '%DATE%' , '.*' ) , 'i' ) ;
183
- return fs . readdirSync ( self . dirname ) . filter ( function ( file ) {
184
- return path . basename ( file ) . match ( fileRegex ) ;
185
- } ) ;
186
- } ) ( ) ;
187
-
188
- if ( logFiles . length === 0 && callback ) {
189
- callback ( null , results ) ;
190
- }
191
-
192
- ( function processLogFile ( file ) {
193
- if ( ! file ) {
194
- return ;
195
- }
196
-
197
- var logFile = path . join ( self . dirname , file ) ;
198
- var buff = '' ;
199
-
200
- var stream ;
201
-
202
- if ( file . endsWith ( '.gz' ) ) {
203
- stream = new PassThrough ( ) ;
204
- fs . createReadStream ( logFile ) . pipe ( zlib . createGunzip ( ) ) . pipe ( stream ) ;
205
- } else {
206
- stream = fs . createReadStream ( logFile , {
207
- encoding : 'utf8'
208
- } ) ;
209
- }
210
-
211
- stream . on ( 'error' , function ( err ) {
212
- if ( stream . readable ) {
213
- stream . destroy ( ) ;
214
- }
215
-
216
- if ( ! callback ) {
217
- return ;
218
- }
219
-
220
- return err . code === 'ENOENT' ? callback ( null , results ) : callback ( err ) ;
221
- } ) ;
222
-
223
- stream . on ( 'data' , function ( data ) {
224
- data = ( buff + data ) . split ( / \n + / ) ;
225
- var l = data . length - 1 ;
226
-
227
- for ( var i = 0 ; i < l ; i ++ ) {
228
- add ( data [ i ] ) ;
229
- }
230
-
231
- buff = data [ l ] ;
232
- } ) ;
233
-
234
- stream . on ( 'end' , function ( ) {
235
- if ( buff ) {
236
- add ( buff , true ) ;
237
- }
238
-
239
- if ( logFiles . length ) {
240
- processLogFile ( logFiles . shift ( ) ) ;
241
- } else if ( callback ) {
242
- results . sort ( function ( a , b ) {
243
- var d1 = new Date ( a . timestamp ) . getTime ( ) ;
244
- var d2 = new Date ( b . timestamp ) . getTime ( ) ;
245
-
246
- return d1 > d2 ? 1 : d1 < d2 ? - 1 : 0 ;
247
- } ) ;
248
-
249
- if ( options . order === 'desc' ) {
250
- results = results . reverse ( ) ;
251
- }
252
-
253
- var start = options . start || 0 ;
254
- var limit = options . limit || results . length ;
255
-
256
- results = results . slice ( start , start + limit ) ;
257
-
258
- if ( options . fields ) {
259
- results = results . map ( function ( log ) {
260
- var obj = { } ;
261
- options . fields . forEach ( function ( key ) {
262
- obj [ key ] = log [ key ] ;
263
- } ) ;
264
- return obj ;
265
- } ) ;
266
- }
267
-
268
- callback ( null , results ) ;
269
- }
270
- } ) ;
271
-
272
- function add ( buff , attempt ) {
273
- try {
274
- var log = JSON . parse ( buff ) ;
275
- if ( ! log || typeof log !== 'object' ) {
276
- return ;
277
- }
278
-
279
- var time = new Date ( log . timestamp ) ;
280
- if ( ( options . from && time < options . from ) || ( options . until && time > options . until ) ) {
281
- return ;
282
- }
283
-
284
- results . push ( log ) ;
285
- } catch ( e ) {
286
- if ( ! attempt ) {
287
- stream . emit ( 'error' , e ) ;
288
- }
289
- }
290
- }
291
- } ) ( logFiles . shift ( ) ) ;
292
- } ;
0 commit comments