From 5c6d86877d6a086433179dd323765364b58c2ec4 Mon Sep 17 00:00:00 2001 From: Justin Chase Date: Wed, 13 Nov 2024 23:47:58 -0600 Subject: [PATCH] feat: log levels (#11) * logger refactor * autolabeler * log level * fmt --- src/logging/base.logger.ts | 34 ++++++++++++++++++------------ src/logging/console.logger.ts | 7 ++---- src/logging/logLevel.ts | 1 - src/logging/logger.interface.ts | 4 ---- src/logging/logger.service.test.ts | 11 ---------- 5 files changed, 22 insertions(+), 35 deletions(-) diff --git a/src/logging/base.logger.ts b/src/logging/base.logger.ts index bf3d21b..36e842f 100644 --- a/src/logging/base.logger.ts +++ b/src/logging/base.logger.ts @@ -2,38 +2,38 @@ 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, ): Promise; - public async trace( - message: string, - data: Record = {}, - ) { - await this.log(LogLevel.Trace, message, data); - } - public async debug( message: string, data: Record = {}, ) { - 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 = {}, ) { - 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 = {}, ) { - 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( @@ -41,10 +41,16 @@ export abstract class BaseLogger implements Logger { error: unknown, data: Record = {}, ) { - 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( diff --git a/src/logging/console.logger.ts b/src/logging/console.logger.ts index d900cfa..48a8fc6 100644 --- a/src/logging/console.logger.ts +++ b/src/logging/console.logger.ts @@ -1,5 +1,4 @@ import { - bgBlue, bgRed, blue, brightBlack, @@ -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( @@ -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: diff --git a/src/logging/logLevel.ts b/src/logging/logLevel.ts index 83f1389..bf6919b 100644 --- a/src/logging/logLevel.ts +++ b/src/logging/logLevel.ts @@ -1,5 +1,4 @@ export enum LogLevel { - Trace = "T", Debug = "D", Warn = "W", Info = "I", diff --git a/src/logging/logger.interface.ts b/src/logging/logger.interface.ts index ed6a4e0..9d15fd2 100644 --- a/src/logging/logger.interface.ts +++ b/src/logging/logger.interface.ts @@ -6,10 +6,6 @@ export type Logger = { message: string, data?: Record, ): Promise; - trace( - message: string, - data?: Record, - ): Promise; debug( message: string, data?: Record, diff --git a/src/logging/logger.service.test.ts b/src/logging/logger.service.test.ts index 64331b9..dc60b3d 100644 --- a/src/logging/logger.service.test.ts +++ b/src/logging/logger.service.test.ts @@ -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: () =>