Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
renan628 committed Nov 4, 2024
1 parent ad1208c commit bcb15da
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 33 deletions.
15 changes: 2 additions & 13 deletions src/multiLogger.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import {Log, Logger} from './logger';
import {extractErrorMessage} from './utilities';

/**
* A logger that stores all logs in memory.
* A logger send logs to multiple loggers.
*/
export class MultiLogger<T extends Log = Log> implements Logger<T> {
private readonly loggers: Array<Logger<T>> = [];
Expand All @@ -12,18 +11,8 @@ export class MultiLogger<T extends Log = Log> implements Logger<T> {
}

public log(log: T): void {
const errors: Error[] = [];

for (const logger of this.loggers) {
try {
logger.log(log);
} catch (error) {
errors.push(error);
}
}

if (errors.length > 0) {
throw new Error(`[${errors.map(error => extractErrorMessage(error))}]`);
logger.log(log);
}
}
}
4 changes: 2 additions & 2 deletions test/filteredLogger.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ describe('A filtered logger', () => {

it('should filter the specified levels', () => {
const logger = new InMemoryLogger();
const suppressedLogger = new FilteredLogger(logger, [LogLevel.ERROR, LogLevel.DEBUG]);
const filteredLogger = new FilteredLogger(logger, [LogLevel.ERROR, LogLevel.DEBUG]);

for (const log of logs) {
suppressedLogger.log(log);
filteredLogger.log(log);
}

expect(logger.getLogs()).toStrictEqual([debugLog, errorLog]);
Expand Down
19 changes: 1 addition & 18 deletions test/multiLogger.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {InMemoryLogger, Log, LogLevel} from '../src';
import {MultiLogger} from '../src/multiLogger';

describe('An in-memory logger', () => {
describe('A multi logger', () => {
const logs: Log[] = [
{
level: LogLevel.DEBUG,
Expand Down Expand Up @@ -45,21 +45,4 @@ describe('An in-memory logger', () => {
expect(firstLogger.getLogs()).toEqual(logs);
expect(secondLogger.getLogs()).toEqual(logs);
});

it('should throw an error with underlying fail messages', () => {
const firstLogger = new InMemoryLogger();
const secondLogger = new InMemoryLogger();
const multiLogger = new MultiLogger([firstLogger, secondLogger]);

const message = 'Some error';

jest.spyOn(firstLogger, 'log').mockImplementation(() => { throw new Error(message); });

for (const log of logs) {
expect(() => multiLogger.log(log)).toThrow(`${[message]}`);
}

expect(firstLogger.getLogs()).toEqual([]);
expect(secondLogger.getLogs()).toEqual(logs);
});
});

0 comments on commit bcb15da

Please sign in to comment.