Skip to content

Commit

Permalink
BREAKING(log): rename LogRecord timestamp to datetime
Browse files Browse the repository at this point in the history
  • Loading branch information
gabeidx authored and Gabe committed Jul 30, 2022
1 parent b94bbc5 commit b6d8dd7
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 19 deletions.
16 changes: 8 additions & 8 deletions log/handlers/console.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ type ConsoleHandlerOptions = {
/** Show colors on the console output. Defaults to `true` */
color?: boolean;

/** Add timestamp to the log message. Defaults to `false` */
timestamp?: boolean;
/** Add datetime to the log message. Defaults to `false` */
datetime?: boolean;

/** Format log message as JSON. Defaults to `false` */
json?: boolean;
Expand All @@ -28,18 +28,18 @@ type ConsoleHandlerOptions = {

export class ConsoleHandler extends LogHandler {
#color: boolean;
#datetime: boolean;
#json: boolean;
#name: boolean;
#target: Deno.WriterSync;
#timestamp: boolean;

constructor(options?: ConsoleHandlerOptions) {
super();
this.#color = options?.color ?? true;
this.#json = options?.json ?? false;
this.#name = options?.name ?? false;
this.#target = options?.target ?? Deno.stdout;
this.#timestamp = options?.timestamp ?? false;
this.#datetime = options?.datetime ?? false;
}

override handle({ loggerName, record }: LogHandlerOptions): string {
Expand All @@ -63,7 +63,7 @@ export class ConsoleHandler extends LogHandler {
if (this.#json) {
return JSON.stringify({
...(this.#name && { name: loggerName }),
...(this.#timestamp && { timestamp: record.time }),
...(this.#datetime && { datetime: record.datetime }),
level: record.level,
message: record.message,
});
Expand All @@ -79,11 +79,11 @@ export class ConsoleHandler extends LogHandler {
}
}

if (this.#timestamp) {
if (this.#datetime) {
if (this.#color) {
output.push(colors.gray(record.time));
output.push(colors.gray(record.datetime));
} else {
output.push(record.time);
output.push(record.datetime);
}
}

Expand Down
6 changes: 3 additions & 3 deletions log/handlers/console_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,20 @@ Deno.test("[log] console: prints the logger name", () => {
);
});

Deno.test("[log] console: prints the timestamp for a log record", () => {
Deno.test("[log] console: prints the datetime for a log record", () => {
const record = new LogRecord({ level: LogLevel.DEBUG, message: "hello" });
const writer = new TestWriter();
const handler = new ConsoleHandler({
color: false,
target: writer,
timestamp: true,
datetime: true,
});

handler.handle({ record });

assertEquals(
new TextDecoder().decode(writer.buffer),
`${record.time} debug hello\n`,
`${record.datetime} debug hello\n`,
);
});

Expand Down
14 changes: 9 additions & 5 deletions log/record.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,20 @@ export type LogRecordOptions = {
export class LogRecord {
readonly level: LogLevel;
readonly message: string;
#timestamp: number;
#datetime: Date;

constructor(options: LogRecordOptions) {
this.level = options.level;
this.message = options.message;
this.#timestamp = Date.now();
this.#datetime = new Date();
}

/** The time the message was created in ISO format */
get time(): string {
return new Date(this.#timestamp).toISOString();
/** The date and time the log record was created, in ISO format */
get datetime(): string {
return new Date(this.#datetime).toISOString();
}

get args(): LogRecordOptions["args"] {
return this.#args;
}
}
5 changes: 2 additions & 3 deletions log/record_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,10 @@ Deno.test("[log] record: has a message", () => {
assertEquals(record.message, "Hello World!");
});

Deno.test("[log] record: has a timestamp", () => {
Deno.test("[log] record: has a datetime in ISO", () => {
const record = new LogRecord({
level: LogLevel.EMERGENCY,
message: "Hello World!",
});

assertEquals(record.time, new Date(record.time).toISOString());
assertEquals(record.datetime, new Date(record.datetime).toISOString());
});

0 comments on commit b6d8dd7

Please sign in to comment.