Skip to content

Commit

Permalink
feat(logger): add timestamp to Log
Browse files Browse the repository at this point in the history
  • Loading branch information
valpinkman committed Feb 14, 2024
1 parent 43a4745 commit de83371
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 14 deletions.
2 changes: 1 addition & 1 deletion packages/core/src/api/Logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export type {
LogContext,
LogData,
LoggerSubscriber,
LogMessages,
LogMessage,
} from "@internal/logger/service/Log";

export class ConsoleLogger implements LoggerSubscriber {
Expand Down
27 changes: 25 additions & 2 deletions packages/core/src/internal/logger/service/Log.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,36 @@
import { Log } from "./Log";

const d = jest.spyOn(Date, "now").mockReturnValue(12346);

describe("Log", () => {
beforeEach(() => {
d.mockClear();
});

it("should create a Log instance", () => {
const log = new Log({ messages: ["test"], data: {}, context: {} });
const log = new Log({
messages: ["test"],
data: {},
context: {},
});
expect(log).toBeInstanceOf(Log);
});

it("should have the correct timestamp", () => {
const log = new Log({
messages: ["test"],
data: {},
context: {},
});
expect(log.timestamp).toBe(12346);
});

it("should add a message", () => {
const log = new Log({ messages: ["test"], data: {}, context: {} });
const log = new Log({
messages: ["test"],
data: {},
context: {},
});
log.addMessage("test2");
expect(log.messages).toEqual(["test", "test2"]);
});
Expand Down
11 changes: 7 additions & 4 deletions packages/core/src/internal/logger/service/Log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,30 @@ export type LogContext = Partial<{

export type LogData = Record<string, unknown>;

export type LogMessages = string[];
export type LogMessage = string;

export type LogContructorArgs = {
messages: LogMessages;
messages: LogMessage[];
data: LogData;
context: LogContext;
timestamp?: number;
};

export interface LoggerSubscriber {
log(level: LogLevel, log: Log): void;
}

export class Log {
messages: string[];
messages: LogMessage[];
data: Record<string, unknown> = {}; // use Maybe type for null/undefined ?
context: LogContext = {}; // use Maybe type for null/undefined ?
timestamp: number = Date.now();

constructor({ messages, data, context }: LogContructorArgs) {
constructor({ messages, data, context, timestamp }: LogContructorArgs) {
this.messages = messages;
this.data = data;
this.context = context;
this.timestamp = timestamp ?? this.timestamp;
}

addMessage(message: string) {
Expand Down
29 changes: 29 additions & 0 deletions packages/core/src/internal/logger/service/LogBuilder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,13 @@ class CustomError {
}

let log: Log;
const d = jest.spyOn(Date, "now").mockReturnValue(12345);

describe("LogBuilder", () => {
beforeEach(() => {
d.mockClear();
});

describe("build", () => {
it("should create a Log instance with context and data", () => {
log = LogBuilder.build(
Expand Down Expand Up @@ -40,6 +45,30 @@ describe("LogBuilder", () => {
});
});

describe("buildWithTimestamp", () => {
it("should create a Log instance with a timestamp", () => {
log = LogBuilder.buildWithTimestamp(
{ type: "test" },
{ key: "value" },
12345,
"test",
);

expect(log).toBeInstanceOf(Log);
expect(log.timestamp).toBe(12345);
expect(log.context).toEqual({ type: "test" });
expect(log.data).toEqual({ key: "value" });
expect(log.messages).toEqual(["test"]);
});

it("should create a Log instance with a timestamp and no context or data", () => {
log = LogBuilder.buildWithTimestamp(undefined, undefined, 12345, "test");
expect(log.context).toEqual({});
expect(log.data).toEqual({});
expect(log.messages).toEqual(["test"]);
});
});

describe("buildFromError", () => {
it("should create a Log instance with a normal Error", () => {
log = LogBuilder.buildFromError(new Error("test"), {}, { key: "value" });
Expand Down
26 changes: 19 additions & 7 deletions packages/core/src/internal/logger/service/LogBuilder.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,37 @@
import { SdkError } from "@root/src/api/Error";

import { Log, LogContext, LogData, LogMessages } from "./Log";
import { Log, LogContext, LogData, LogMessage } from "./Log";

export class LogBuilder {
static build(
context: LogContext = {},
data: LogData = {},
...messages: LogMessages
...messages: LogMessage[]
) {
return new Log({ context, data, messages });
}

static buildWithTimestamp(
context: LogContext = {},
data: LogData = {},
timestamp: number,
...messages: LogMessage[]
) {
return new Log({ context, data, messages, timestamp });
}

static buildFromError(
error: SdkError | Error,
context: LogContext = { type: "error" },
data: LogData = {},
timestamp?: number,
) {
const isSdkError = "_tag" in error;
const message = isSdkError
? error.originalError
? error.originalError.message
: error._tag
: error.message;

return new Log({
context: {
Expand All @@ -28,11 +43,8 @@ export class LogBuilder {
...data,
error,
},
messages: isSdkError
? error.originalError
? [error.originalError.message]
: [error._tag]
: [error.message],
messages: [message],
timestamp,
});
}
}

0 comments on commit de83371

Please sign in to comment.