simple.tests.js
28.8 KB
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
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
/* eslint-disable max-nested-callbacks,no-unused-expressions */
'use strict';
var path = require('path');
var expect = require('chai').expect;
var winston = require('winston');
var rimraf = require('rimraf');
var mkdirp = require('mkdirp');
var moment = require('moment');
var fs = require('fs');
var tk = require('timekeeper');
var MemoryStream = require('./memory-stream');
var DailyRotateFile = require('../');
var fixturesDir = path.join(__dirname, 'fixtures');
var transports = {
'file': new DailyRotateFile({
filename: path.join(fixturesDir, 'testfilename.log'),
prepend: false
}),
'stream': new DailyRotateFile({stream: new MemoryStream()}),
'prepended file': new DailyRotateFile({
filename: path.join(fixturesDir, 'testfilename.log'),
prepend: true
}),
'weekday file': new DailyRotateFile({
filename: path.join(fixturesDir, 'testfilename_weekday'),
datePattern: '.ddd.log'
}),
'prepend weekday file': new DailyRotateFile({
filename: path.join(fixturesDir, 'testfilename_prepend_weekday.log'),
datePattern: 'ddd-',
prepend: true
})
};
describe('winston/transports/daily-rotate-file', function () {
before(function () {
rimraf.sync(fixturesDir);
mkdirp.sync(fixturesDir);
});
describe('an instance of the transport', function () {
describe('with / characters in the datePattern', function () {
it('should create the full path', function (done) {
var now = moment();
var transport = new DailyRotateFile({
filename: path.join(fixturesDir, 'application'),
datePattern: '/yyyy/MM/dd.log',
createTree: true
});
transport.log('info', 'test message', {}, function (err) {
if (err) {
done(err);
}
fs.readFile(path.join(fixturesDir, 'application', now.format('YYYY'), now.format('MM'), now.format('DD') + '.log'), 'utf8', function (err, contents) {
if (err) {
done(err);
}
var lines = contents.split('\n').filter(function (n) {
return n !== '';
});
expect(lines.length).to.equal(1);
done();
});
});
});
});
describe('with default datePatterns', function () {
it('should have a proper filename when prepend option is false', function () {
var now = moment().utc().format('YYYY-MM-DD');
var transport = new DailyRotateFile({
filename: path.join(fixturesDir, 'prepend-false.log'),
prepend: false
});
expect(transport._getFilename()).to.equal('prepend-false.log.' + now);
});
it('should have a proper filename when prepend option is false (localtime)', function () {
var now = moment().format('YYYY-MM-DD');
var transport = new DailyRotateFile({
filename: path.join(fixturesDir, 'prepend-false.log'),
localTime: true,
prepend: false
});
expect(transport._getFilename()).to.equal('prepend-false.log.' + now);
});
it('should have a proper filename when prepend options is true', function () {
var now = moment().utc().format('YYYY-MM-DD');
var transport = new DailyRotateFile({
filename: path.join(fixturesDir, 'prepend-true.log'),
prepend: true
});
expect(transport._getFilename()).to.equal(now + '.prepend-true.log');
});
it('should remove leading dot if one is provided with datePattern', function () {
var now = moment().utc().format('YYYYMMDD');
var transport = new DailyRotateFile({
filename: path.join(fixturesDir, 'prepend-false.log'),
prepend: false,
datePattern: '.yyyyMMdd'
});
expect(transport._getFilename()).to.equal('prepend-false.log.' + now);
});
it('should not add leading dot if one is not provided with datePattern', function () {
var now = moment().utc().format('YYYY-MM-DD');
var transport = new DailyRotateFile({
filename: path.join(fixturesDir, 'log'),
datePattern: '-yyyy-MM-dd.log'
});
expect(transport._getFilename()).to.equal('log-' + now + '.log');
});
it('should remove leading dot if one is provided with datePattern when prepend option is true', function () {
var now = moment().utc().format('YYYY-MM-DD');
var transport = new DailyRotateFile({
filename: path.join(fixturesDir, 'prepend-true.log'),
prepend: true,
datePattern: '.yyyy-MM-dd'
});
expect(transport._getFilename()).to.equal(now + '.prepend-true.log');
});
});
Object.keys(transports).forEach(function (t) {
describe('when passed a valid ' + t, function () {
var transport;
beforeEach(function () {
transport = transports[t];
});
it('should have the proper methods defined', function () {
expect(transport).to.be.instanceOf(DailyRotateFile);
expect(transport).to.respondTo('log');
});
var levels = winston.config.npm.levels;
Object.keys(levels).forEach(function (level) {
describe('with the ' + level + ' level', function () {
it('should respond with true when passed no metadata', function (done) {
transport.log(level, 'test message', {}, function (err, logged) {
expect(err).to.be.null;
expect(logged).to.be.true;
done();
});
});
var circular = { };
circular.metadata = circular;
var params = {
no: {},
object: {metadata: true},
primitive: 'metadata',
circular: circular
};
Object.keys(params).forEach(function (param) {
it('should respond with true when passed ' + param + ' metadata', function (done) {
transport.log(level, 'test message', params[param], function (err, logged) {
expect(err).to.be.null;
expect(logged).to.be.true;
done();
});
});
});
});
});
});
});
describe('when passed an invalid filename', function () {
var transport;
beforeEach(function () {
transport = new DailyRotateFile({
filename: path.join(fixturesDir, 'invalid', 'testfilename.log')
});
});
it('should have proper methods defined', function () {
expect(transport).to.be.instanceOf(DailyRotateFile);
expect(transport).to.respondTo('log');
});
it('should enter noop failed state', function (done) {
transport.on('error', function (emitErr) {
expect(emitErr).to.be.instanceOf(Error);
expect(emitErr.code, 'ENOENT');
expect(transport._failures).to.equal(transport.maxRetries);
done();
});
transport.log('error', 'test message');
});
});
describe('when passed an valid filename with different date patterns for log rotation', function () {
// patterns having one start timestamp for which log file will be creted,
// then one mid timestamp for which log file should not be rotated,
// and finally one end timestamp for which log file should be rotated and
// new logfile should be created.
var patterns = {
'full year pattern .yyyy': {
pattern: '.yyyy',
start: 1861947160000, // GMT: Mon, 01 Jan 2029 07:32:40 GMT
mid: 1874993560000, // GMT: Fri, 01 Jun 2029 07:32:40 GMT
end: 1893483160000, // GMT: Tue, 01 Jan 2030 07:32:40 GMT
oldfile: 'test-rotation.log.2029',
newfile: 'test-rotation.log.2030'
},
'small year pattern .yy': {
pattern: '.yy',
start: 1861947160000, // GMT: Mon, 01 Jan 2029 07:32:40 GMT
mid: 1874993560000, // GMT: Fri, 01 Jun 2029 07:32:40 GMT
end: 1893483160000, // GMT: Tue, 01 Jan 2030 07:32:40 GMT
oldfile: 'test-rotation.log.29',
newfile: 'test-rotation.log.30'
},
'month pattern .M': {
pattern: '.M',
start: 1861947160000, // GMT: Mon, 01 Jan 2029 07:32:40 GMT
mid: 1863156760000, // GMT: Mon, 15 Jan 2029 07:32:40 GMT
end: 1864625560000, // GMT: Thu, 01 Feb 2029 07:32:40 GMT
oldfile: 'test-rotation.log.1',
newfile: 'test-rotation.log.2'
},
'zero padded month pattern .MM': {
pattern: '.MM',
start: 1861947160000, // GMT: Mon, 01 Jan 2029 07:32:40 GMT
mid: 1863156760000, // GMT: Mon, 15 Jan 2029 07:32:40 GMT
end: 1864625560000, // GMT: Thu, 01 Feb 2029 07:32:40 GMT
oldfile: 'test-rotation.log.01',
newfile: 'test-rotation.log.02'
},
'daypattern .d': {
pattern: '.d',
start: 1861947160000, // GMT: Mon, 01 Jan 2029 07:32:40 GMT
mid: 1861986760000, // GMT: Mon, 01 Jan 2029 18:32:40 GMT
end: 1863156760000, // GMT: Mon, 15 Jan 2029 07:32:40 GMT
oldfile: 'test-rotation.log.1',
newfile: 'test-rotation.log.15'
},
'zero padded day pattern .dd': {
pattern: '.dd',
start: 1861947160000, // GMT: Mon, 01 Jan 2029 07:32:40 GMT
mid: 1861986760000, // GMT: Mon, 01 Jan 2029 18:32:40 GMT
end: 1863156760000, // GMT: Mon, 15 Jan 2029 07:32:40 GMT
oldfile: 'test-rotation.log.01',
newfile: 'test-rotation.log.15'
},
'hour pattern .H': {
pattern: '.H',
start: 1861947160000, // GMT: Mon, 01 Jan 2029 07:32:40 GMT
mid: 1861947760000, // GMT: Mon, 01 Jan 2029 07:42:40 GMT
end: 1861950760000, // GMT: Mon, 01 Jan 2029 08:32:40 GMT
oldfile: 'test-rotation.log.7',
newfile: 'test-rotation.log.8'
},
'zero padded hour pattern .HH': {
pattern: '.HH',
start: 1861947160000, // GMT: Mon, 01 Jan 2029 07:32:40 GMT
mid: 1861947760000, // GMT: Mon, 01 Jan 2029 07:42:40 GMT
end: 1861950760000, // GMT: Mon, 01 Jan 2029 08:32:40 GMT
oldfile: 'test-rotation.log.07',
newfile: 'test-rotation.log.08'
},
'minute pattern .m': {
pattern: '.m',
start: 1861947160000, // GMT: Mon, 01 Jan 2029 07:32:50 GMT
mid: 1861947170000, // GMT: Mon, 01 Jan 2029 07:32:50 GMT
end: 1861947760000, // GMT: Mon, 01 Jan 2029 07:42:40 GMT
oldfile: 'test-rotation.log.32',
newfile: 'test-rotation.log.42'
},
'zero padded minute pattern .mm': {
pattern: '.mm',
start: 1861947160000, // GMT: Mon, 01 Jan 2029 07:32:50 GMT
mid: 1861947170000, // GMT: Mon, 01 Jan 2029 07:32:50 GMT
end: 1861947760000, // GMT: Mon, 01 Jan 2029 07:42:40 GMT
oldfile: 'test-rotation.log.32',
newfile: 'test-rotation.log.42'
},
'daily rotation pattern .yyyy-MM-dd': {
pattern: '.yyyy-MM-dd',
start: 1861947160000, // GMT: Mon, 01 Jan 2029 07:32:40 GMT
mid: 1861965828000, // GMT: Mon, 01 Jan 2029 12:43:48 GMT
end: 1863156760000, // GMT: Mon, 15 Jan 2029 07:32:40 GMT
oldfile: 'test-rotation.log.2029-01-01',
newfile: 'test-rotation.log.2029-01-15'
}
};
Object.keys(patterns).forEach(function (pattern) {
describe('when passed the pattern ' + pattern, function () {
var transport;
var rotationLogPath = path.join(fixturesDir, 'rotations');
beforeEach(function (done) {
this.time = new Date(patterns[pattern].start);
tk.travel(this.time);
rimraf.sync(rotationLogPath);
mkdirp.sync(rotationLogPath);
transport = new DailyRotateFile({
filename: path.join(rotationLogPath, 'test-rotation.log'),
datePattern: patterns[pattern].pattern
});
done();
});
afterEach(function (done) {
tk.reset();
done();
});
it('should create log with proper timestamp', function (done) {
var self = this;
transport.log('error', 'test message', {}, function (err) {
if (err) {
done(err);
}
var filesCreated = fs.readdirSync(rotationLogPath);
expect(filesCreated.length).to.eql(1);
expect(filesCreated).to.include(patterns[pattern].oldfile);
self.time = new Date(patterns[pattern].mid);
tk.travel(self.time);
transport.log('error', '2nd test message', {}, function (err) {
if (err) {
done(err);
}
filesCreated = fs.readdirSync(rotationLogPath);
expect(filesCreated.length).to.eql(1);
expect(filesCreated).to.include(patterns[pattern].oldfile);
self.time = new Date(patterns[pattern].end);
tk.travel(self.time);
transport.log('error', '3rd test message', {}, function (err) {
if (err) {
done(err);
}
filesCreated = fs.readdirSync(rotationLogPath);
expect(filesCreated.length).to.eql(2);
expect(filesCreated).to.include(patterns[pattern].newfile);
transport.close();
done();
});
});
});
});
});
});
});
describe('when passed with maxsize and maxfiles', function () {
var dailyRotationPattern = {
pattern: '.yyyy-MM-dd',
start: 1861947160000, // GMT: Mon, 01 Jan 2029 07:32:40 GMT
mid: 1861986760000, // GMT: Mon, 01 Jan 2029 18:32:40 GMT
file1: 'test-rotation.log.2029-01-01',
file2: 'test-rotation.log.2029-01-01.1',
file3: 'test-rotation.log.2029-01-01.2'
};
describe('when passed the pattern ' + dailyRotationPattern.pattern, function () {
var transport;
var rotationLogPath = path.join(fixturesDir, 'rotations');
beforeEach(function (done) {
this.time = new Date(dailyRotationPattern.start);
tk.travel(this.time);
rimraf.sync(rotationLogPath);
mkdirp.sync(rotationLogPath);
transport = new DailyRotateFile({
filename: path.join(rotationLogPath, 'test-rotation.log'),
datePattern: dailyRotationPattern.pattern,
maxFiles: 2,
maxsize: 100
});
done();
});
afterEach(function (done) {
tk.reset();
done();
});
it('should properly rotate log with old files getting deleted', function (done) {
var self = this;
transport.log('error', 'test message with more than 100 bytes data', {}, function (err) {
if (err) {
done(err);
}
transport.log('error', '2nd test with more than 100 bytes data', {}, function (err) {
if (err) {
done(err);
}
self.time = new Date(dailyRotationPattern.mid);
tk.travel(self.time);
transport.log('error', '3rd test', {}, function (err) {
if (err) {
done(err);
}
transport.log('error', '4th test message with more than 100 bytes data', {}, function (err) {
if (err) {
done(err);
}
var filesCreated = fs.readdirSync(rotationLogPath);
expect(filesCreated.length).to.eql(2);
expect(filesCreated).not.to.include(dailyRotationPattern.file1);
expect(filesCreated).to.include(dailyRotationPattern.file2);
expect(filesCreated).to.include(dailyRotationPattern.file3);
done();
});
});
});
});
});
});
});
describe('when passed with maxfiles set and maxsize not set', function () {
var dailyRotationPattern = {
pattern: '.yyyy-MM-dd',
jan1: 1861947160000, // GMT: Mon, 01 Jan 2029 07:32:40 GMT
jan2: 1862033560000, // GMT: Mon, 02 Jan 2029 07:32:40 GMT
jan3: 1862119960000, // GMT: Mon, 03 Jan 2029 07:32:40 GMT
file1: 'test-rotation-no-maxsize.log.2029-01-01',
file2: 'test-rotation-no-maxsize.log.2029-01-02',
file3: 'test-rotation-no-maxsize.log.2029-01-03'
};
describe('when passed the pattern ' + dailyRotationPattern.pattern + ' and no maxsize', function () {
var transport;
var rotationLogPath = path.join(fixturesDir, 'rotations_no_maxsize');
beforeEach(function (done) {
rimraf.sync(rotationLogPath);
mkdirp.sync(rotationLogPath);
transport = new DailyRotateFile({
filename: path.join(rotationLogPath, 'test-rotation-no-maxsize.log'),
datePattern: dailyRotationPattern.pattern,
maxFiles: 2
});
done();
});
afterEach(function () {
tk.reset();
});
it('should properly rotate log without maxzsize set and with old files getting deleted', function (done) {
var self = this;
self.time = new Date(dailyRotationPattern.jan1);
tk.travel(self.time);
transport.log('error', 'test message on Jan 1st', {}, function (err) {
if (err) {
done(err);
}
self.time = new Date(dailyRotationPattern.jan2);
tk.travel(self.time);
transport.log('error', 'test message on Jan 2nd', {}, function (err) {
if (err) {
done(err);
}
self.time = new Date(dailyRotationPattern.jan3);
tk.travel(self.time);
transport.log('error', 'test message on Jan 3rd', {}, function (err) {
if (err) {
done(err);
}
self.time = new Date(dailyRotationPattern.jan3);
tk.travel(self.time);
transport.log('error', 'second test message on Jan 3rd', {}, function (err) {
if (err) {
done(err);
}
var filesCreated = fs.readdirSync(rotationLogPath);
console.log('files : ' + filesCreated);
expect(filesCreated.length).to.eql(2);
expect(filesCreated).not.to.include(dailyRotationPattern.file1);
expect(filesCreated).to.include(dailyRotationPattern.file2);
expect(filesCreated).to.include(dailyRotationPattern.file3);
done();
});
});
});
});
});
});
});
describe('when zippedArchive is true and passed an valid filename with different date patterns for log rotation', function () {
// patterns having one start timestamp for which log file will be creted,
// then one mid timestamp for which log file should not be rotated,
// and finally one end timestamp for which log file should be rotated and
// new logfile should be created.
var patterns = {
'full year pattern .yyyy': {
pattern: '.yyyy',
start: 1861947160000, // GMT: Mon, 01 Jan 2029 07:32:40 GMT
mid: 1874993560000, // GMT: Fri, 01 Jun 2029 07:32:40 GMT
end: 1893483160000, // GMT: Tue, 01 Jan 2030 07:32:40 GMT
oldfile: 'test-rotation.log.2029',
newfile: 'test-rotation.log.2030'
},
'small year pattern .yy': {
pattern: '.yy',
start: 1861947160000, // GMT: Mon, 01 Jan 2029 07:32:40 GMT
mid: 1874993560000, // GMT: Fri, 01 Jun 2029 07:32:40 GMT
end: 1893483160000, // GMT: Tue, 01 Jan 2030 07:32:40 GMT
oldfile: 'test-rotation.log.29',
newfile: 'test-rotation.log.30'
},
'month pattern .M': {
pattern: '.M',
start: 1861947160000, // GMT: Mon, 01 Jan 2029 07:32:40 GMT
mid: 1863156760000, // GMT: Mon, 15 Jan 2029 07:32:40 GMT
end: 1864625560000, // GMT: Thu, 01 Feb 2029 07:32:40 GMT
oldfile: 'test-rotation.log.1',
newfile: 'test-rotation.log.2'
},
'zero padded month pattern .MM': {
pattern: '.MM',
start: 1861947160000, // GMT: Mon, 01 Jan 2029 07:32:40 GMT
mid: 1863156760000, // GMT: Mon, 15 Jan 2029 07:32:40 GMT
end: 1864625560000, // GMT: Thu, 01 Feb 2029 07:32:40 GMT
oldfile: 'test-rotation.log.01',
newfile: 'test-rotation.log.02'
},
'daypattern .d': {
pattern: '.d',
start: 1861947160000, // GMT: Mon, 01 Jan 2029 07:32:40 GMT
mid: 1861986760000, // GMT: Mon, 01 Jan 2029 18:32:40 GMT
end: 1863156760000, // GMT: Mon, 15 Jan 2029 07:32:40 GMT
oldfile: 'test-rotation.log.1',
newfile: 'test-rotation.log.15'
},
'zero padded day pattern .dd': {
pattern: '.dd',
start: 1861947160000, // GMT: Mon, 01 Jan 2029 07:32:40 GMT
mid: 1861986760000, // GMT: Mon, 01 Jan 2029 18:32:40 GMT
end: 1863156760000, // GMT: Mon, 15 Jan 2029 07:32:40 GMT
oldfile: 'test-rotation.log.01',
newfile: 'test-rotation.log.15'
},
'hour pattern .H': {
pattern: '.H',
start: 1861947160000, // GMT: Mon, 01 Jan 2029 07:32:40 GMT
mid: 1861947760000, // GMT: Mon, 01 Jan 2029 07:42:40 GMT
end: 1861950760000, // GMT: Mon, 01 Jan 2029 08:32:40 GMT
oldfile: 'test-rotation.log.7',
newfile: 'test-rotation.log.8'
},
'zero padded hour pattern .HH': {
pattern: '.HH',
start: 1861947160000, // GMT: Mon, 01 Jan 2029 07:32:40 GMT
mid: 1861947760000, // GMT: Mon, 01 Jan 2029 07:42:40 GMT
end: 1861950760000, // GMT: Mon, 01 Jan 2029 08:32:40 GMT
oldfile: 'test-rotation.log.07',
newfile: 'test-rotation.log.08'
},
'minute pattern .m': {
pattern: '.m',
start: 1861947160000, // GMT: Mon, 01 Jan 2029 07:32:50 GMT
mid: 1861947170000, // GMT: Mon, 01 Jan 2029 07:32:50 GMT
end: 1861947760000, // GMT: Mon, 01 Jan 2029 07:42:40 GMT
oldfile: 'test-rotation.log.32',
newfile: 'test-rotation.log.42'
},
'zero padded minute pattern .mm': {
pattern: '.mm',
start: 1861947160000, // GMT: Mon, 01 Jan 2029 07:32:50 GMT
mid: 1861947170000, // GMT: Mon, 01 Jan 2029 07:32:50 GMT
end: 1861947760000, // GMT: Mon, 01 Jan 2029 07:42:40 GMT
oldfile: 'test-rotation.log.32',
newfile: 'test-rotation.log.42'
},
'daily rotation pattern .yyyy-MM-dd': {
pattern: '.yyyy-MM-dd',
start: 1861947160000, // GMT: Mon, 01 Jan 2029 07:32:40 GMT
mid: 1861965828000, // GMT: Mon, 01 Jan 2029 12:43:48 GMT
end: 1863156760000, // GMT: Mon, 15 Jan 2029 07:32:40 GMT
oldfile: 'test-rotation.log.2029-01-01',
newfile: 'test-rotation.log.2029-01-15'
}
};
Object.keys(patterns).forEach(function (pattern) {
describe('when passed the pattern ' + pattern, function () {
var transport;
var rotationLogPath = path.join(fixturesDir, 'rotations');
beforeEach(function (done) {
this.time = new Date(patterns[pattern].start);
tk.travel(this.time);
rimraf.sync(rotationLogPath);
mkdirp.sync(rotationLogPath);
transport = new DailyRotateFile({
filename: path.join(rotationLogPath, 'test-rotation.log'),
datePattern: patterns[pattern].pattern,
zippedArchive: true
});
done();
});
afterEach(function () {
tk.reset();
});
it('should create log with proper timestamp', function (done) {
var self = this;
transport.log('error', 'test message', {}, function (err) {
if (err) {
done(err);
}
var filesCreated = fs.readdirSync(rotationLogPath);
expect(filesCreated.length).to.eql(1);
expect(filesCreated).to.include(patterns[pattern].oldfile);
self.time = new Date(patterns[pattern].mid);
tk.travel(self.time);
transport.log('error', '2nd test message', {}, function (err) {
if (err) {
done(err);
}
filesCreated = fs.readdirSync(rotationLogPath);
expect(filesCreated.length).to.eql(1);
expect(filesCreated).to.include(patterns[pattern].oldfile);
self.time = new Date(patterns[pattern].end);
tk.travel(self.time);
transport.log('error', '3rd test message', {}, function (err) {
if (err) {
done(err);
}
filesCreated = fs.readdirSync(rotationLogPath);
expect(filesCreated.length).to.eql(2);
expect(filesCreated).to.include(patterns[pattern].newfile);
expect(filesCreated).to.include(patterns[pattern].oldfile + '.gz');
transport.close();
done();
});
});
});
});
});
});
});
});
describe('when zippedArchive is true', function () {
var pattern = {
pattern: '.m',
start: 1861947160000, // GMT: Mon, 01 Jan 2029 07:32:50 GMT
mid: 1861947240000, // GMT: Mon, 01 Jan 2029 07:34:00 GMT
end: 1861947760000, // GMT: Mon, 01 Jan 2029 07:42:40 GMT
oldfile: 'test-rotation.log.32',
midfile: 'test-rotation.log.34',
newfile: 'test-rotation.log.42'
};
var transport;
var rotationLogPath = path.join(fixturesDir, 'rotations');
beforeEach(function (done) {
this.time = new Date(pattern.start);
tk.travel(this.time);
rimraf.sync(rotationLogPath);
mkdirp.sync(rotationLogPath);
transport = new DailyRotateFile({
filename: path.join(rotationLogPath, 'test-rotation.log'),
datePattern: pattern.pattern,
zippedArchive: true
});
done();
});
afterEach(function () {
tk.reset();
});
it('should compress logfile even if there is only one log message', function (done) {
var self = this;
transport.log('error', 'test message', {}, function (err) {
if (err) {
done(err);
}
var filesCreated = fs.readdirSync(rotationLogPath);
expect(filesCreated.length).to.eql(1);
expect(filesCreated).to.include(pattern.oldfile);
self.time = new Date(pattern.end);
tk.travel(self.time);
transport.log('error', '2nd test message', {}, function (err) {
if (err) {
done(err);
}
filesCreated = fs.readdirSync(rotationLogPath);
expect(filesCreated.length).to.eql(2);
expect(filesCreated).to.include(pattern.newfile);
expect(filesCreated).to.include(pattern.oldfile + '.gz');
transport.close();
done();
});
});
});
it('should compress logfile without adding count to file extension if there are no files with the same name', function (done) {
var self = this;
transport.log('error', 'test message', {}, function (err) {
if (err) {
done(err);
}
var filesCreated = fs.readdirSync(rotationLogPath);
expect(filesCreated.length).to.eql(1);
expect(filesCreated).to.include(pattern.oldfile);
self.time = new Date(pattern.mid);
tk.travel(self.time);
transport.log('error', '2nd test message', {}, function (err) {
if (err) {
done(err);
}
filesCreated = fs.readdirSync(rotationLogPath);
expect(filesCreated.length).to.eql(2);
expect(filesCreated).to.include(pattern.oldfile + '.gz');
expect(filesCreated).to.include(pattern.midfile);
self.time = new Date(pattern.end);
tk.travel(self.time);
transport.log('error', '3rd test message', {}, function (err) {
if (err) {
done(err);
}
filesCreated = fs.readdirSync(rotationLogPath);
expect(filesCreated.length).to.eql(3);
expect(filesCreated).to.not.include(pattern.newfile + '.1');
expect(filesCreated).to.include(pattern.newfile);
expect(filesCreated).to.include(pattern.midfile + '.gz');
expect(filesCreated).to.include(pattern.oldfile + '.gz');
transport.close();
done();
});
});
});
});
});
});