file-tailrolling-test.js 2.8 KB

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