fileAppender-test.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. "use strict";
  2. var vows = require('vows')
  3. , fs = require('fs')
  4. , path = require('path')
  5. , sandbox = require('sandboxed-module')
  6. , log4js = require('../lib/log4js')
  7. , assert = require('assert');
  8. log4js.clearAppenders();
  9. function remove(filename) {
  10. try {
  11. fs.unlinkSync(filename);
  12. } catch (e) {
  13. //doesn't really matter if it failed
  14. }
  15. }
  16. vows.describe('log4js fileAppender').addBatch({
  17. 'adding multiple fileAppenders': {
  18. topic: function () {
  19. var listenersCount = process.listeners('exit').length
  20. , logger = log4js.getLogger('default-settings')
  21. , count = 5, logfile;
  22. while (count--) {
  23. logfile = path.join(__dirname, '/fa-default-test' + count + '.log');
  24. log4js.addAppender(require('../lib/appenders/file').appender(logfile), 'default-settings');
  25. }
  26. return listenersCount;
  27. },
  28. 'does not add more than one `exit` listeners': function (initialCount) {
  29. assert.ok(process.listeners('exit').length <= initialCount + 1);
  30. }
  31. },
  32. 'exit listener': {
  33. topic: function() {
  34. var exitListener
  35. , openedFiles = []
  36. , fileAppender = sandbox.require(
  37. '../lib/appenders/file',
  38. {
  39. globals: {
  40. process: {
  41. on: function(evt, listener) {
  42. exitListener = listener;
  43. }
  44. }
  45. },
  46. requires: {
  47. '../streams': {
  48. RollingFileStream: function(filename) {
  49. openedFiles.push(filename);
  50. this.end = function() {
  51. openedFiles.shift();
  52. };
  53. this.on = function() {};
  54. }
  55. }
  56. }
  57. }
  58. );
  59. for (var i=0; i < 5; i += 1) {
  60. fileAppender.appender('test' + i, null, 100);
  61. }
  62. assert.isNotEmpty(openedFiles);
  63. exitListener();
  64. return openedFiles;
  65. },
  66. 'should close all open files': function(openedFiles) {
  67. assert.isEmpty(openedFiles);
  68. }
  69. },
  70. 'with default fileAppender settings': {
  71. topic: function() {
  72. var that = this
  73. , testFile = path.join(__dirname, '/fa-default-test.log')
  74. , logger = log4js.getLogger('default-settings');
  75. remove(testFile);
  76. log4js.clearAppenders();
  77. log4js.addAppender(require('../lib/appenders/file').appender(testFile), 'default-settings');
  78. logger.info("This should be in the file.");
  79. setTimeout(function() {
  80. fs.readFile(testFile, "utf8", that.callback);
  81. }, 100);
  82. },
  83. 'should write log messages to the file': function(err, fileContents) {
  84. assert.include(fileContents, "This should be in the file.\n");
  85. },
  86. 'log messages should be in the basic layout format': function(err, fileContents) {
  87. assert.match(
  88. fileContents,
  89. /\[\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}\.\d{3}\] \[INFO\] default-settings - /
  90. );
  91. }
  92. },
  93. 'with a max file size and no backups': {
  94. topic: function() {
  95. var testFile = path.join(__dirname, '/fa-maxFileSize-test.log')
  96. , logger = log4js.getLogger('max-file-size')
  97. , that = this;
  98. remove(testFile);
  99. remove(testFile + '.1');
  100. //log file of 100 bytes maximum, no backups
  101. log4js.clearAppenders();
  102. log4js.addAppender(
  103. require('../lib/appenders/file').appender(testFile, log4js.layouts.basicLayout, 100, 0),
  104. 'max-file-size'
  105. );
  106. logger.info("This is the first log message.");
  107. logger.info("This is an intermediate log message.");
  108. logger.info("This is the second log message.");
  109. //wait for the file system to catch up
  110. setTimeout(function() {
  111. fs.readFile(testFile, "utf8", that.callback);
  112. }, 100);
  113. },
  114. 'log file should only contain the second message': function(err, fileContents) {
  115. assert.include(fileContents, "This is the second log message.\n");
  116. assert.equal(fileContents.indexOf("This is the first log message."), -1);
  117. },
  118. 'the number of files': {
  119. topic: function() {
  120. fs.readdir(__dirname, this.callback);
  121. },
  122. 'starting with the test file name should be two': function(err, files) {
  123. //there will always be one backup if you've specified a max log size
  124. var logFiles = files.filter(
  125. function(file) { return file.indexOf('fa-maxFileSize-test.log') > -1; }
  126. );
  127. assert.equal(logFiles.length, 2);
  128. }
  129. }
  130. },
  131. 'with a max file size and 2 backups': {
  132. topic: function() {
  133. var testFile = path.join(__dirname, '/fa-maxFileSize-with-backups-test.log')
  134. , logger = log4js.getLogger('max-file-size-backups');
  135. remove(testFile);
  136. remove(testFile+'.1');
  137. remove(testFile+'.2');
  138. //log file of 50 bytes maximum, 2 backups
  139. log4js.clearAppenders();
  140. log4js.addAppender(
  141. require('../lib/appenders/file').appender(testFile, log4js.layouts.basicLayout, 50, 2),
  142. 'max-file-size-backups'
  143. );
  144. logger.info("This is the first log message.");
  145. logger.info("This is the second log message.");
  146. logger.info("This is the third log message.");
  147. logger.info("This is the fourth log message.");
  148. var that = this;
  149. //give the system a chance to open the stream
  150. setTimeout(function() {
  151. fs.readdir(__dirname, function(err, files) {
  152. if (files) {
  153. that.callback(null, files.sort());
  154. } else {
  155. that.callback(err, files);
  156. }
  157. });
  158. }, 200);
  159. },
  160. 'the log files': {
  161. topic: function(files) {
  162. var logFiles = files.filter(
  163. function(file) { return file.indexOf('fa-maxFileSize-with-backups-test.log') > -1; }
  164. );
  165. return logFiles;
  166. },
  167. 'should be 3': function (files) {
  168. assert.equal(files.length, 3);
  169. },
  170. 'should be named in sequence': function (files) {
  171. assert.deepEqual(files, [
  172. 'fa-maxFileSize-with-backups-test.log',
  173. 'fa-maxFileSize-with-backups-test.log.1',
  174. 'fa-maxFileSize-with-backups-test.log.2'
  175. ]);
  176. },
  177. 'and the contents of the first file': {
  178. topic: function(logFiles) {
  179. fs.readFile(path.join(__dirname, logFiles[0]), "utf8", this.callback);
  180. },
  181. 'should be the last log message': function(contents) {
  182. assert.include(contents, 'This is the fourth log message.');
  183. }
  184. },
  185. 'and the contents of the second file': {
  186. topic: function(logFiles) {
  187. fs.readFile(path.join(__dirname, logFiles[1]), "utf8", this.callback);
  188. },
  189. 'should be the third log message': function(contents) {
  190. assert.include(contents, 'This is the third log message.');
  191. }
  192. },
  193. 'and the contents of the third file': {
  194. topic: function(logFiles) {
  195. fs.readFile(path.join(__dirname, logFiles[2]), "utf8", this.callback);
  196. },
  197. 'should be the second log message': function(contents) {
  198. assert.include(contents, 'This is the second log message.');
  199. }
  200. }
  201. }
  202. }
  203. }).addBatch({
  204. 'configure' : {
  205. 'with fileAppender': {
  206. topic: function() {
  207. var log4js = require('../lib/log4js')
  208. , logger;
  209. //this config file defines one file appender (to ./tmp-tests.log)
  210. //and sets the log level for "tests" to WARN
  211. log4js.configure('test/log4js.json');
  212. logger = log4js.getLogger('tests');
  213. logger.info('this should not be written to the file');
  214. logger.warn('this should be written to the file');
  215. fs.readFile('tmp-tests.log', 'utf8', this.callback);
  216. },
  217. 'should load appender configuration from a json file': function(err, contents) {
  218. assert.include(contents, 'this should be written to the file\n');
  219. assert.equal(contents.indexOf('this should not be written to the file'), -1);
  220. }
  221. }
  222. }
  223. }).addBatch({
  224. 'when underlying stream errors': {
  225. topic: function() {
  226. var consoleArgs
  227. , errorHandler
  228. , fileAppender = sandbox.require(
  229. '../lib/appenders/file',
  230. {
  231. globals: {
  232. console: {
  233. error: function() {
  234. consoleArgs = Array.prototype.slice.call(arguments);
  235. }
  236. }
  237. },
  238. requires: {
  239. '../streams': {
  240. RollingFileStream: function(filename) {
  241. this.end = function() {};
  242. this.on = function(evt, cb) {
  243. if (evt === 'error') {
  244. errorHandler = cb;
  245. }
  246. };
  247. }
  248. }
  249. }
  250. }
  251. );
  252. fileAppender.appender('test1.log', null, 100);
  253. errorHandler({ error: 'aargh' });
  254. return consoleArgs;
  255. },
  256. 'should log the error to console.error': function(consoleArgs) {
  257. assert.isNotEmpty(consoleArgs);
  258. assert.equal(consoleArgs[0], 'log4js.fileAppender - Writing to file %s, error happened ');
  259. assert.equal(consoleArgs[1], 'test1.log');
  260. assert.equal(consoleArgs[2].error, 'aargh');
  261. }
  262. }
  263. }).export(module);