dateFileAppender-test.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. "use strict";
  2. var vows = require('vows')
  3. , assert = require('assert')
  4. , path = require('path')
  5. , fs = require('fs')
  6. , sandbox = require('sandboxed-module')
  7. , log4js = require('../lib/log4js');
  8. function removeFile(filename) {
  9. return function() {
  10. fs.unlink(path.join(__dirname, filename), function(err) {
  11. if (err) {
  12. console.log("Could not delete ", filename, err);
  13. }
  14. });
  15. };
  16. }
  17. vows.describe('../lib/appenders/dateFile').addBatch({
  18. 'appender': {
  19. 'adding multiple dateFileAppenders': {
  20. topic: function () {
  21. var listenersCount = process.listeners('exit').length,
  22. dateFileAppender = require('../lib/appenders/dateFile'),
  23. count = 5,
  24. logfile;
  25. while (count--) {
  26. logfile = path.join(__dirname, 'datefa-default-test' + count + '.log');
  27. log4js.addAppender(dateFileAppender.appender(logfile));
  28. }
  29. return listenersCount;
  30. },
  31. teardown: function() {
  32. removeFile('datefa-default-test0.log')();
  33. removeFile('datefa-default-test1.log')();
  34. removeFile('datefa-default-test2.log')();
  35. removeFile('datefa-default-test3.log')();
  36. removeFile('datefa-default-test4.log')();
  37. },
  38. 'should only add one `exit` listener': function (initialCount) {
  39. assert.equal(process.listeners('exit').length, initialCount + 1);
  40. },
  41. },
  42. 'exit listener': {
  43. topic: function() {
  44. var exitListener
  45. , openedFiles = []
  46. , dateFileAppender = sandbox.require(
  47. '../lib/appenders/dateFile',
  48. {
  49. globals: {
  50. process: {
  51. on: function(evt, listener) {
  52. exitListener = listener;
  53. }
  54. }
  55. },
  56. requires: {
  57. '../streams': {
  58. DateRollingFileStream: function(filename) {
  59. openedFiles.push(filename);
  60. this.end = function() {
  61. openedFiles.shift();
  62. };
  63. }
  64. }
  65. }
  66. }
  67. );
  68. for (var i=0; i < 5; i += 1) {
  69. dateFileAppender.appender('test' + i);
  70. }
  71. assert.isNotEmpty(openedFiles);
  72. exitListener();
  73. return openedFiles;
  74. },
  75. 'should close all open files': function(openedFiles) {
  76. assert.isEmpty(openedFiles);
  77. }
  78. },
  79. 'with default settings': {
  80. topic: function() {
  81. var that = this,
  82. testFile = path.join(__dirname, 'date-appender-default.log'),
  83. appender = require('../lib/appenders/dateFile').appender(testFile),
  84. logger = log4js.getLogger('default-settings');
  85. log4js.clearAppenders();
  86. log4js.addAppender(appender, 'default-settings');
  87. logger.info("This should be in the file.");
  88. setTimeout(function() {
  89. fs.readFile(testFile, "utf8", that.callback);
  90. }, 100);
  91. },
  92. teardown: removeFile('date-appender-default.log'),
  93. 'should write to the file': function(contents) {
  94. assert.include(contents, 'This should be in the file');
  95. },
  96. 'should use the basic layout': function(contents) {
  97. assert.match(
  98. contents,
  99. /\[\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}\.\d{3}\] \[INFO\] default-settings - /
  100. );
  101. }
  102. }
  103. }
  104. }).addBatch({
  105. 'configure': {
  106. 'with dateFileAppender': {
  107. topic: function() {
  108. var log4js = require('../lib/log4js')
  109. , logger;
  110. //this config file defines one file appender (to ./date-file-test.log)
  111. //and sets the log level for "tests" to WARN
  112. log4js.configure('test/with-dateFile.json');
  113. logger = log4js.getLogger('tests');
  114. logger.info('this should not be written to the file');
  115. logger.warn('this should be written to the file');
  116. fs.readFile(path.join(__dirname, 'date-file-test.log'), 'utf8', this.callback);
  117. },
  118. teardown: removeFile('date-file-test.log'),
  119. 'should load appender configuration from a json file': function(err, contents) {
  120. assert.include(contents, 'this should be written to the file' + require('os').EOL);
  121. assert.equal(contents.indexOf('this should not be written to the file'), -1);
  122. }
  123. },
  124. 'with options.alwaysIncludePattern': {
  125. topic: function() {
  126. var self = this
  127. , log4js = require('../lib/log4js')
  128. , format = require('../lib/date_format')
  129. , logger
  130. , options = {
  131. "appenders": [
  132. {
  133. "category": "tests",
  134. "type": "dateFile",
  135. "filename": "test/date-file-test",
  136. "pattern": "-from-MM-dd.log",
  137. "alwaysIncludePattern": true,
  138. "layout": {
  139. "type": "messagePassThrough"
  140. }
  141. }
  142. ]
  143. }
  144. , thisTime = format.asString(options.appenders[0].pattern, new Date());
  145. fs.writeFileSync(
  146. path.join(__dirname, 'date-file-test' + thisTime),
  147. "this is existing data" + require('os').EOL,
  148. 'utf8'
  149. );
  150. log4js.clearAppenders();
  151. log4js.configure(options);
  152. logger = log4js.getLogger('tests');
  153. logger.warn('this should be written to the file with the appended date');
  154. this.teardown = removeFile('date-file-test' + thisTime);
  155. //wait for filesystem to catch up
  156. setTimeout(function() {
  157. fs.readFile(path.join(__dirname, 'date-file-test' + thisTime), 'utf8', self.callback);
  158. }, 100);
  159. },
  160. 'should create file with the correct pattern': function(contents) {
  161. assert.include(contents, 'this should be written to the file with the appended date');
  162. },
  163. 'should not overwrite the file on open (bug found in issue #132)': function(contents) {
  164. assert.include(contents, 'this is existing data');
  165. }
  166. },
  167. 'with cwd option': {
  168. topic: function() {
  169. var fileOpened,
  170. appender = sandbox.require(
  171. '../lib/appenders/dateFile',
  172. { requires:
  173. { '../streams':
  174. { DateRollingFileStream:
  175. function(file) {
  176. fileOpened = file;
  177. return {
  178. on: function() {},
  179. end: function() {}
  180. };
  181. }
  182. }
  183. }
  184. }
  185. );
  186. appender.configure(
  187. {
  188. filename: "whatever.log",
  189. maxLogSize: 10
  190. },
  191. { cwd: '/absolute/path/to' }
  192. );
  193. return fileOpened;
  194. },
  195. 'should prepend options.cwd to config.filename': function(fileOpened) {
  196. assert.equal(fileOpened, "/absolute/path/to/whatever.log");
  197. }
  198. }
  199. }
  200. }).exportTo(module);