From 6b075a4d61e3309e5ec0cc4f6375810b0046406f Mon Sep 17 00:00:00 2001 From: Renan Date: Mon, 4 Nov 2024 16:12:14 -0300 Subject: [PATCH] apply requested changes --- src/filteredLogger.ts | 10 ++++++---- test/filteredLogger.test.ts | 7 +++---- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/src/filteredLogger.ts b/src/filteredLogger.ts index b9fbca6..553c726 100644 --- a/src/filteredLogger.ts +++ b/src/filteredLogger.ts @@ -6,15 +6,17 @@ import {Log, Logger, LogLevel} from './logger'; export class FilteredLogger implements Logger { private readonly logger: Logger; - private readonly levels: LogLevel[]; + private readonly levelIndex: number; - public constructor(logger: Logger, levels: LogLevel[]) { + public constructor(logger: Logger, level: LogLevel) { this.logger = logger; - this.levels = levels; + this.levelIndex = Object.values(LogLevel).findIndex(value => value === level); } public log(log: T): void { - if (this.levels.includes(log.level)) { + const index = Object.values(LogLevel).findIndex(value => value === log.level); + + if (index >= this.levelIndex) { this.logger.log(log); } } diff --git a/test/filteredLogger.test.ts b/test/filteredLogger.test.ts index 47e4de1..ab7d4d9 100644 --- a/test/filteredLogger.test.ts +++ b/test/filteredLogger.test.ts @@ -1,5 +1,4 @@ -import {InMemoryLogger, Log, LogLevel} from '../src'; -import {FilteredLogger} from '../src/filteredLogger'; +import {FilteredLogger, InMemoryLogger, Log, LogLevel} from '../src'; describe('A filtered logger', () => { const debugLog: Log = { @@ -38,12 +37,12 @@ describe('A filtered logger', () => { it('should filter the specified levels', () => { const logger = new InMemoryLogger(); - const filteredLogger = new FilteredLogger(logger, [LogLevel.ERROR, LogLevel.DEBUG]); + const filteredLogger = new FilteredLogger(logger, LogLevel.WARNING); for (const log of logs) { filteredLogger.log(log); } - expect(logger.getLogs()).toStrictEqual([debugLog, errorLog]); + expect(logger.getLogs()).toStrictEqual([warningLog, errorLog]); }); });