UNPKG

2.31 kBJavaScriptView Raw
1/*
2 * file-archive-test.js: Tests for instances of the File transport setting the archive option,
3 *
4 * (C) 2015 Nimrod Becker
5 * MIT LICENSE
6 *
7 */
8
9var assert = require('assert'),
10 exec = require('child_process').exec,
11 fs = require('fs'),
12 path = require('path'),
13 vows = require('vows'),
14 winston = require('../../lib/winston'),
15 helpers = require('../helpers');
16
17var archiveTransport = new winston.transports.File({
18 timestamp: true,
19 json: false,
20 zippedArchive: true,
21 tailable: true,
22 filename: 'testarchive.log',
23 dirname: path.join(__dirname, '..', 'fixtures', 'logs'),
24 maxsize: 4096,
25 maxFiles: 3
26});
27
28function data(ch) {
29 return new Array(1018).join(String.fromCharCode(65 + ch));
30}
31
32function logKbytes(kbytes, txt) {
33 //
34 // With no timestamp and at the info level,
35 // winston adds exactly 7 characters:
36 // [info](4)[ :](2)[\n](1)
37 //
38 for (var i = 0; i < kbytes; i++) {
39 archiveTransport.log('info', data(txt), null, function() {});
40 }
41}
42
43vows.describe('winston/transports/file/zippedArchive').addBatch({
44 "An instance of the File Transport with tailable true": {
45 "when created archived files are rolled": {
46 topic: function() {
47 var that = this,
48 created = 0;
49
50 archiveTransport.on('logged', function() {
51 if (++created === 6) {
52 return that.callback();
53 }
54
55 logKbytes(4, created);
56 });
57
58 logKbytes(4, created);
59 },
60 "should be only 3 files called testarchive.log, testarchive1.log.gz and testarchive2.log.gz": function() {
61 //Give the archive a little time to settle
62 // setTimeout(function() {
63 for (var num = 0; num < 6; num++) {
64 var file = !num ? 'testarchive.log' : 'testarchive' + num + '.log.gz',
65 fullpath = path.join(__dirname, '..', 'fixtures', 'logs', file);
66
67 // There should be no files with that name
68 if (num >= 3) {
69 assert.throws(function() {
70 fs.statSync(fullpath);
71 }, Error);
72 } else {
73 // The other files should exist
74 assert.doesNotThrow(function() {
75 fs.statSync(fullpath);
76 }, Error);
77 }
78 }
79 //},5000);
80 },
81 }
82 },
83}).export(module);