Skip to content

Commit

Permalink
colorize error stack trace and add unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
hiro5id committed May 26, 2023
1 parent 8f75bc9 commit 65b3ba9
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/colors/colorize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { jsonStringifySafe } from '../json-stringify-safe/stringify-safe';
export interface IDefaultColorMap {
black: string;
red: string;
darkRed: string;
lightRed: string;
green: string;
darkGreen: string;
lightGreen: string;
Expand All @@ -24,6 +26,8 @@ export interface IDefaultColorMap {
export const defaultColorMap: IDefaultColorMap = {
black: '\x1b[30m',
red: '\x1b[31m',
darkRed: '\x1b[38;2;179;5;15m',
lightRed: '\x1b[38;2;255;137;149m',
green: '\x1b[32m',
darkGreen: '\x1b[38;2;36;119;36m',
lightGreen: '\x1b[38;2;0;255;127m',
Expand Down Expand Up @@ -66,6 +70,8 @@ export interface IColorConfiguration {
packageName: ColorValue;
timestampKey: ColorValue;
timestamp: ColorValue;
errCallStackKey: ColorValue;
errCallStack: ColorValue;
}

export type ColorItemName = keyof IColorConfiguration;
Expand All @@ -92,6 +98,8 @@ export const defaultColors: IColorConfiguration = {
packageName: 'yellow',
timestampKey: 'pink',
timestamp: 'lightPink',
errCallStackKey: 'darkRed',
errCallStack: 'lightRed',
};

// TODO: this is super beta, consider using Sindre's supports-colors
Expand Down Expand Up @@ -156,6 +164,9 @@ export function colorJson(jsonInput: any, colorsInput: Partial<IColorConfigurati
if (/\"@timestamp\"/i.test(match)) {
colorCode = 'timestampKey';
}
if (/\"errCallStack\"/i.test(match)) {
colorCode = 'errCallStackKey';
}
} else {
colorCode = 'string';
// If the key is "level" then handle value with special color
Expand Down Expand Up @@ -192,6 +203,9 @@ export function colorJson(jsonInput: any, colorsInput: Partial<IColorConfigurati
if (/\"@timestamp\"/i.test(previousMatchedValue)) {
colorCode = 'timestamp';
}
if (/\"errCallStack\"/i.test(previousMatchedValue)) {
colorCode = 'errCallStack';
}
}
} else if (/true|false/.test(match)) {
colorCode = 'boolean';
Expand Down
17 changes: 17 additions & 0 deletions test/logger.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1099,6 +1099,23 @@ describe('logger', () => {
expect(outputText[0].startsWith("\u001b[30m{\u001b[0m\u001b[38;2;26;175;192m\"level\":\u001b[30m\u001b[0m\u001b[38;2;31;230;255m\"info\"\u001b[30m,\u001b[0m\u001b[38;2;36;119;36m\"message\":\u001b[30m\u001b[0m\u001b[38;2;0;255;127m\"this is a test - more messages\"\u001b[30m,\u001b[0m\u001b[38;2;159;147;45m\"@filename\":\u001b[30m\u001b[0m\u001b[33m\"")).eql(true);
});

it('color console.log logs with error object', async () => {
sandbox.stub(process.env, 'CONSOLE_LOG_COLORIZE').value('TRUE');
const { originalWrite, outputText } = overrideStdOut();
LoggerAdaptToConsole();

const err = new Error("HEY MAN THIS IS AN ERROR!");
await console.log('this is a test', { a: 'stuff-a', b: 'stuff-b' }, 'more messages', err,{ c: 1234 });

restoreStdOut(originalWrite);
LoggerRestoreConsole();

console.log(outputText[0]);

// const testObj = JSON.parse(outputText[0]);
expect(outputText[0].startsWith("\u001b[30m{\u001b[0m\u001b[38;2;26;175;192m\"level\":\u001b[30m\u001b[0m\u001b[31m\"error\"\u001b[30m,\u001b[0m\u001b[38;2;36;119;36m\"message\":\u001b[30m\u001b[0m\u001b[31m\"this is a test - more messages - HEY MAN THIS IS AN ERROR!\"\u001b[30m,\u001b[0m\u001b[38;2;135;38;162m\"@errorObjectName\":\u001b[30m\u001b[0m\u001b[37m\"Error\"\u001b[30m,\u001b[0m\u001b[38;2;159;147;45m\"@filename\":\u001b[30m\u001b[0m\u001b[33m\"")).eql(true);
});

// Todo: test multiple nested ErrorWithContext objects to ensure proper stacktrace and error messages
});

Expand Down

0 comments on commit 65b3ba9

Please sign in to comment.