Skip to content

Commit

Permalink
Add better Buffer and Date support with Tests, also fix path ti types.
Browse files Browse the repository at this point in the history
…Fix #178
  • Loading branch information
terehov committed Nov 22, 2022
1 parent 31096c0 commit 91c0378
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 5 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
},
"main": "./dist/nodejs/cjs/index.js",
"module": "./dist/nodejs/esm/index.js",
"types": "./dist/nodejs/types/index.d.ts",
"types": "./dist/types/index.d.ts",
"browser": {
"tslog": "./dist/browser/index.js",
"util": false,
Expand Down
7 changes: 3 additions & 4 deletions src/BaseLogger.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getMeta, getErrorTrace, transportFormatted, transportJSON, prettyFormatLogObj, IMeta, isError } from "./runtime/nodejs";
import { getMeta, getErrorTrace, transportFormatted, transportJSON, prettyFormatLogObj, IMeta, isError, isBuffer } from "./runtime/nodejs";
import { formatTemplate } from "./formatTemplate";
import { formatNumberAddZeros } from "./formatNumberAddZeros";
import { ISettingsParam, ISettings, ILogObjMeta, ILogObj, IErrorObject } from "./interfaces";
Expand Down Expand Up @@ -233,10 +233,9 @@ export class BaseLogger<LogObj> {

private _toLogObj(args: unknown[], clonedLogObj: LogObj = {} as LogObj): LogObj {
args = args?.map((arg) => (isError(arg) ? this._toErrorObject(arg as Error) : arg));

if (this.settings.argumentsArrayName == null) {
if (args.length === 1) {
clonedLogObj = typeof args[0] === "object" && !Array.isArray(args[0]) ? { ...args[0], ...clonedLogObj } : { 0: args[0], ...clonedLogObj };
if (args.length === 1 && !Array.isArray(args[0]) && isBuffer(args[0]) !== true && !(args[0] instanceof Date)) {
clonedLogObj = typeof args[0] === "object" ? { ...args[0], ...clonedLogObj } : { 0: args[0], ...clonedLogObj };
} else {
clonedLogObj = { ...clonedLogObj, ...args };
}
Expand Down
4 changes: 4 additions & 0 deletions src/runtime/browser/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,3 +172,7 @@ export function transportFormatted<LogObj>(logMetaMarkup: string, logArgs: unkno
export function transportJSON<LogObj>(json: LogObj & ILogObjMeta): void {
console.log(JSON.stringify(json, null, 2));
}

export function isBuffer(arg: unknown) {
return undefined;
}
4 changes: 4 additions & 0 deletions src/runtime/nodejs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,3 +162,7 @@ export function transportJSON<LogObj>(json: LogObj & ILogObjMeta): void {
);
}
}

export function isBuffer(arg: unknown) {
return Buffer.isBuffer(arg);
}
19 changes: 19 additions & 0 deletions tests/Nodejs/4_json_Log_Types.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,16 @@ describe("JSON: Log Types", () => {
3,`);
});

test("Buffer", (): void => {
const logger = new Logger({ type: "json" });
const buffer = Buffer.from("foo");
const log1 = logger.log(1234, "testLevel", buffer);
expect(getConsoleLog()).toContain(`"Buffer"`);
expect(log1?.["0"]).toBe(buffer);
const log2 = logger.log(1234, "testLevel", "1", buffer);
expect(log2?.["1"]).toBe(buffer);
});

test("Object", (): void => {
const logger = new Logger({ type: "json" });
logger.log(1234, "testLevel", { test: true, nested: { 1: false } });
Expand All @@ -52,6 +62,15 @@ describe("JSON: Log Types", () => {
"_meta": {`);
});

test("Date", (): void => {
const logger = new Logger({ type: "json" });
const date = new Date(0);
const log1 = logger.log(1234, "testLevel", date);
console.log("***" + log1?.["0"]);
expect(log1?.["0"]).toBe(date);
expect(getConsoleLog()).toContain(`"1970-01-01T00:00:00.000Z"`);
});

test("String, Object", (): void => {
const logger = new Logger({ type: "json" });
logger.log(1234, "testLevel", "test", { test: true, nested: { 1: false } });
Expand Down
16 changes: 16 additions & 0 deletions tests/Nodejs/5_pretty_Log_Types.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,15 @@ describe("Pretty: Log Types", () => {
expect(getConsoleLog()).toContain("\n]");
});

test("Buffer", (): void => {
const logger = new Logger({ type: "pretty" });
const buffer = Buffer.from("foo");
logger.log(1234, "testLevel", buffer);
expect(getConsoleLog()).toContain(`<Buffer 66 6f 6f>`);
logger.log(1234, "testLevel", "1", buffer);
expect(getConsoleLog()).toContain(`1 <Buffer 66 6f 6f>`);
});

test("Object", (): void => {
const logger = new Logger({ type: "pretty" });
logger.log(1234, "testLevel", { test: true, nested: { 1: false } });
Expand All @@ -58,6 +67,13 @@ describe("Pretty: Log Types", () => {
}`);
});

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("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 91c0378

Please sign in to comment.