2022-08-23 20:08:53 +02:00
|
|
|
import { LogLevel, TaggedLogger } from "../types";
|
|
|
|
import { ActivatableLogger } from "../logger";
|
2020-06-30 01:10:18 +02:00
|
|
|
|
2022-08-23 20:08:53 +02:00
|
|
|
export type MockLogMessages = unknown[][];
|
2020-06-30 01:10:18 +02:00
|
|
|
type TaggedLogMessages = {
|
2022-08-23 20:08:53 +02:00
|
|
|
tags: { [key: string]: TaggedLogMessages };
|
|
|
|
logs: { [key: string]: MockLogMessages };
|
|
|
|
};
|
2020-06-30 01:10:18 +02:00
|
|
|
|
2022-02-09 18:01:44 +01:00
|
|
|
export class MockLogger implements ActivatableLogger {
|
2022-08-23 20:08:53 +02:00
|
|
|
private taggedLogMessages: TaggedLogMessages =
|
|
|
|
MockLogger.emptyTaggedLogMessages();
|
2020-06-30 01:10:18 +02:00
|
|
|
|
2022-08-23 20:08:53 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-empty-function
|
2020-06-30 01:10:18 +02:00
|
|
|
constructor() {}
|
|
|
|
|
|
|
|
private static emptyTaggedLogMessages(): TaggedLogMessages {
|
|
|
|
return {
|
|
|
|
tags: {},
|
2022-08-23 20:08:53 +02:00
|
|
|
logs: {},
|
2020-06-30 01:10:18 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
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-08-23 20:08:53 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-empty-function,@typescript-eslint/no-unused-vars
|
|
|
|
init(...args: unknown[]): void {}
|
2020-06-30 01:10:18 +02:00
|
|
|
|
2022-08-23 20:08:53 +02:00
|
|
|
private doLog(
|
|
|
|
taggedLogMessages: TaggedLogMessages,
|
|
|
|
level: LogLevel,
|
|
|
|
tags: string[],
|
|
|
|
args: unknown[]
|
|
|
|
): void {
|
2020-06-30 01:10:18 +02:00
|
|
|
if (tags.length > 0) {
|
|
|
|
const tag = tags[0];
|
|
|
|
const remainingTags = tags.slice(1);
|
|
|
|
const subTaggedLogsMessages: TaggedLogMessages =
|
2022-08-23 20:08:53 +02:00
|
|
|
taggedLogMessages.tags[tag] ||
|
|
|
|
MockLogger.emptyTaggedLogMessages();
|
2020-06-30 01:10:18 +02:00
|
|
|
this.doLog(subTaggedLogsMessages, level, remainingTags, args);
|
|
|
|
taggedLogMessages.tags[tag] = subTaggedLogsMessages;
|
|
|
|
} else {
|
2022-08-23 20:08:53 +02:00
|
|
|
const logMessages: MockLogMessages =
|
|
|
|
taggedLogMessages.logs[level] || [];
|
2020-06-30 01:10:18 +02:00
|
|
|
logMessages.push(args);
|
|
|
|
taggedLogMessages.logs[level] = logMessages;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
tag(...tags: string[]): TaggedLogger {
|
2022-08-23 20:08:53 +02:00
|
|
|
const doLog = this.doLog.bind(this);
|
|
|
|
const taggedLogMessages = this.taggedLogMessages;
|
2020-06-30 01:10:18 +02:00
|
|
|
return {
|
2022-08-23 20:08:53 +02:00
|
|
|
log(level: LogLevel, ...args: unknown[]): void {
|
|
|
|
doLog(taggedLogMessages, level, tags, args);
|
2022-02-09 18:01:44 +01:00
|
|
|
},
|
2022-08-23 20:08:53 +02:00
|
|
|
debug(...args: unknown[]): void {
|
|
|
|
this.log("debug", ...args);
|
2022-02-09 18:01:44 +01:00
|
|
|
},
|
2022-08-23 20:08:53 +02:00
|
|
|
info(...args: unknown[]): void {
|
|
|
|
this.log("info", ...args);
|
2022-02-09 18:01:44 +01:00
|
|
|
},
|
2022-08-23 20:08:53 +02:00
|
|
|
warn(...args: unknown[]): void {
|
|
|
|
this.log("warn", ...args);
|
2022-02-09 18:01:44 +01:00
|
|
|
},
|
2022-08-23 20:08:53 +02:00
|
|
|
error(...args: unknown[]): void {
|
|
|
|
this.log("error", ...args);
|
2022-02-09 18:01:44 +01:00
|
|
|
},
|
2022-08-23 20:08:53 +02:00
|
|
|
profile(...args: unknown[]): void {
|
|
|
|
this.log("profile", ...args);
|
2022-02-09 18:01:44 +01:00
|
|
|
},
|
2022-08-23 20:08:53 +02:00
|
|
|
};
|
2020-06-30 01:10:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default new MockLogger();
|