Skip to content

Commit

Permalink
Change logger to serialize errors
Browse files Browse the repository at this point in the history
  • Loading branch information
tiagogoncalves89 committed Oct 1, 2020
1 parent b3e550e commit c5759ea
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 3 deletions.
26 changes: 26 additions & 0 deletions package-lock.json

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

5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"license": "MIT",
"devDependencies": {
"@types/callsites": "^3.0.0",
"@types/clone": "^2.1.0",
"@types/faker": "^4.1.12",
"@types/jest": "^26.0.13",
"@types/json-stringify-safe": "^5.0.0",
Expand All @@ -52,7 +53,9 @@
},
"dependencies": {
"callsites": "^3.1.0",
"json-stringify-safe": "^5.0.1"
"clone": "^2.1.2",
"json-stringify-safe": "^5.0.1",
"serialize-error": "^7.0.1"
},
"description": "The simplest json logger. Include typescript definitions.",
"repository": {
Expand Down
14 changes: 12 additions & 2 deletions src/logger-filter.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import clone from "clone";
import stringify from "json-stringify-safe";
import { serializeError } from "serialize-error";
import { DEFAULT_BLACK_LIST } from "./default-black-list";

type TItem = { [key: string]: any };
Expand Down Expand Up @@ -27,8 +29,12 @@ export class LoggerFilter {
}

private filterObject(item: TItem): TItem {
const objectWithoutCircularReference = JSON.parse(stringify(item));
Object.keys(item).forEach((key: string): void => {
item[key] = this.filterItem(key, item[key]);
const innerObject = this.isPlainObject(item[key])
? objectWithoutCircularReference[key]
: item[key];
item[key] = this.filterItem(key, innerObject);
});

return item;
Expand All @@ -39,6 +45,10 @@ export class LoggerFilter {
return this.placeholder;
}

if (item instanceof Error) {
return serializeError(item);
}

if (this.isPlainObject(item)) {
return this.filterObject(item);
}
Expand All @@ -55,7 +65,7 @@ export class LoggerFilter {
}

private clone(item: object): object {
return JSON.parse(stringify(item));
return clone(item);
}

private isOnBlacklist(key: string): boolean {
Expand Down
20 changes: 20 additions & 0 deletions test/logger-filter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,26 @@ describe("LoggerFilter", () => {
expect(parsed).toStrictEqual(expectedResult);
});

it("Should serialize an error", () => {
// Given
const error = new Error("My internal error");
const item = { error };
const expectedResult = {
error: {
message: "My internal error",
name: "Error",
},
};
const filter = new LoggerFilter();

// When
const parsed: any = filter.process(item);

// Then
expect(parsed.error.message).toBe(expectedResult.error.message);
expect(parsed.error.name).toBe(expectedResult.error.name);
});

it("Should replace an item value when it is an array of string", () => {
// Given
const key = faker.random.arrayElement(DEFAULT_BLACK_LIST);
Expand Down

0 comments on commit c5759ea

Please sign in to comment.