2022-08-23 20:08:53 +02:00
|
|
|
import {
|
|
|
|
isString,
|
|
|
|
Logger,
|
|
|
|
LoggingConfig,
|
|
|
|
LogLevel,
|
|
|
|
TaggedLogger,
|
|
|
|
} from "./types";
|
|
|
|
import moment from "moment";
|
2020-06-30 01:10:18 +02:00
|
|
|
|
2022-08-23 20:08:53 +02:00
|
|
|
export type LoggingFunction = (...args: unknown[]) => void;
|
2020-06-30 01:10:18 +02:00
|
|
|
|
2022-08-23 20:08:53 +02:00
|
|
|
// noinspection JSUnusedLocalSymbols
|
2021-08-23 21:05:17 +02:00
|
|
|
const noopTaggedLogger: TaggedLogger = {
|
2022-08-23 20:08:53 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars,@typescript-eslint/no-empty-function
|
|
|
|
log(level: LogLevel, ...args: unknown[]): void {},
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars,@typescript-eslint/no-empty-function
|
|
|
|
debug(...args: unknown[]): void {},
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars,@typescript-eslint/no-empty-function
|
|
|
|
info(...args: unknown[]): void {},
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars,@typescript-eslint/no-empty-function
|
|
|
|
warn(...args: unknown[]): void {},
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars,@typescript-eslint/no-empty-function
|
|
|
|
error(...args: unknown[]): void {},
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars,@typescript-eslint/no-empty-function
|
|
|
|
profile(...args: unknown[]): void {},
|
2021-08-23 21:05:17 +02:00
|
|
|
};
|
2020-06-30 01:10:18 +02:00
|
|
|
|
2022-02-09 18:01:44 +01:00
|
|
|
export interface ActivatableLogger extends Logger {
|
2022-07-28 15:07:35 +02:00
|
|
|
init(config: LoggingConfig, loggingFunction?: LoggingFunction): void;
|
2022-02-09 18:01:44 +01:00
|
|
|
}
|
|
|
|
|
2022-07-28 12:19:40 +02:00
|
|
|
/**
|
|
|
|
* TODO: Check if LoggingConfig.debug and LoggingConfig.profile are handled.
|
|
|
|
*/
|
2022-02-09 18:01:44 +01:00
|
|
|
export class ActivatableLoggerImpl implements ActivatableLogger {
|
2022-07-28 15:07:35 +02:00
|
|
|
private config: LoggingConfig = new LoggingConfig(false, false, false);
|
2022-02-09 18:01:44 +01:00
|
|
|
private loggingFunction: LoggingFunction = console.info;
|
2020-06-30 01:10:18 +02:00
|
|
|
|
2022-07-28 15:07:35 +02:00
|
|
|
init(config: LoggingConfig, loggingFunction?: LoggingFunction): void {
|
|
|
|
this.config = config;
|
2022-02-09 18:01:44 +01:00
|
|
|
this.loggingFunction = loggingFunction || console.info;
|
2020-06-30 01:10:18 +02:00
|
|
|
}
|
|
|
|
|
2022-02-09 18:01:44 +01:00
|
|
|
tag(...tags: string[]): TaggedLogger {
|
2022-07-28 15:07:35 +02:00
|
|
|
if (this.config.enabled) {
|
|
|
|
const debug = this.config.debug;
|
|
|
|
const profile = this.config.profile;
|
2022-02-09 18:01:44 +01:00
|
|
|
const loggingFunction = this.loggingFunction;
|
2021-08-23 21:05:17 +02:00
|
|
|
return {
|
2022-08-23 20:08:53 +02:00
|
|
|
log(level: LogLevel, ...args: unknown[]): void {
|
|
|
|
const timeStr = moment().format("YYYY-MM-DD HH:mm:ss");
|
2022-02-09 18:01:44 +01:00
|
|
|
const levelStr = level.toUpperCase();
|
2022-08-23 20:08:53 +02:00
|
|
|
const tagsStr = tags ? "[" + tags.join(", ") + "]" : "";
|
2022-02-09 18:01:44 +01:00
|
|
|
const messagePrefix = `${timeStr} ${levelStr} - ${tagsStr}`;
|
|
|
|
|
|
|
|
// Make sure to only replace %s, etc. in real log message
|
|
|
|
// but not in tags.
|
2022-08-23 20:08:53 +02:00
|
|
|
const escapedMessagePrefix = messagePrefix.replace(
|
|
|
|
/%/g,
|
|
|
|
"%%"
|
|
|
|
);
|
2022-07-28 13:16:13 +02:00
|
|
|
|
2022-08-23 20:08:53 +02:00
|
|
|
let message = "";
|
2022-07-28 13:16:13 +02:00
|
|
|
if (args && isString(args[0])) {
|
2022-02-09 18:01:44 +01:00
|
|
|
message = args[0];
|
|
|
|
args.shift();
|
|
|
|
}
|
|
|
|
|
|
|
|
const logStr = message
|
|
|
|
? `${escapedMessagePrefix} ${message}`
|
|
|
|
: escapedMessagePrefix;
|
|
|
|
loggingFunction(logStr, ...args);
|
|
|
|
},
|
2022-08-23 20:08:53 +02:00
|
|
|
debug(...args: unknown[]): void {
|
2022-07-28 15:07:35 +02:00
|
|
|
if (debug) {
|
2022-08-23 20:08:53 +02:00
|
|
|
this.log("debug", ...args);
|
2022-07-28 15:07:35 +02:00
|
|
|
}
|
2021-08-23 21:05:17 +02:00
|
|
|
},
|
2022-08-23 20:08:53 +02:00
|
|
|
info(...args: unknown[]): void {
|
|
|
|
this.log("info", ...args);
|
2021-08-23 21:05:17 +02:00
|
|
|
},
|
2022-08-23 20:08:53 +02:00
|
|
|
warn(...args: unknown[]): void {
|
|
|
|
this.log("warn", ...args);
|
2021-08-23 21:05:17 +02:00
|
|
|
},
|
2022-08-23 20:08:53 +02:00
|
|
|
error(...args: unknown[]): void {
|
|
|
|
this.log("error", ...args);
|
2021-08-23 21:05:17 +02:00
|
|
|
},
|
2022-08-23 20:08:53 +02:00
|
|
|
profile(...args: unknown[]): void {
|
2022-07-28 15:07:35 +02:00
|
|
|
if (profile) {
|
2022-08-23 20:08:53 +02:00
|
|
|
this.log("profile", ...args);
|
2022-07-28 15:07:35 +02:00
|
|
|
}
|
2021-08-23 21:05:17 +02:00
|
|
|
},
|
2022-08-23 20:08:53 +02:00
|
|
|
};
|
2021-08-23 21:05:17 +02:00
|
|
|
} else {
|
|
|
|
return noopTaggedLogger;
|
2020-06-30 01:10:18 +02:00
|
|
|
}
|
|
|
|
}
|
2021-08-23 21:05:17 +02:00
|
|
|
}
|
|
|
|
|
2022-02-09 18:01:44 +01:00
|
|
|
export default new ActivatableLoggerImpl() as ActivatableLogger;
|