Skip to content

Commit

Permalink
feat: Colorize stack traces different from error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
FoxxMD committed Mar 21, 2024
1 parent 378f9d8 commit dd4c8d8
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 6 deletions.
Binary file modified assets/example.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@foxxmd/logging",
"type": "module",
"version": "0.1.11",
"version": "0.1.12",
"repository": "https://github.com/foxxmd/logging",
"description": "A typed, opinionated, batteries-included, Pino-based logging solution for backend TS/JS projects",
"scripts": {
Expand Down
3 changes: 2 additions & 1 deletion src/pretty.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ export const prettyOptsFactory = (opts: PrettyOptionsExtra = {}): PrettyOptions
const labels: string[] = log.labels as string[] ?? [];
const labelContent = labels.length === 0 ? '' : `${labels.map((x: string) => colors.blackBright(`[${x}]`)).join(' ')} `;
const msg = redactFunc((log[messageKey] as string));
const stackTrace = log.err !== undefined ? redactFunc(`\n${(log.err as any).stack}`) : '';
let stackTrace = log.err !== undefined ? redactFunc(`\n${(log.err as any).stack}`) : '';
stackTrace = stackTrace.replaceAll(/^\s+at\s.+$/gm, (match) => `${colors.blackBright(match)}`) //$&
return `${labelContent}${msg}${stackTrace}`;
},
hideObject: false,
Expand Down
20 changes: 18 additions & 2 deletions tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import dateFormatDef from "dateformat";
const dateFormat = dateFormatDef as unknown as typeof dateFormatDef.default;


const testConsoleLogger = (config?: object): [Logger, Transform, Transform] => {
const testConsoleLogger = (config?: object, colorize = false): [Logger, Transform, Transform] => {
const opts = parseLogOptions(config, process.cwd());
const testStream = new PassThrough();
const rawStream = new PassThrough();
Expand All @@ -37,7 +37,7 @@ const testConsoleLogger = (config?: object): [Logger, Transform, Transform] => {
opts.console,
{
destination: testStream,
colorize: false,
colorize,
...opts
}
),
Expand Down Expand Up @@ -587,6 +587,22 @@ describe('Child Logger', function() {
});
});

describe('Pretty Features', function () {
const [logger, testStream, rawStream] = testConsoleLogger(undefined, true);
it('colors error stack traces', async function () {
const formattedBuff = pEvent(testStream, 'data');

const rootError = new Error('The root error message');
const topError = new Error('The top error message', {cause: rootError});

logger.error(topError);
await sleep(10);
const formatted = (await formattedBuff).toString();
expect(formatted).to.not.be.undefined;
expect(formatted.match(/^\s+at\s.+$/gm)).to.be.null;
});
});

describe('Pretty Options', function() {
it('Redacts CWD by default', async function () {
const [logger, testStream, rawStream] = testAppLogger({file: false});
Expand Down

0 comments on commit dd4c8d8

Please sign in to comment.