2022-07-28 13:16:13 +02:00
|
|
|
import {isString, Logger, LogLevel, TaggedLogger} from './types';
|
2022-02-09 18:01:44 +01:00
|
|
|
import moment from 'moment';
|
2020-06-30 01:10:18 +02:00
|
|
|
|
2022-02-09 18:01:44 +01:00
|
|
|
export type LoggingFunction = (...args: any[]) => void;
|
2020-06-30 01:10:18 +02:00
|
|
|
|
2021-08-23 21:05:17 +02:00
|
|
|
const noopTaggedLogger: TaggedLogger = {
|
2022-07-28 12:19:40 +02:00
|
|
|
log(_level: LogLevel, ..._args: any[]): void {},
|
|
|
|
debug(..._args: any[]): void {},
|
|
|
|
info(..._args: any[]): void {},
|
|
|
|
warn(..._args: any[]): void {},
|
|
|
|
error(..._args: any[]): void {},
|
|
|
|
profile(..._args: any[]): 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 {
|
|
|
|
init(enabled: boolean, loggingFunction?: LoggingFunction): void;
|
|
|
|
}
|
|
|
|
|
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 {
|
2021-08-23 21:05:17 +02:00
|
|
|
private enabled: boolean = false;
|
2022-02-09 18:01:44 +01:00
|
|
|
private loggingFunction: LoggingFunction = console.info;
|
2020-06-30 01:10:18 +02:00
|
|
|
|
2022-02-09 18:01:44 +01:00
|
|
|
init(enabled: boolean, loggingFunction?: LoggingFunction): void {
|
2021-08-23 21:05:17 +02:00
|
|
|
this.enabled = enabled;
|
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 {
|
2021-08-23 21:05:17 +02:00
|
|
|
if (this.enabled) {
|
2022-02-09 18:01:44 +01:00
|
|
|
const loggingFunction = this.loggingFunction;
|
2021-08-23 21:05:17 +02:00
|
|
|
return {
|
2022-02-09 18:01:44 +01:00
|
|
|
log(level: LogLevel, ...args: any[]): void {
|
|
|
|
const timeStr = moment().format('YYYY-MM-DD HH:mm:ss');
|
|
|
|
const levelStr = level.toUpperCase();
|
2022-07-28 13:16:13 +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.
|
|
|
|
const escapedMessagePrefix = messagePrefix.replace(/%/g, '%%');
|
2022-07-28 13:16:13 +02:00
|
|
|
|
2022-02-09 18:01:44 +01: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);
|
|
|
|
},
|
|
|
|
debug(...args: any[]): void {
|
|
|
|
this.log('debug', ...args);
|
2021-08-23 21:05:17 +02:00
|
|
|
},
|
2022-02-09 18:01:44 +01:00
|
|
|
info(...args: any[]): void {
|
|
|
|
this.log('info', ...args);
|
2021-08-23 21:05:17 +02:00
|
|
|
},
|
2022-02-09 18:01:44 +01:00
|
|
|
warn(...args: any[]): void {
|
|
|
|
this.log('warn', ...args);
|
2021-08-23 21:05:17 +02:00
|
|
|
},
|
2022-02-09 18:01:44 +01:00
|
|
|
error(...args: any[]): void {
|
|
|
|
this.log('error', ...args);
|
2021-08-23 21:05:17 +02:00
|
|
|
},
|
2022-02-09 18:01:44 +01:00
|
|
|
profile(...args: any[]): void {
|
|
|
|
this.log('profile', ...args);
|
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;
|