2022-02-09 18:01:44 +01:00
|
|
|
import {Logger, TaggedLogger, LogLevel} from '../types';
|
|
|
|
import {ActivatableLogger} from '../logger';
|
2020-06-30 01:10:18 +02:00
|
|
|
|
|
|
|
export type MockLogMessages = any[][];
|
|
|
|
type TaggedLogMessages = {
|
|
|
|
tags: {[key: string]: TaggedLogMessages},
|
|
|
|
logs: {[key: string]: MockLogMessages}
|
|
|
|
}
|
|
|
|
|
2022-02-09 18:01:44 +01:00
|
|
|
export class MockLogger implements ActivatableLogger {
|
2020-06-30 01:10:18 +02:00
|
|
|
private taggedLogMessages: TaggedLogMessages = MockLogger.emptyTaggedLogMessages();
|
|
|
|
|
|
|
|
constructor() {}
|
|
|
|
|
|
|
|
private static emptyTaggedLogMessages(): TaggedLogMessages {
|
|
|
|
return {
|
|
|
|
tags: {},
|
|
|
|
logs: {}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
reset(): void {
|
|
|
|
this.taggedLogMessages = MockLogger.emptyTaggedLogMessages();
|
|
|
|
}
|
|
|
|
|
|
|
|
getMessages(level: LogLevel, ...tags: string[]): MockLogMessages {
|
|
|
|
let taggedLogMessages = this.taggedLogMessages;
|
|
|
|
for (const tag of tags) {
|
|
|
|
if (!taggedLogMessages.tags[tag]) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
taggedLogMessages = taggedLogMessages.tags[tag];
|
|
|
|
}
|
|
|
|
|
|
|
|
return taggedLogMessages.logs[level] || [];
|
|
|
|
}
|
|
|
|
|
2022-02-09 18:01:44 +01:00
|
|
|
init(...args: any[]): void {}
|
2020-06-30 01:10:18 +02:00
|
|
|
|
|
|
|
private doLog(taggedLogMessages: TaggedLogMessages, level: LogLevel, tags: string[], args: any[]): void {
|
|
|
|
if (tags.length > 0) {
|
|
|
|
const tag = tags[0];
|
|
|
|
const remainingTags = tags.slice(1);
|
|
|
|
const subTaggedLogsMessages: TaggedLogMessages =
|
|
|
|
taggedLogMessages.tags[tag] || MockLogger.emptyTaggedLogMessages();
|
|
|
|
this.doLog(subTaggedLogsMessages, level, remainingTags, args);
|
|
|
|
taggedLogMessages.tags[tag] = subTaggedLogsMessages;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
const logMessages: MockLogMessages = taggedLogMessages.logs[level] || [];
|
|
|
|
logMessages.push(args);
|
|
|
|
taggedLogMessages.logs[level] = logMessages;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
tag(...tags: string[]): TaggedLogger {
|
2022-02-09 18:01:44 +01:00
|
|
|
const logger: MockLogger = this;
|
2020-06-30 01:10:18 +02:00
|
|
|
return {
|
2022-02-09 18:01:44 +01:00
|
|
|
log(level: LogLevel, ...args: any[]): void {
|
|
|
|
logger.doLog(logger.taggedLogMessages, level, tags, args);
|
|
|
|
},
|
|
|
|
debug(...args: any[]): void {
|
|
|
|
this.log('debug', ...args);
|
|
|
|
},
|
|
|
|
info(...args: any[]): void {
|
|
|
|
this.log('info', ...args);
|
|
|
|
},
|
|
|
|
warn(...args: any[]): void {
|
|
|
|
this.log('warn', ...args);
|
|
|
|
},
|
|
|
|
error(...args: any[]): void {
|
|
|
|
this.log('error', ...args);
|
|
|
|
},
|
|
|
|
profile(...args: any[]): void {
|
|
|
|
this.log('profile', ...args);
|
|
|
|
},
|
2020-06-30 01:10:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default new MockLogger();
|