Skip to content

Commit

Permalink
feat: log levels (#11)
Browse files Browse the repository at this point in the history
* logger refactor

* autolabeler

* log level

* fmt
  • Loading branch information
justinmchase authored Nov 14, 2024
1 parent 459058f commit 5c6d868
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 35 deletions.
34 changes: 20 additions & 14 deletions src/logging/base.logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,49 +2,55 @@ import { LogLevel } from "./logLevel.ts";
import type { Logger } from "./logger.interface.ts";

export abstract class BaseLogger implements Logger {
constructor(protected readonly level: LogLevel = LogLevel.Info) {}
public abstract log(
level: LogLevel,
message: string,
data: Record<keyof unknown, unknown>,
): Promise<void>;

public async trace(
message: string,
data: Record<keyof unknown, unknown> = {},
) {
await this.log(LogLevel.Trace, message, data);
}

public async debug(
message: string,
data: Record<keyof unknown, unknown> = {},
) {
await this.log(LogLevel.Debug, message, data);
if ([LogLevel.Debug].includes(this.level)) {
await this.log(LogLevel.Debug, message, data);
}
}

public async warn(
message: string,
data: Record<keyof unknown, unknown> = {},
) {
await this.log(LogLevel.Warn, message, data);
if ([LogLevel.Debug, LogLevel.Warn].includes(this.level)) {
await this.log(LogLevel.Warn, message, data);
}
}

public async info(
message: string,
data: Record<keyof unknown, unknown> = {},
) {
await this.log(LogLevel.Info, message, data);
if ([LogLevel.Debug, LogLevel.Warn, LogLevel.Info].includes(this.level)) {
await this.log(LogLevel.Info, message, data);
}
}

public async error(
message: string,
error: unknown,
data: Record<keyof unknown, unknown> = {},
) {
await this.log(LogLevel.Error, message, {
...data,
error,
});
if (
[LogLevel.Debug, LogLevel.Warn, LogLevel.Info, LogLevel.Error].includes(
this.level,
)
) {
await this.log(LogLevel.Error, message, {
...data,
error,
});
}
}

public async critical(
Expand Down
7 changes: 2 additions & 5 deletions src/logging/console.logger.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {
bgBlue,
bgRed,
blue,
brightBlack,
Expand All @@ -13,8 +12,8 @@ import { BaseLogger } from "./base.logger.ts";

export class ConsoleLogger extends BaseLogger {
private readonly isTTY: boolean;
constructor() {
super();
constructor(logLevel: LogLevel = LogLevel.Info) {
super(logLevel);
this.isTTY = Deno.stdout.isTerminal();
}
public async log(
Expand All @@ -27,8 +26,6 @@ export class ConsoleLogger extends BaseLogger {
const l = (() => {
if (this.isTTY) {
switch (level) {
case LogLevel.Trace:
return bgBlue(level);
case LogLevel.Debug:
return blue(level);
case LogLevel.Info:
Expand Down
1 change: 0 additions & 1 deletion src/logging/logLevel.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
export enum LogLevel {
Trace = "T",
Debug = "D",
Warn = "W",
Info = "I",
Expand Down
4 changes: 0 additions & 4 deletions src/logging/logger.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@ export type Logger = {
message: string,
data?: Record<keyof unknown, unknown>,
): Promise<void>;
trace(
message: string,
data?: Record<keyof unknown, unknown>,
): Promise<void>;
debug(
message: string,
data?: Record<keyof unknown, unknown>,
Expand Down
11 changes: 0 additions & 11 deletions src/logging/logger.service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,6 @@ Deno.test({
const logger = new ConsoleLogger();
const assertLog = logAsserter(logger);
const assertError = logErrorAsserter(logger);

await t.step({
name: "log_level_00",
fn: () =>
assertLog(
LogLevel.Trace,
"log test",
{},
'T "log test" {}',
),
});
await t.step({
name: "log_level_01",
fn: () =>
Expand Down

0 comments on commit 5c6d868

Please sign in to comment.