[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 extra pretty options

* Test redact CWD option
* Test level padding option

FoxxMD (Mar 11, 2024, 2:17 PM EDT) 34a8569a adb983d5

+74 -45
+74 -45
tests/index.test.ts
··· 1 1 import {describe} from "mocha"; 2 - import {parseLogOptions, Logger, childLogger} from '../src/index.js'; 2 + import { 3 + parseLogOptions, 4 + Logger, 5 + childLogger, 6 + LogOptions, 7 + LoggerAppExtras, 8 + FileLogOptions, 9 + LogLevelStreamEntry, loggerAppRolling, loggerApp 10 + } from '../src/index.js'; 3 11 import {PassThrough, Transform} from "node:stream"; 4 12 import chai, {expect} from "chai"; 5 13 import {pEvent} from 'p-event'; ··· 80 88 ]); 81 89 }; 82 90 83 - const testRollingAppLogger = async (config?: object): Promise<[Logger, Transform, Transform]> => { 91 + const testRollingAppLogger = async (config: LogOptions | object = {}, extras: LoggerAppExtras = {}): Promise<[Logger, Transform, Transform]> => { 84 92 const opts = parseLogOptions(config, process.cwd()); 85 - const { 86 - file: { 87 - path: logPath, 88 - level, 89 - frequency, 90 - ...rest 91 - } = {} 92 - } = opts; 93 93 const testStream = new PassThrough(); 94 94 const rawStream = new PassThrough(); 95 - const streamEntry = await buildDestinationRollingFile( 96 - level, 97 - { 98 - frequency, 99 - path: logPath, 100 - ...opts, 101 - ...rest 102 - } 103 - ); 104 - const logger = buildLogger('debug', [ 95 + const streams: LogLevelStreamEntry[] = [ 105 96 buildDestinationStream( 106 97 opts.console, 107 98 { ··· 113 104 { 114 105 level: opts.console, 115 106 stream: rawStream 116 - }, 117 - buildDestinationStream(opts.console, { destination: 1, colorize: false }), 118 - streamEntry 119 - ]); 107 + } 108 + ]; 109 + const {destinations = [], ...restExtras} = extras; 110 + const logger = await loggerAppRolling({...opts, console: 'silent'}, {destinations: [...destinations, ...streams], ...restExtras}); 120 111 return [logger, testStream, rawStream]; 121 112 } 122 113 123 - const testAppLogger = (config?: object): [Logger, Transform, Transform] => { 114 + const testAppLogger = (config: LogOptions | object = {}, extras: LoggerAppExtras = {}): [Logger, Transform, Transform] => { 124 115 const opts = parseLogOptions(config, process.cwd()); 125 - const { 126 - file: { 127 - path: logPath, 128 - level, 129 - ...rest 130 - } = {} 131 - } = opts; 132 116 const testStream = new PassThrough(); 133 117 const rawStream = new PassThrough(); 134 - const streamEntry = buildDestinationFile( 135 - level, 136 - { 137 - path: logPath, 138 - ...opts, 139 - ...rest 140 - } 141 - ); 142 - const logger = buildLogger('debug', [ 118 + const {destinations = [], pretty = {}, ...restExtras} = extras; 119 + 120 + const streams = [ 143 121 buildDestinationStream( 144 122 opts.console, 145 123 { 124 + ...pretty, 146 125 destination: testStream, 147 126 colorize: false, 148 - ...opts 127 + ...opts, 149 128 } 150 129 ), 151 130 { 152 131 level: opts.console, 153 132 stream: rawStream 154 133 }, 155 - buildDestinationStream(opts.console, { destination: 1, colorize: false }), 156 - streamEntry 157 - ]); 134 + ]; 135 + 136 + const logger = loggerApp({...opts, console: 'silent'}, {destinations: [...destinations, ...streams], ...pretty, ...restExtras}); 158 137 return [logger, testStream, rawStream]; 159 138 } 160 139 ··· 535 514 }); 536 515 }); 537 516 }); 517 + 518 + describe('Pretty Options', function() { 519 + it('Redacts CWD by default', async function () { 520 + const [logger, testStream, rawStream] = testAppLogger({file: false}); 521 + const formattedBuff = pEvent(testStream, 'data'); 522 + const rawBuff = pEvent(rawStream, 'data'); 523 + const subPath = '/a/subfolder/to/file.txt'; 524 + const cwdPath = path.join(process.cwd(), subPath); 525 + 526 + logger.debug(`An example with current working directory substr ${cwdPath}`); 527 + await sleep(10); 528 + const formatted = (await formattedBuff).toString(); 529 + expect(formatted).does.not.includes(process.cwd()); 530 + expect(formatted).includes(`${path.join('CWD', subPath)}`); 531 + }); 532 + 533 + it('Retains CWD when configured', async function () { 534 + const [logger, testStream, rawStream] = testAppLogger({file: false}, {pretty: {redactCwd: false}}); 535 + const formattedBuff = pEvent(testStream, 'data'); 536 + const subPath = '/a/subfolder/to/file.txt'; 537 + const cwdPath = path.join(process.cwd(), subPath); 538 + 539 + logger.debug(`An example with current working directory substr ${cwdPath}`); 540 + await sleep(10); 541 + const formatted = (await formattedBuff).toString(); 542 + expect(formatted).includes(cwdPath); 543 + }); 544 + 545 + it('Pads levels by default', async function () { 546 + const [logger, testStream, rawStream] = testAppLogger({file: false}); 547 + const formattedBuff = pEvent(testStream, 'data'); 548 + const rawBuff = pEvent(rawStream, 'data'); 549 + 550 + logger.debug(`Test padding`); 551 + await sleep(10); 552 + const formatted = (await formattedBuff).toString(); 553 + expect(formatted).includes('DEBUG :'); 554 + }); 555 + 556 + it('Does not Pad levels when configured', async function () { 557 + const [logger, testStream, rawStream] = testAppLogger({file: false}, {pretty: {padLevels: false}}); 558 + const formattedBuff = pEvent(testStream, 'data'); 559 + const rawBuff = pEvent(rawStream, 'data'); 560 + 561 + logger.debug(`Test padding`); 562 + await sleep(10); 563 + const formatted = (await formattedBuff).toString(); 564 + expect(formatted).includes('DEBUG:'); 565 + }); 566 + });