UNPKG

2.85 kBJavaScriptView Raw
1var assert = require('assert'),
2 fs = require('fs'),
3 path = require('path'),
4 vows = require('vows'),
5 winston = require('../../lib/winston'),
6 helpers = require('../helpers');
7
8var maxfilesTransport = new winston.transports.File({
9 timestamp: false,
10 json: false,
11 filename: path.join(__dirname, '..', 'fixtures', 'logs', 'testtailrollingfiles.log'),
12 maxsize: 4096,
13 maxFiles: 3,
14 tailable: true
15});
16
17process.on('uncaughtException', function (err) {
18 console.log('caught exception');
19 console.error(err);
20});
21
22vows.describe('winston/transports/file/tailrolling').addBatch({
23 "An instance of the File Transport": {
24 "when delete old test files": {
25 topic: function () {
26 var logs = path.join(__dirname, '..', 'fixtures', 'logs');
27 fs.readdirSync(logs).forEach(function (file) {
28 if (~file.indexOf('testtailrollingfiles')) {
29 fs.unlinkSync(path.join(logs, file));
30 }
31 });
32
33 this.callback();
34 },
35 "and when passed more files than the maxFiles": {
36 topic: function () {
37 var that = this,
38 created = 0;
39
40 function data(ch) {
41 return new Array(1018).join(String.fromCharCode(65 + ch));
42 };
43
44 function logKbytes(kbytes, txt) {
45 //
46 // With no timestamp and at the info level,
47 // winston adds exactly 7 characters:
48 // [info](4)[ :](2)[\n](1)
49 //
50 for (var i = 0; i < kbytes; i++) {
51 maxfilesTransport.log('info', data(txt), null, function () { });
52 }
53 }
54
55 maxfilesTransport.on('logged', function () {
56 if (++created == 4) {
57 return that.callback();
58 }
59
60 logKbytes(4, created);
61 });
62
63 logKbytes(4, created);
64 },
65 "should be 3 log files, base to maxFiles - 1": function () {
66 var file, fullpath;
67 for (var num = 0; num < 4; num++) {
68 file = !num ? 'testtailrollingfiles.log' : 'testtailrollingfiles' + num + '.log';
69 fullpath = path.join(__dirname, '..', 'fixtures', 'logs', file);
70
71 if (num == 3) {
72 return assert.ok(!fs.existsSync(fullpath));
73 }
74
75 assert.ok(fs.existsSync(fullpath));
76 }
77
78 return false;
79 },
80 "should have files in correct order": function () {
81 var file, fullpath, content;
82 ['D', 'C', 'B'].forEach(function (letter, i) {
83 file = !i ? 'testtailrollingfiles.log' : 'testtailrollingfiles' + i + '.log';
84 content = fs.readFileSync(path.join(__dirname, '..', 'fixtures', 'logs', file), 'ascii');
85
86 assert.lengthOf(content.match(new RegExp(letter, 'g')), 4068);
87 });
88 }
89 }
90 }
91 }
92}).export(module);
\No newline at end of file