file-archive-test.js 2.3 KB

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