[READ-ONLY] Mirror of https://github.com/FoxxMD/logging. A typed, opinionated, batteries-included, Pino-based logging solution for backend TS/JS projects foxxmd.github.io/logging
child-logger logging logging-library nodejs pinojs typescript-library
0

Configure Feed

Select the types of activity you want to include in your feed.

test: Add log file options testing

* Fix test logger options spread
* test file path
* test rolling file path/func
* test rolling file frequency/size/timestamp options

FoxxMD (Mar 11, 2024, 1:42 PM EDT) c74fd47a f99c8aa6

+109 -6
+109 -6
tests/index.test.ts
··· 40 40 file: { 41 41 level, 42 42 path: logPath, 43 - frequency 43 + frequency, 44 + ...rest 44 45 } = {} 45 46 } = opts; 46 47 const streamEntry = await buildDestinationRollingFile( ··· 48 49 { 49 50 frequency, 50 51 path: logPath, 51 - ...opts 52 + ...opts, 53 + ...rest 52 54 } 53 55 ); 54 56 return buildLogger('debug', [ ··· 62 64 file: { 63 65 path: logPath, 64 66 level, 67 + ...rest 65 68 } = {} 66 69 } = opts; 67 70 const streamEntry = buildDestinationFile( 68 71 level, 69 72 { 70 73 path: logPath, 71 - ...opts 74 + ...opts, 75 + ...rest 72 76 } 73 77 ); 74 78 return buildLogger('debug', [ ··· 82 86 file: { 83 87 path: logPath, 84 88 level, 85 - frequency 89 + frequency, 90 + ...rest 86 91 } = {} 87 92 } = opts; 88 93 const testStream = new PassThrough(); ··· 92 97 { 93 98 frequency, 94 99 path: logPath, 95 - ...opts 100 + ...opts, 101 + ...rest 96 102 } 97 103 ); 98 104 const logger = buildLogger('debug', [ ··· 120 126 file: { 121 127 path: logPath, 122 128 level, 129 + ...rest 123 130 } = {} 124 131 } = opts; 125 132 const testStream = new PassThrough(); ··· 128 135 level, 129 136 { 130 137 path: logPath, 131 - ...opts 138 + ...opts, 139 + ...rest 132 140 } 133 141 ); 134 142 const logger = buildLogger('debug', [ ··· 249 257 expect(readdirSync('./logs').length).eq(1); 250 258 }, {unsafeCleanup: true}); 251 259 }); 260 + 261 + it('Writes to specified file', async function () { 262 + await withLocalTmpDir(async () => { 263 + const logger = await testFileRollingLogger({file: { path: './myLogs.log' }}); 264 + logger.debug('Test'); 265 + await sleep(20); 266 + const files = readdirSync('.'); 267 + expect(files.length).eq(1); 268 + expect(files[0]).includes('myLogs') 269 + }, {unsafeCleanup: true}); 270 + }); 271 + 272 + it('Writes to specified file with string function', async function () { 273 + await withLocalTmpDir(async () => { 274 + const logger = await testFileRollingLogger({file: { path: () => path.resolve(process.cwd(), './logs/thisFile.log') }}); 275 + logger.debug('Test'); 276 + await sleep(20); 277 + const files = readdirSync('./logs'); 278 + expect(files.length).eq(1); 279 + expect(files[0]).eq('thisFile.1.log') 280 + }, {unsafeCleanup: true}); 281 + }); 282 + 283 + it('Does not write frequency pattern if only size is specified', async function () { 284 + await withLocalTmpDir(async () => { 285 + const logger = await testFileRollingLogger({file: { path: './myLogs.log', size: '10M' }}); 286 + logger.debug('Test'); 287 + await sleep(20); 288 + const files = readdirSync('.'); 289 + expect(files.length).eq(1); 290 + expect(files[0]).eq('myLogs.1.log') 291 + }, {unsafeCleanup: true}); 292 + }); 293 + 294 + describe('Frequency', function() { 295 + 296 + it('Writes to specified file with daily frequency when timestamp is auto', async function () { 297 + const dt = new Date().toISOString().split('T')[0]; 298 + await withLocalTmpDir(async () => { 299 + const logger = await testFileRollingLogger({file: { path: './myLogs.log', frequency: 'daily' }}); 300 + logger.debug('Test'); 301 + await sleep(20); 302 + const files = readdirSync('.'); 303 + expect(files.length).eq(1); 304 + expect(files[0]).eq(`myLogs-${dt}.1.log`) 305 + }, {unsafeCleanup: true}); 306 + }); 307 + 308 + it('Writes to specified file with iso for other frequencies when timestamp is auto', async function () { 309 + const dt = new Date().toISOString().substring(0, 19); 310 + await withLocalTmpDir(async () => { 311 + const logger = await testFileRollingLogger({file: { path: './myLogs.log', frequency: 'hourly' }}); 312 + logger.debug('Test'); 313 + await sleep(20); 314 + const files = readdirSync('.'); 315 + expect(files.length).eq(1); 316 + expect(files[0]).includes(`myLogs-${dt}`) 317 + }, {unsafeCleanup: true}); 318 + }); 319 + 320 + it('Writes to specified file with unix timestamp when configured', async function () { 321 + const dt = Date.now().toString().substring(0,11); 322 + await withLocalTmpDir(async () => { 323 + const logger = await testFileRollingLogger({file: { path: './myLogs.log', frequency: 'hourly', timestamp: 'unix' }}); 324 + logger.debug('Test'); 325 + await sleep(20); 326 + const files = readdirSync('.'); 327 + expect(files.length).eq(1); 328 + expect(files[0]).includes(`myLogs-${dt}`) 329 + }, {unsafeCleanup: true}); 330 + }); 331 + 332 + it('Writes to specified file with iso when timestamp is iso', async function () { 333 + const dt = new Date().toISOString().substring(0, 19); 334 + await withLocalTmpDir(async () => { 335 + const logger = await testFileRollingLogger({file: { path: './myLogs.log', frequency: 'daily', timestamp: 'iso' }}); 336 + logger.debug('Test'); 337 + await sleep(20); 338 + const files = readdirSync('.'); 339 + expect(files.length).eq(1); 340 + expect(files[0]).includes(`myLogs-${dt}`) 341 + }, {unsafeCleanup: true}); 342 + }); 343 + }); 252 344 }); 253 345 254 346 describe('File', async function () { ··· 268 360 logger.debug('Test'); 269 361 await sleep(20); 270 362 expect(readdirSync('./logs').length).eq(1); 363 + }, {unsafeCleanup: true}); 364 + }); 365 + 366 + it('Writes to specified file path', async function () { 367 + await withLocalTmpDir(async () => { 368 + const logger = await testFileLogger({file: { path: './myLogs.log' }}); 369 + logger.debug('Test'); 370 + await sleep(20); 371 + const files = readdirSync('.'); 372 + expect(files.length).eq(1); 373 + expect(files[0]).eq('myLogs.log') 271 374 }, {unsafeCleanup: true}); 272 375 }); 273 376 });