forked from winstonjs/winston-daily-rotate-file
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
738 lines (650 loc) · 19.5 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
'use strict';
var fs = require('fs');
var path = require('path');
var util = require('util');
var common = require('winston/lib/winston/common');
var Transport = require('winston').Transport;
var Stream = require('stream').Stream;
var os = require('os');
var winston = require('winston');
var weekday = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
//
// ### function DailyRotateFile (options)
// #### @options {Object} Options for this instance.
// Constructor function for the DailyRotateFile transport object responsible
// for persisting log messages and metadata to one or more files.
//
var DailyRotateFile = module.exports = function (options) {
Transport.call(this, options);
//
// Helper function which throws an `Error` in the event
// that any of the rest of the arguments is present in `options`.
//
function throwIf(target /* , illegal... */) {
Array.prototype.slice.call(arguments, 1).forEach(function (name) {
if (options[name]) {
throw new Error('Cannot set ' + name + ' and ' + target + 'together');
}
});
}
if (options.filename || options.dirname) {
throwIf('filename or dirname', 'stream');
this._basename = this.filename = options.filename ?
path.basename(options.filename) :
'winston.log';
this.dirname = options.dirname || path.dirname(options.filename);
this.options = options.options || {flags: 'a'};
//
// "24 bytes" is maybe a good value for logging lines.
//
this.options.highWaterMark = this.options.highWaterMark || 24;
} else if (options.stream) {
throwIf('stream', 'filename', 'maxsize');
this._stream = options.stream;
var self = this;
this._stream.on('error', function (error) {
self.emit('error', error);
});
//
// We need to listen for drain events when
// write() returns false. This can make node
// mad at times.
//
this._stream.setMaxListeners(Infinity);
} else {
throw new Error('Cannot log to file without filename or stream.');
}
this.json = options.json !== false;
this.colorize = options.colorize || false;
this.maxsize = options.maxsize || null;
this.logstash = options.logstash || null;
this.maxFiles = options.maxFiles || null;
this.label = options.label || null;
this.prettyPrint = options.prettyPrint || false;
this.showLevel = options.showLevel === undefined ? true : options.showLevel;
this.timestamp = options.timestamp ? options.timestamp : true;
this.datePattern = options.datePattern ? options.datePattern : '.yyyy-MM-dd';
this.depth = options.depth || null;
this.eol = options.eol || os.EOL;
this.maxRetries = options.maxRetries || 2;
this.prepend = options.prepend || false;
this.localTime = options.localTime || false;
if (this.json) {
this.stringify = options.stringify;
}
//
// Internal state variables representing the number
// of files this instance has created and the current
// size (in bytes) of the current logfile.
//
this._size = 0;
this._created = 0;
this._buffer = [];
this._draining = false;
this._failures = 0;
// Internal variable which will hold a record of all files
// belonging to this transport which are currently in the
// log directory in chronological order.
//
this._currentFiles = function () {
//
// Only proceed if maxsize is not configured for this transport.
if (!this.maxsize) {
try {
return fs.readdirSync(this.dirname).filter(function (file) {
return file.includes(this._basename);
}.bind(this)).map(function (file) {
return {
name: file,
time: fs.statSync(path.join(this.dirname, file)).mtime.getTime()
};
}.bind(this)).sort(function (a, b) {
return a.time - b.time;
}).map(function (v) {
return v.name;
});
} catch (e) {
// directory doesnt exist so there are no files. Do nothing.
}
}
return [];
}.bind(this)();
// Get correct time values and copy into this
var now = this._getTimeFields();
this._year = now._year;
this._month = now._month;
this._date = now._date;
this._hour = now._hour;
this._minute = now._minute;
this._weekday = now._weekday;
var token = /d{1,4}|m{1,4}|yy(?:yy)?|([HhM])\1?/g;
var pad = function (val, len) {
val = String(val);
len = len || 2;
while (val.length < len) {
val = '0' + val;
}
return val;
};
this.getFormattedDate = function () {
var flags = {
yy: String(this._year).slice(2),
yyyy: this._year,
M: this._month + 1,
MM: pad(this._month + 1),
d: this._date,
dd: pad(this._date),
H: this._hour,
HH: pad(this._hour),
m: this._minute,
mm: pad(this._minute),
ddd: this._weekday
};
return this.datePattern.replace(token, function ($0) {
return $0 in flags ? flags[$0] : $0.slice(1, $0.length - 1);
});
};
};
//
// Inherit from `winston.Transport`.
//
util.inherits(DailyRotateFile, Transport);
/**
* Define a getter so that `winston.transports.DailyRotateFile`
* is available and thus backwards compatible.
*/
winston.transports.DailyRotateFile = DailyRotateFile;
//
// Expose the name of this Transport on the prototype
//
DailyRotateFile.prototype.name = 'dailyRotateFile';
//
// ### function log (level, msg, [meta], callback)
// #### @level {string} Level at which to log the message.
// #### @msg {string} Message to log
// #### @meta {Object} **Optional** Additional metadata to attach
// #### @callback {function} Continuation to respond to when complete.
// Core logging method exposed to Winston. Metadata is optional.
//
DailyRotateFile.prototype.log = function (level, msg, meta, callback) {
if (this.silent) {
return callback(null, true);
}
//
// If failures exceeds maxRetries then we can't access the
// stream. In this case we need to perform a noop and return
// an error.
//
if (this._failures >= this.maxRetries) {
return callback(new Error('Transport is in a failed state.'));
}
var self = this;
var output = common.log({
level: level,
message: msg,
meta: meta,
json: this.json,
colorize: this.colorize,
logstash: this.logstash,
prettyPrint: this.prettyPrint,
timestamp: this.timestamp,
label: this.label,
stringify: this.stringify,
showLevel: this.showLevel,
depth: this.depth,
formatter: this.formatter,
humanReadableUnhandledException: this.humanReadableUnhandledException
}) + this.eol;
this._size += output.length;
if (this.filename) {
this.open(function (err) {
if (err) {
//
// If there was an error enqueue the message
//
return self._buffer.push([output, callback]);
}
self._write(output, callback);
self._lazyDrain();
});
} else {
//
// If there is no `filename` on this instance then it was configured
// with a raw `WriteableStream` instance and we should not perform any
// size restrictions.
//
this._write(output, callback);
this._lazyDrain();
}
};
//
// ### function _write (data, cb)
// #### @data {String|Buffer} Data to write to the instance's stream.
// #### @cb {function} Continuation to respond to when complete.
// Write to the stream, ensure execution of a callback on completion.
//
DailyRotateFile.prototype._write = function (data, callback) {
// If this is a file write stream, we could use the builtin
// callback functionality, however, the stream is not guaranteed
// to be an fs.WriteStream.
var ret = this._stream.write(data);
if (!callback) {
return;
}
if (ret === false) {
return this._stream.once('drain', function () {
callback(null, true);
});
}
callback(null, true);
};
//
// ### function query (options, callback)
// #### @options {Object} Loggly-like query options for this instance.
// #### @callback {function} Continuation to respond to when complete.
// Query the transport. Options object is optional.
//
DailyRotateFile.prototype.query = function (options, callback) {
if (typeof options === 'function') {
callback = options;
options = {};
}
// TODO when maxfilesize rotate occurs
var file = path.join(this.dirname, this._getFilename());
options = this.normalizeQuery(options);
var buff = '';
var results = [];
var row = 0;
var stream = fs.createReadStream(file, {
encoding: 'utf8'
});
stream.on('error', function (err) {
if (stream.readable) {
stream.destroy();
}
if (!callback) {
return;
}
return err.code === 'ENOENT' ? callback(null, results) : callback(err);
});
stream.on('data', function (data) {
data = (buff + data).split(/\n+/);
var l = data.length - 1;
var i = 0;
for (; i < l; i++) {
if (!options.start || row >= options.start) {
add(data[i]);
}
row++;
}
buff = data[l];
});
stream.on('close', function () {
if (buff) {
add(buff, true);
}
if (options.order === 'desc') {
results = results.reverse();
}
if (callback) {
callback(null, results);
}
});
function add(buff, attempt) {
try {
var log = JSON.parse(buff);
if (check(log)) {
push(log);
}
} catch (e) {
if (!attempt) {
stream.emit('error', e);
}
}
}
function push(log) {
if (options.rows && results.length >= options.rows) {
if (stream.readable) {
stream.destroy();
}
return;
}
if (options.fields) {
var obj = {};
options.fields.forEach(function (key) {
obj[key] = log[key];
});
log = obj;
}
results.push(log);
}
function check(log) {
if (!log) {
return;
}
if (typeof log !== 'object') {
return;
}
var time = new Date(log.timestamp);
if ((options.from && time < options.from) ||
(options.until && time > options.until)) {
return;
}
return true;
}
};
//
// ### function stream (options)
// #### @options {Object} Stream options for this instance.
// Returns a log stream for this transport. Options object is optional.
//
DailyRotateFile.prototype.stream = function (options) {
var file = path.join(this.dirname, this._getFilename());
options = options || {};
var stream = new Stream();
var tail = {
file: file,
start: options.start
};
stream.destroy = common.tailFile(tail, function (err, line) {
if (err) {
return stream.emit('error', err);
}
try {
stream.emit('data', line);
line = JSON.parse(line);
stream.emit('log', line);
} catch (e) {
stream.emit('error', e);
}
});
if (stream.resume) {
stream.resume();
}
return stream;
};
//
// ### function open (callback)
// #### @callback {function} Continuation to respond to when complete
// Checks to see if a new file needs to be created based on the `maxsize`
// (if any) and the current size of the file used.
//
DailyRotateFile.prototype.open = function (callback) {
if (this.opening) {
//
// If we are already attempting to open the next
// available file then respond with a value indicating
// that the message should be buffered.
//
return callback(true);
} else if (!this._stream || (this.maxsize && this._size >= this.maxsize) ||
this._filenameHasExpired()) {
//
// If we dont have a stream or have exceeded our size, then create
// the next stream and respond with a value indicating that
// the message should be buffered.
//
callback(true);
return this._createStream();
}
//
// Otherwise we have a valid (and ready) stream.
//
callback();
};
//
// ### function close ()
// Closes the stream associated with this instance.
//
DailyRotateFile.prototype.close = function () {
var self = this;
if (this._stream) {
this._stream.end();
this._stream.destroySoon();
this._stream.once('drain', function () {
self.emit('flush');
self.emit('closed');
});
}
};
//
// ### function flush ()
// Flushes any buffered messages to the current `stream`
// used by this instance.
//
DailyRotateFile.prototype.flush = function () {
var self = this;
//
// Iterate over the `_buffer` of enqueued messaged
// and then write them to the newly created stream.
//
this._buffer.forEach(function (item) {
var str = item[0];
var callback = item[1];
process.nextTick(function () {
self._write(str, callback);
self._size += str.length;
});
});
//
// Quickly truncate the `_buffer` once the write operations
// have been started
//
self._buffer.length = 0;
//
// When the stream has drained we have flushed
// our buffer.
//
self._stream.once('drain', function () {
self.emit('flush');
self.emit('logged');
});
};
//
// ### @private function _createStream ()
// Attempts to open the next appropriate file for this instance
// based on the common state (such as `maxsize` and `_basename`).
//
DailyRotateFile.prototype._createStream = function () {
var self = this;
this.opening = true;
(function checkFile(target) {
var fullname = path.join(self.dirname, target);
//
// Creates the `WriteStream` and then flushes any
// buffered messages.
//
function createAndFlush(size) {
if (self._stream) {
self._stream.end();
self._stream.destroySoon();
}
self._size = size;
self.filename = target;
self._stream = fs.createWriteStream(fullname, self.options);
self._stream.on('error', function (error) {
if (self._failures < self.maxRetries) {
self._createStream();
self._failures++;
} else {
self.emit('error', error);
}
});
//
// We need to listen for drain events when
// write() returns false. This can make node
// mad at times.
//
self._stream.setMaxListeners(Infinity);
//
// When the current stream has finished flushing
// then we can be sure we have finished opening
// and thus can emit the `open` event.
//
self.once('flush', function () {
// Because "flush" event is based on native stream "drain" event,
// logs could be written inbetween "self.flush()" and here
// Therefore, we need to flush again to make sure everything is flushed
self.flush();
self.opening = false;
self.emit('open', fullname);
});
//
// Remark: It is possible that in the time it has taken to find the
// next logfile to be written more data than `maxsize` has been buffered,
// but for sensible limits (10s - 100s of MB) this seems unlikely in less
// than one second.
//
self.flush();
}
fs.stat(fullname, function (err, stats) {
if (err) {
if (err.code !== 'ENOENT') {
return self.emit('error', err);
}
return createAndFlush(0);
}
if (!stats || (self.maxsize && stats.size >= self.maxsize)) {
//
// If `stats.size` is greater than the `maxsize` for
// this instance then try again
//
return checkFile(self._getFile(true));
}
if (self._filenameHasExpired()) {
var now = self._getTimeFields();
self._year = now._year;
self._month = now._month;
self._date = now._date;
self._hour = now._hour;
self._minute = now._minute;
self._weekday = now._weekday;
self._created = 0;
return checkFile(self._getFile());
}
createAndFlush(stats.size);
});
})(this._getFile());
};
//
// ### @private function _getFile ()
// Gets the next filename to use for this instance
// in the case that log filesizes are being capped.
//
DailyRotateFile.prototype._getFile = function (inc) {
var filename = this._getFilename();
var remaining;
if (inc) {
//
// Increment the number of files created or
// checked by this instance.
//
// Check for maxFiles option and delete file
if (this.maxFiles && (this._created >= (this.maxFiles - 1))) {
remaining = this._created - (this.maxFiles - 1);
if (remaining === 0) {
try {
fs.unlinkSync(path.join(this.dirname, filename));
} catch (e) {}
} else {
try {
fs.unlinkSync(path.join(this.dirname, filename + '.' + remaining));
} catch (e) {}
}
}
this._created += 1;
} else if (!this.maxsize) {
//
// If the filename does not exist in the _currentFiles array then add it.
if (this._currentFiles.indexOf(filename) === -1) {
this._currentFiles.push(filename);
}
// While the _currentFiles array contains more file names than is configured
// in maxFiles loop the _currentFiles array and delete the file found at el
// 0.
while (this.maxFiles && (this._currentFiles.length > this.maxFiles)) {
try {
fs.unlinkSync(path.join(this.dirname, this._currentFiles[0]));
} catch (e) {
// File isn't accessible, do nothing.
}
// Remove the filename that was just deleted from the _currentFiles array.
this._currentFiles = this._currentFiles.slice(1);
}
}
return this._created ? filename + '.' + this._created : filename;
};
//
// ### @private function _getFilename ()
// Returns the log filename depending on `this.prepend` option value
//
DailyRotateFile.prototype._getFilename = function () {
var formattedDate = this.getFormattedDate();
if (this.prepend) {
if (this.datePattern === '.yyyy-MM-dd') {
this.datePattern = 'yyyy-MM-dd.';
formattedDate = this.getFormattedDate();
}
return formattedDate + this._basename;
}
return this._basename + formattedDate;
};
//
// ### @private function _lazyDrain ()
// Lazily attempts to emit the `logged` event when `this.stream` has
// drained. This is really just a simple mutex that only works because
// Node.js is single-threaded.
//
DailyRotateFile.prototype._lazyDrain = function () {
var self = this;
if (!this._draining && this._stream) {
this._draining = true;
this._stream.once('drain', function () {
this._draining = false;
self.emit('logged');
});
}
};
//
// ### @private function _getTimeFields
// Gets time fields in either URC or local time depending on config
//
DailyRotateFile.prototype._getTimeFields = function () {
var now = new Date();
return this.localTime ? {
_year: now.getFullYear(),
_month: now.getMonth(),
_date: now.getDate(),
_hour: now.getHours(),
_minute: now.getMinutes(),
_weekday: weekday[now.getDay()]
} : {
_year: now.getUTCFullYear(),
_month: now.getUTCMonth(),
_date: now.getUTCDate(),
_hour: now.getUTCHours(),
_minute: now.getUTCMinutes(),
_weekday: weekday[now.getUTCDay()]
};
};
//
// ### @private function _filenameHasExpired ()
// Checks whether the current log file is valid
// based on given datepattern
//
DailyRotateFile.prototype._filenameHasExpired = function () {
var now = this._getTimeFields();
// searching for m is enough to say minute in date pattern
if (this.datePattern.match(/m/)) {
return (this._year < now._year || this._month < now._month || this._date < now._date || this._hour < now._hour || this._minute < now._minute);
} else if (this.datePattern.match(/H/)) {
return (this._year < now._year || this._month < now._month || this._date < now._date || this._hour < now._hour);
} else if (this.datePattern.match(/d/)) {
return (this._year < now._year || this._month < now._month || this._date < now._date);
} else if (this.datePattern.match(/M/)) {
return (this._year < now._year || this._month < now._month);
} else if (this.datePattern.match(/yy/)) {
return (this._year < now._year);
}
return false;
};