[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.

feat: Colorize stack traces different from error messages

FoxxMD (Mar 21, 2024, 2:51 PM EDT) dd4c8d89 378f9d83

+23 -6
assets/example.png

This is a binary file and will not be displayed.

+2 -2
package-lock.json
··· 1 1 { 2 2 "name": "@foxxmd/logging", 3 - "version": "0.1.11", 3 + "version": "0.1.12", 4 4 "lockfileVersion": 3, 5 5 "requires": true, 6 6 "packages": { 7 7 "": { 8 8 "name": "@foxxmd/logging", 9 - "version": "0.1.11", 9 + "version": "0.1.12", 10 10 "license": "MIT", 11 11 "dependencies": { 12 12 "pino": "^8.19.0",
+1 -1
package.json
··· 1 1 { 2 2 "name": "@foxxmd/logging", 3 3 "type": "module", 4 - "version": "0.1.11", 4 + "version": "0.1.12", 5 5 "repository": "https://github.com/foxxmd/logging", 6 6 "description": "A typed, opinionated, batteries-included, Pino-based logging solution for backend TS/JS projects", 7 7 "scripts": {
+2 -1
src/pretty.ts
··· 41 41 const labels: string[] = log.labels as string[] ?? []; 42 42 const labelContent = labels.length === 0 ? '' : `${labels.map((x: string) => colors.blackBright(`[${x}]`)).join(' ')} `; 43 43 const msg = redactFunc((log[messageKey] as string)); 44 - const stackTrace = log.err !== undefined ? redactFunc(`\n${(log.err as any).stack}`) : ''; 44 + let stackTrace = log.err !== undefined ? redactFunc(`\n${(log.err as any).stack}`) : ''; 45 + stackTrace = stackTrace.replaceAll(/^\s+at\s.+$/gm, (match) => `${colors.blackBright(match)}`) //$& 45 46 return `${labelContent}${msg}${stackTrace}`; 46 47 }, 47 48 hideObject: false,
+18 -2
tests/index.test.ts
··· 28 28 const dateFormat = dateFormatDef as unknown as typeof dateFormatDef.default; 29 29 30 30 31 - const testConsoleLogger = (config?: object): [Logger, Transform, Transform] => { 31 + const testConsoleLogger = (config?: object, colorize = false): [Logger, Transform, Transform] => { 32 32 const opts = parseLogOptions(config, process.cwd()); 33 33 const testStream = new PassThrough(); 34 34 const rawStream = new PassThrough(); ··· 37 37 opts.console, 38 38 { 39 39 destination: testStream, 40 - colorize: false, 40 + colorize, 41 41 ...opts 42 42 } 43 43 ), ··· 584 584 expect(formatted).includes(' [Parent] '); 585 585 expect(formatted).includes(' [Runtime] '); 586 586 }); 587 + }); 588 + }); 589 + 590 + describe('Pretty Features', function () { 591 + const [logger, testStream, rawStream] = testConsoleLogger(undefined, true); 592 + it('colors error stack traces', async function () { 593 + const formattedBuff = pEvent(testStream, 'data'); 594 + 595 + const rootError = new Error('The root error message'); 596 + const topError = new Error('The top error message', {cause: rootError}); 597 + 598 + logger.error(topError); 599 + await sleep(10); 600 + const formatted = (await formattedBuff).toString(); 601 + expect(formatted).to.not.be.undefined; 602 + expect(formatted.match(/^\s+at\s.+$/gm)).to.be.null; 587 603 }); 588 604 }); 589 605