Skip to content

Commit

Permalink
apply requested changes
Browse files Browse the repository at this point in the history
  • Loading branch information
renan628 committed Nov 4, 2024
1 parent b3a6651 commit 6b075a4
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
10 changes: 6 additions & 4 deletions src/filteredLogger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,17 @@ import {Log, Logger, LogLevel} from './logger';
export class FilteredLogger<T extends Log = Log> implements Logger<T> {
private readonly logger: Logger<T>;

private readonly levels: LogLevel[];
private readonly levelIndex: number;

public constructor(logger: Logger<T>, levels: LogLevel[]) {
public constructor(logger: Logger<T>, 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);
}
}
Expand Down
7 changes: 3 additions & 4 deletions test/filteredLogger.test.ts
Original file line number Diff line number Diff line change
@@ -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 = {
Expand Down Expand Up @@ -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]);
});
});

0 comments on commit 6b075a4

Please sign in to comment.