| 13 | +function remove(filename) { |
| 14 | + try { |
| 15 | + fs.unlinkSync(filename); |
| 16 | + } catch (e) { |
| 17 | + // doesn't really matter if it failed |
| 18 | + } |
| 19 | +} |
| 20 | + |
| 21 | +test('log4js fileAppender', (batch) => { |
| 22 | + batch.test('adding multiple fileAppenders', (t) => { |
| 23 | + const initialCount = process.listeners('exit').length; |
| 24 | + let count = 5; |
| 25 | + let logfile; |
| 26 | + |
| 27 | + while (count--) { |
| 28 | + logfile = path.join(__dirname, `fa-default-test${count}.log`); |
| 29 | + log4js.addAppender( |
| 30 | + require('../../lib/appenders/file').appender(logfile), |
| 31 | + 'default-settings' |
| 32 | + ); |
| 33 | + } |
| 34 | + |
| 35 | + t.equal(initialCount + 1, process.listeners('exit').length, 'should not add more than one exit listener'); |
| 36 | + t.end(); |
| 37 | + }); |
| 38 | + |
| 39 | + batch.test('exit listener', (t) => { |
| 40 | + let exitListener; |
| 41 | + const openedFiles = []; |
| 42 | + |
| 43 | + const fileAppender = sandbox.require( |
| 44 | + '../../lib/appenders/file', |
| 45 | + { |
| 46 | + globals: { |
| 47 | + process: { |
| 48 | + on: function (evt, listener) { |
| 49 | + if (evt === 'exit') { |
| 50 | + exitListener = listener; |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | + }, |
| 55 | + singleOnly: true, |
| 56 | + requires: { |
| 57 | + streamroller: { |
| 58 | + RollingFileStream: function (filename) { |
| 59 | + openedFiles.push(filename); |
| 60 | + |
| 61 | + this.end = function () { |
| 62 | + openedFiles.shift(); |
| 63 | + }; |
| 64 | + |
| 65 | + this.on = function () { |
| 66 | + }; |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | + ); |
| 72 | + |
| 73 | + for (let i = 0; i < 5; i += 1) { |
| 74 | + fileAppender.appender(`test${i}`, null, 100); |
| 75 | + } |
| 76 | + t.ok(openedFiles); |
| 77 | + exitListener(); |
| 78 | + t.equal(openedFiles.length, 0, 'should close all open files'); |
| 79 | + t.end(); |
| 80 | + }); |
| 81 | + |
| 82 | + batch.test('with default fileAppender settings', (t) => { |
| 83 | + const testFile = path.join(__dirname, 'fa-default-test.log'); |
| 84 | + const logger = log4js.getLogger('default-settings'); |
| 85 | + remove(testFile); |
| 86 | + |
| 87 | + log4js.clearAppenders(); |
| 88 | + log4js.addAppender( |
| 89 | + require('../../lib/appenders/file').appender(testFile), |
| 90 | + 'default-settings' |
| 91 | + ); |
| 92 | + |
| 93 | + logger.info('This should be in the file.'); |
| 94 | + |
| 95 | + setTimeout(() => { |
| 96 | + fs.readFile(testFile, 'utf8', (err, fileContents) => { |
| 97 | + t.include(fileContents, `This should be in the file.${EOL}`); |
| 98 | + t.match( |
| 99 | + fileContents, |
| 100 | + /\[\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}\.\d{3}] \[INFO] default-settings - / |
| 101 | + ); |
| 102 | + t.end(); |
| 103 | + }); |
| 104 | + }, 100); |
| 105 | + }); |
| 106 | + |
| 107 | + batch.test('fileAppender subcategories', (t) => { |
| 108 | + log4js.clearAppenders(); |
| 109 | + |
| 110 | + function addAppender(cat) { |
| 111 | + const testFile = path.join( |
| 112 | + __dirname, |
| 113 | + `fa-subcategories-test-${cat.join('-').replace(/\./g, '_')}.log` |
| 114 | + ); |
| 115 | + remove(testFile); |
| 116 | + log4js.addAppender(require('../../lib/appenders/file').appender(testFile), cat); |
| 117 | + return testFile; |
| 118 | + } |
| 119 | + |
| 120 | + /* eslint-disable camelcase */ |
| 121 | + const file_sub1 = addAppender(['sub1']); |
| 122 | + const file_sub1_sub12$sub1_sub13 = addAppender(['sub1.sub12', 'sub1.sub13']); |
| 123 | + const file_sub1_sub12 = addAppender(['sub1.sub12']); |
| 124 | + const logger_sub1_sub12_sub123 = log4js.getLogger('sub1.sub12.sub123'); |
| 125 | + const logger_sub1_sub13_sub133 = log4js.getLogger('sub1.sub13.sub133'); |
| 126 | + const logger_sub1_sub14 = log4js.getLogger('sub1.sub14'); |
| 127 | + const logger_sub2 = log4js.getLogger('sub2'); |
| 128 | + |
| 129 | + logger_sub1_sub12_sub123.info('sub1_sub12_sub123'); |
| 130 | + logger_sub1_sub13_sub133.info('sub1_sub13_sub133'); |
| 131 | + logger_sub1_sub14.info('sub1_sub14'); |
| 132 | + logger_sub2.info('sub2'); |
| 133 | + |
| 134 | + setTimeout(() => { |
| 135 | + t.test('file contents', (assert) => { |
| 136 | + const fileContents = { |
| 137 | + file_sub1: fs.readFileSync(file_sub1).toString(), |
| 138 | + file_sub1_sub12$sub1_sub13: fs.readFileSync(file_sub1_sub12$sub1_sub13).toString(), |
| 139 | + file_sub1_sub12: fs.readFileSync(file_sub1_sub12).toString() |
| 140 | + }; |
| 141 | + // everything but category 'sub2' |
| 142 | + assert.match( |
| 143 | + fileContents.file_sub1, |
| 144 | + /^(\[\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}\.\d{3}] \[INFO] (sub1.sub12.sub123 - sub1_sub12_sub123|sub1.sub13.sub133 - sub1_sub13_sub133|sub1.sub14 - sub1_sub14)[\s\S]){3}$/ // eslint-disable-line |
| 145 | + ); |
| 146 | + assert.ok( |
| 147 | + fileContents.file_sub1.match(/sub123/) && |
| 148 | + fileContents.file_sub1.match(/sub133/) && |
| 149 | + fileContents.file_sub1.match(/sub14/) |
| 150 | + ); |
| 151 | + assert.ok(!fileContents.file_sub1.match(/sub2/)); |
| 152 | + |
| 153 | + // only catgories starting with 'sub1.sub12' and 'sub1.sub13' |
| 154 | + assert.match( |
| 155 | + fileContents.file_sub1_sub12$sub1_sub13, |
| 156 | + /^(\[\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}\.\d{3}] \[INFO] (sub1.sub12.sub123 - sub1_sub12_sub123|sub1.sub13.sub133 - sub1_sub13_sub133)[\s\S]){2}$/ // eslint-disable-line |
| 157 | + ); |
| 158 | + assert.ok( |
| 159 | + fileContents.file_sub1_sub12$sub1_sub13.match(/sub123/) && |
| 160 | + fileContents.file_sub1_sub12$sub1_sub13.match(/sub133/) |
| 161 | + ); |
| 162 | + assert.ok(!fileContents.file_sub1_sub12$sub1_sub13.match(/sub14|sub2/)); |
| 163 | + |
| 164 | + // only catgories starting with 'sub1.sub12' |
| 165 | + assert.match( |
| 166 | + fileContents.file_sub1_sub12, |
| 167 | + /^(\[\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}\.\d{3}] \[INFO] (sub1.sub12.sub123 - sub1_sub12_sub123)[\s\S]){1}$/ // eslint-disable-line |
| 168 | + ); |
| 169 | + assert.ok(!fileContents.file_sub1_sub12.match(/sub14|sub2|sub13/)); |
| 170 | + assert.end(); |
| 171 | + }); |
| 172 | + t.end(); |
| 173 | + }, 3000); |
| 174 | + }); |
| 175 | + |
| 176 | + batch.test('with a max file size and no backups', (t) => { |
| 177 | + const testFile = path.join(__dirname, 'fa-maxFileSize-test.log'); |
| 178 | + const logger = log4js.getLogger('max-file-size'); |
| 179 | + remove(testFile); |
| 180 | + remove(`${testFile}.1`); |
| 181 | + // log file of 100 bytes maximum, no backups |
| 182 | + log4js.clearAppenders(); |
| 183 | + log4js.addAppender( |
| 184 | + require('../../lib/appenders/file').appender(testFile, log4js.layouts.basicLayout, 100, 0), |
| 185 | + 'max-file-size' |
| 186 | + ); |
| 187 | + logger.info('This is the first log message.'); |
| 188 | + logger.info('This is an intermediate log message.'); |
| 189 | + logger.info('This is the second log message.'); |
| 190 | + // wait for the file system to catch up |
| 191 | + setTimeout(() => { |
| 192 | + fs.readFile(testFile, 'utf8', (err, fileContents) => { |
| 193 | + t.include(fileContents, 'This is the second log message.'); |
| 194 | + t.equal(fileContents.indexOf('This is the first log message.'), -1); |
| 195 | + fs.readdir(__dirname, (e, files) => { |
| 196 | + const logFiles = files.filter( |
| 197 | + file => file.includes('fa-maxFileSize-test.log') |
| 198 | + ); |
| 199 | + t.equal(logFiles.length, 2, 'should be 2 files'); |
| 200 | + t.end(); |
| 201 | + }); |
| 202 | + }); |
| 203 | + }, 100); |
| 204 | + }); |
| 205 | + |
| 206 | + batch.test('with a max file size and 2 backups', (t) => { |
| 207 | + const testFile = path.join(__dirname, 'fa-maxFileSize-with-backups-test.log'); |
| 208 | + const logger = log4js.getLogger('max-file-size-backups'); |
| 209 | + remove(testFile); |
| 210 | + remove(`${testFile}.1`); |
| 211 | + remove(`${testFile}.2`); |
| 212 | + |
| 213 | + // log file of 50 bytes maximum, 2 backups |
| 214 | + log4js.clearAppenders(); |
| 215 | + log4js.addAppender( |
| 216 | + require('../../lib/appenders/file').appender(testFile, log4js.layouts.basicLayout, 50, 2), |
| 217 | + 'max-file-size-backups' |
| 218 | + ); |
| 219 | + logger.info('This is the first log message.'); |
| 220 | + logger.info('This is the second log message.'); |
| 221 | + logger.info('This is the third log message.'); |
| 222 | + logger.info('This is the fourth log message.'); |
| 223 | + // give the system a chance to open the stream |
| 224 | + setTimeout(() => { |
| 225 | + fs.readdir(__dirname, (err, files) => { |
| 226 | + const logFiles = files.sort().filter( |
| 227 | + file => file.includes('fa-maxFileSize-with-backups-test.log') |
| 228 | + ); |
| 229 | + t.equal(logFiles.length, 3); |
| 230 | + t.same(logFiles, [ |
| 231 | + 'fa-maxFileSize-with-backups-test.log', |
| 232 | + 'fa-maxFileSize-with-backups-test.log.1', |
| 233 | + 'fa-maxFileSize-with-backups-test.log.2' |
| 234 | + ]); |
| 235 | + t.test('the contents of the first file', (assert) => { |
| 236 | + fs.readFile(path.join(__dirname, logFiles[0]), 'utf8', (e, contents) => { |
| 237 | + assert.include(contents, 'This is the fourth log message.'); |
| 238 | + assert.end(); |
| 239 | + }); |
| 240 | + }); |
| 241 | + t.test('the contents of the second file', (assert) => { |
| 242 | + fs.readFile(path.join(__dirname, logFiles[1]), 'utf8', (e, contents) => { |
| 243 | + assert.include(contents, 'This is the third log message.'); |
| 244 | + assert.end(); |
| 245 | + }); |
| 246 | + }); |
| 247 | + t.test('the contents of the third file', (assert) => { |
| 248 | + fs.readFile(path.join(__dirname, logFiles[2]), 'utf8', (e, contents) => { |
| 249 | + assert.include(contents, 'This is the second log message.'); |
| 250 | + assert.end(); |
| 251 | + }); |
| 252 | + }); |
| 253 | + t.end(); |
| 254 | + }); |
| 255 | + }, 200); |
| 256 | + }); |
| 257 | + |
| 258 | + batch.test('with a max file size and 2 compressed backups', (t) => { |
| 259 | + const testFile = path.join(__dirname, 'fa-maxFileSize-with-backups-compressed-test.log'); |
| 260 | + const logger = log4js.getLogger('max-file-size-backups'); |
| 261 | + remove(testFile); |
| 262 | + remove(`${testFile}.1.gz`); |
| 263 | + remove(`${testFile}.2.gz`); |
| 264 | + |
| 265 | + // log file of 50 bytes maximum, 2 backups |
| 266 | + log4js.clearAppenders(); |
| 267 | + log4js.addAppender( |
| 268 | + require('../../lib/appenders/file').appender( |
| 269 | + testFile, log4js.layouts.basicLayout, 50, 2, { compress: true } |
| 270 | + ), |
| 271 | + 'max-file-size-backups' |
| 272 | + ); |
| 273 | + logger.info('This is the first log message.'); |
| 274 | + logger.info('This is the second log message.'); |
| 275 | + logger.info('This is the third log message.'); |
| 276 | + logger.info('This is the fourth log message.'); |
| 277 | + // give the system a chance to open the stream |
| 278 | + setTimeout(() => { |
| 279 | + fs.readdir(__dirname, (err, files) => { |
| 280 | + const logFiles = files.sort().filter( |
| 281 | + file => file.includes('fa-maxFileSize-with-backups-compressed-test.log') |
| 282 | + ); |
| 283 | + t.equal(logFiles.length, 3, 'should be 3 files'); |
| 284 | + t.same(logFiles, [ |
| 285 | + 'fa-maxFileSize-with-backups-compressed-test.log', |
| 286 | + 'fa-maxFileSize-with-backups-compressed-test.log.1.gz', |
| 287 | + 'fa-maxFileSize-with-backups-compressed-test.log.2.gz' |
| 288 | + ]); |
| 289 | + t.test('the contents of the first file', (assert) => { |
| 290 | + fs.readFile(path.join(__dirname, logFiles[0]), 'utf8', (e, contents) => { |
| 291 | + assert.include(contents, 'This is the fourth log message.'); |
| 292 | + assert.end(); |
| 293 | + }); |
| 294 | + }); |
| 295 | + t.test('the contents of the second file', (assert) => { |
| 296 | + zlib.gunzip(fs.readFileSync(path.join(__dirname, logFiles[1])), (e, contents) => { |
| 297 | + assert.include(contents.toString('utf8'), 'This is the third log message.'); |
| 298 | + assert.end(); |
| 299 | + }); |
| 300 | + }); |
| 301 | + t.test('the contents of the third file', (assert) => { |
| 302 | + zlib.gunzip(fs.readFileSync(path.join(__dirname, logFiles[2])), (e, contents) => { |
| 303 | + assert.include(contents.toString('utf8'), 'This is the second log message.'); |
| 304 | + assert.end(); |
| 305 | + }); |
| 306 | + }); |
| 307 | + t.end(); |
| 308 | + }); |
| 309 | + }, 1000); |
| 310 | + }); |
| 311 | + |
| 312 | + batch.test('configure with fileAppender', (t) => { |
| 313 | + // this config file defines one file appender (to ./tmp-tests.log) |
| 314 | + // and sets the log level for "tests" to WARN |
| 315 | + log4js.configure('./test/tap/log4js.json'); |
| 316 | + const logger = log4js.getLogger('tests'); |
| 317 | + logger.info('this should not be written to the file'); |
| 318 | + logger.warn('this should be written to the file'); |
| 319 | + |
| 320 | + fs.readFile('tmp-tests.log', 'utf8', (err, contents) => { |
| 321 | + t.include(contents, `this should be written to the file${EOL}`); |
| 322 | + t.equal(contents.indexOf('this should not be written to the file'), -1); |
| 323 | + t.end(); |
| 324 | + }); |
| 325 | + }); |
| 326 | + |
| 327 | + batch.test('when underlying stream errors', (t) => { |
| 328 | + let consoleArgs; |
| 329 | + let errorHandler; |
| 330 | + |
| 331 | + const fileAppender = sandbox.require( |
| 332 | + '../../lib/appenders/file', |
| 333 | + { |
| 334 | + globals: { |
| 335 | + console: { |
| 336 | + error: function () { |
| 337 | + consoleArgs = Array.prototype.slice.call(arguments); |
| 338 | + } |
| 339 | + } |
| 340 | + }, |
| 341 | + requires: { |
| 342 | + streamroller: { |
| 343 | + RollingFileStream: function () { |
| 344 | + this.end = function () { |
| 345 | + }; |
| 346 | + this.on = function (evt, cb) { |
| 347 | + if (evt === 'error') { |
| 348 | + errorHandler = cb; |
| 349 | + } |
| 350 | + }; |
| 351 | + } |
| 352 | + } |
| 353 | + } |
| 354 | + } |
| 355 | + ); |
| 356 | + |
| 357 | + fileAppender.appender('test1.log', null, 100); |
| 358 | + errorHandler({ error: 'aargh' }); |
| 359 | + |
| 360 | + t.test('should log the error to console.error', (assert) => { |
| 361 | + assert.ok(consoleArgs); |
| 362 | + assert.equal(consoleArgs[0], 'log4js.fileAppender - Writing to file %s, error happened '); |
| 363 | + assert.equal(consoleArgs[1], 'test1.log'); |
| 364 | + assert.equal(consoleArgs[2].error, 'aargh'); |
| 365 | + assert.end(); |
| 366 | + }); |
| 367 | + t.end(); |
| 368 | + }); |
| 369 | + |
| 370 | + batch.end(); |
| 371 | +}); |
0 commit comments