-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
69 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import {Log, Logger} from './logger'; | ||
|
||
/** | ||
* A logger that stores all logs in memory. | ||
*/ | ||
export class InMemoryLogger<T extends Log = Log> implements Logger<T> { | ||
/** | ||
* The received logs. | ||
*/ | ||
private readonly logs: T[] = []; | ||
|
||
public log(log: T): void { | ||
this.logs.push(log); | ||
} | ||
|
||
/** | ||
* Returns all received logs. | ||
*/ | ||
public getLogs(): readonly T[] { | ||
return this.logs; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
export * from './logger'; | ||
export * from './suppressedLogger'; | ||
export * from './consoleLogger'; | ||
export * from './inMemoryLogger'; | ||
export * from './utilities'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import {InMemoryLogger, Log, LogLevel} from '../src'; | ||
|
||
describe('An in-memory logger', () => { | ||
it('should log messages', () => { | ||
const logger = new InMemoryLogger(); | ||
|
||
const logs: Log[] = [ | ||
{ | ||
level: LogLevel.DEBUG, | ||
message: 'A debug message', | ||
details: { | ||
id: 1, | ||
}, | ||
}, | ||
{ | ||
level: LogLevel.INFO, | ||
message: 'An info message', | ||
details: { | ||
id: 2, | ||
}, | ||
}, | ||
{ | ||
level: LogLevel.WARNING, | ||
message: 'A warning message', | ||
details: { | ||
id: 3, | ||
}, | ||
}, | ||
{ | ||
level: LogLevel.ERROR, | ||
message: 'An error message', | ||
details: { | ||
id: 4, | ||
}, | ||
}, | ||
]; | ||
|
||
for (const log of logs) { | ||
logger.log(log); | ||
} | ||
|
||
expect(logger.getLogs()).toEqual(logs); | ||
}); | ||
}); |