Skip to content

Commit

Permalink
Merge pull request #290 from fullstack-build/development
Browse files Browse the repository at this point in the history
Bugfix and dependency updates
  • Loading branch information
terehov authored Jun 8, 2024
2 parents f7474a3 + c7fd912 commit f42d3fb
Show file tree
Hide file tree
Showing 12 changed files with 130 additions and 14 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ logger.fatal(new Error("I am a pretty Error with a stacktrace."));
## All Features

- **Universal:** Works in browsers and Node.js
- **Tested:** 100% code coverage, CI
- **Tested:** Great code coverage, CI
- **Super customizable:** Every aspect can be overwritten
- **Fully typed:** Written in TypeScript, with native TypeScript support
- **Default log level:** `silly`, `trace`, `debug`, `info`, `warn`, `error`, `fatal` (different colors)
Expand Down
4 changes: 2 additions & 2 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ logger.fatal(new Error("I am a pretty Error with a stacktrace."));
## All Features

- **Universal:** Works in browsers and Node.js
- **Tested:** 100% code coverage, CI
- **Tested:** Great code coverage, CI
- **Super customizable:** Every aspect can be overwritten
- **Fully typed:** Written in TypeScript, with native TypeScript support
- **Default log level:** `silly`, `trace`, `debug`, `info`, `warn`, `error`, `fatal` (different colors)
Expand Down Expand Up @@ -686,7 +686,7 @@ For `pretty` logs:
For `JSON` logs (no formatting happens here):
```typescript
const logger = new Logger({
type: "pretty",
type: "json",
overwrite: {
transportJSON: (logObjWithMeta: any) => {
// transport the LogObj to console, StdOut, a file or an external service
Expand Down
11 changes: 11 additions & 0 deletions examples/nodejs/index2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,3 +119,14 @@ class CustomError extends Error {

const err = new CustomError("a", "b");
logger.error(err);

console.log("***********");
logger.debug(null);
logger.debug(undefined);
logger.debug("*", undefined);
console.log("###############");
//jsonLogger.debug(null);
jsonLogger.debug(undefined);
//jsonLogger.debug('*', undefined);
console.log("###############");
logger.debug(new URL("https://www.test.de"));
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,6 +1,6 @@
{
"name": "tslog",
"version": "4.9.1",
"version": "4.9.2",
"description": "Extensible TypeScript Logger for Node.js and Browser.",
"author": "Eugene <opensource@terehov.de> (https://fullstack.build)",
"license": "MIT",
Expand Down
8 changes: 7 additions & 1 deletion src/BaseLogger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,13 @@ export class BaseLogger<LogObj> {
return Object.getOwnPropertyNames(source).reduce((o, prop) => {
o[prop] = keys.includes(this.settings?.maskValuesOfKeysCaseInsensitive !== true ? prop : prop.toLowerCase())
? this.settings.maskPlaceholder
: this._recursiveCloneAndMaskValuesOfKeys((source as Record<string, unknown>)[prop], keys, seen);
: (() => {
try {
return this._recursiveCloneAndMaskValuesOfKeys((source as Record<string, unknown>)[prop], keys, seen);
} catch (e) {
return null;
}
})();
return o;
}, baseObject) as T;
} else {
Expand Down
3 changes: 1 addition & 2 deletions src/runtime/browser/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,7 @@ export function getCallerStackFrame(stackDepthLevel: number, error: Error = Erro
}

export function getErrorTrace(error: Error): IStackFrame[] {
return (error as Error)?.stack
?.split("\n")
return ((error as Error)?.stack?.split("\n") ?? [])
?.filter((line: string) => !line.includes("Error: "))
?.reduce((result: IStackFrame[], line: string) => {
result.push(stackLineToStackFrame(line));
Expand Down
10 changes: 5 additions & 5 deletions src/runtime/browser/util.inspect.polyfil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ function isBoolean(arg: unknown) {
}

function isUndefined(arg: unknown) {
return arg == null;
return arg === undefined;
}

function stylizeNoColor(str: string) {
Expand Down Expand Up @@ -377,19 +377,19 @@ function reduceToSingleString(output: string[], base: string, braces: string[]):
return braces[0] + (base === "" ? "" : base + "\n") + " " + output.join(",\n ") + " " + braces[1];
}

function _extend(origin: object, add: object) {
function _extend(origin: object, add: object): object {
const typedOrigin = { ...origin } as { [key: string]: unknown };
// Don't do anything if add isn't an object
if (!add || !isObject(add)) return origin;

const clonedOrigin = { ...origin } as { [key: string]: unknown };
const clonedAdd = { ...add } as { [key: string]: unknown };

const keys = Object.keys(add);
let i = keys.length;
while (i--) {
clonedOrigin[keys[i]] = clonedAdd[keys[i]];
typedOrigin[keys[i]] = clonedAdd[keys[i]];
}
return origin;
return typedOrigin;
}

export function formatWithOptions(inspectOptions: InspectOptions, ...args: unknown[]) {
Expand Down
3 changes: 3 additions & 0 deletions src/runtime/nodejs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,9 @@ export function transportJSON<LogObj>(json: LogObj & ILogObjMeta): void {
if (typeof value === "bigint") {
return `${value}`;
}
if (typeof value === "undefined") {
return "[undefined]";
}
return value;
});
}
Expand Down
28 changes: 28 additions & 0 deletions tests/Browser/1_json.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,32 @@ describe("Browser: JSON: Log level", () => {

expect(consoleOutput).toContain("Foo bar");
});

it("pretty undefined", async () => {
await page.evaluate(() => {
// @ts-ignore
const logger = new tslog.Logger({ type: "pretty", stylePrettyLogs: false });
logger.info(undefined);
});
expect(consoleOutput).toContain("undefined");
});

it("pretty null", async () => {
await page.evaluate(() => {
// @ts-ignore
const logger = new tslog.Logger({ type: "pretty", stylePrettyLogs: false });
logger.info(null);
});
expect(consoleOutput).toContain("null");
});

it("pretty nullish", async () => {
await page.evaluate(() => {
// @ts-ignore
const logger = new tslog.Logger({ type: "pretty", stylePrettyLogs: false });
logger.info({ foo: null, bar: undefined });
});
expect(consoleOutput).toContain("null");
expect(consoleOutput).toContain("undefined");
});
});
20 changes: 20 additions & 0 deletions tests/Nodejs/4_json_Log_Types.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,26 @@ describe("JSON: Log Types", () => {
expect(getConsoleLog()).toContain('"1":"Test2"');
});

it("pretty undefined", async () => {
const logger = new Logger({ type: "json" });
logger.info(undefined);
expect(getConsoleLog()).toContain('"0":"[undefined]"');
});

it("pretty null", async () => {
const logger = new Logger({ type: "json" });
logger.info(null);
expect(getConsoleLog()).toContain('"0":null');
});

it("pretty nullish", async () => {
const logger = new Logger({ type: "json" });
logger.info({ foo: null, bar: undefined });

expect(getConsoleLog()).toContain('"foo":null');
expect(getConsoleLog()).toContain('"bar":"[undefined]"');
});

test("boolean", (): void => {
const logger = new Logger({ type: "json" });
logger.log(1234, "testLevel", true);
Expand Down
49 changes: 49 additions & 0 deletions tests/Nodejs/5_pretty_Log_Types.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,28 @@ describe("Pretty: Log Types", () => {
expect(getConsoleLog()).toContain("Test1 Test2");
});

it("pretty undefined", async () => {
const logger = new Logger({ type: "pretty", stylePrettyLogs: false });
logger.info(undefined);

expect(getConsoleLog()).toContain("undefined");
});

it("pretty null", async () => {
const logger = new Logger({ type: "pretty", stylePrettyLogs: false });
logger.info(null);

expect(getConsoleLog()).toContain("null");
});

it("pretty nullish", async () => {
const logger = new Logger({ type: "pretty", stylePrettyLogs: false });
logger.info({ foo: null, bar: undefined });

expect(getConsoleLog()).toContain("null");
expect(getConsoleLog()).toContain("undefined");
});

test("boolean", (): void => {
const logger = new Logger({ type: "pretty" });
logger.log(1234, "testLevel", true);
Expand All @@ -45,6 +67,12 @@ describe("Pretty: Log Types", () => {
expect(getConsoleLog()).toContain("555");
});

test("null", (): void => {
const logger = new Logger({ type: "pretty" });
logger.log(1234, "testLevel", null);
expect(getConsoleLog()).toContain("null");
});

test("Array, stylePrettyLogs: false", (): void => {
const logger = new Logger({ type: "pretty", stylePrettyLogs: false });
logger.log(1234, "testLevel", [1, 2, 3, "test"]);
Expand Down Expand Up @@ -94,6 +122,27 @@ describe("Pretty: Log Types", () => {
expect(getConsoleLog()).toContain("https://example2.com/");
});

test("Date", (): void => {
const logger = new Logger({ type: "pretty" });
const date = new Date(0);
logger.log(1234, "testLevel", date);
expect(getConsoleLog()).toContain("1970-01-01T00:00:00.000Z");
});

test("Map", (): void => {
const logger = new Logger({ type: "pretty" });
const map = new Map();
logger.log(1234, "testLevel", map);
expect(getConsoleLog()).toContain("Map(0) {}");
});

test("Set", (): void => {
const logger = new Logger({ type: "pretty" });
const set = new Set();
logger.log(1234, "testLevel", set);
expect(getConsoleLog()).toContain("Set(0) {}");
});

test("String, Object", (): void => {
const logger = new Logger({ type: "pretty" });
logger.log(1234, "testLevel", "test", { test: true, nested: { 1: false } });
Expand Down

0 comments on commit f42d3fb

Please sign in to comment.