1
0
mirror of https://github.com/bpatrik/pigallery2.git synced 2024-12-25 02:04:15 +02:00
pigallery2/src/backend/Logger.ts
Patrik J. Braun 2fa1c8f87f Implementing custom Logger, removing deprecated winston dependency
Winston had a major API refactor since v2, so update was not straight forward. Pigallery also uses simple logging, so no need for the complex feature set of winston.
2021-05-16 11:11:53 +02:00

66 lines
1.8 KiB
TypeScript

import {Config} from '../common/config/private/Config';
import {LogLevel} from '../common/config/private/PrivateConfig';
export type logFN = (...args: (string | number)[]) => void;
const forcedDebug = process.env.NODE_ENV === 'debug';
if (forcedDebug === true) {
console.log('NODE_ENV environmental variable is set to debug, forcing all logs to print');
}
export class Logger {
public static silly(...args: (string | number)[]): void {
if (!forcedDebug && Config.Server.Log.level < LogLevel.silly) {
return;
}
Logger.log(`[\x1b[35mSILLY\x1b[0m]`, ...args);
}
public static debug(...args: (string | number)[]): void {
if (!forcedDebug && Config.Server.Log.level < LogLevel.debug) {
return;
}
Logger.log(`[\x1b[34mDEBUG\x1b[0m]`, ...args);
}
public static verbose(...args: (string | number)[]): void {
if (!forcedDebug && Config.Server.Log.level < LogLevel.verbose) {
return;
}
Logger.log(`[\x1b[36mVERBS\x1b[0m]`, ...args);
}
public static info(...args: (string | number)[]): void {
if (!forcedDebug && Config.Server.Log.level < LogLevel.info) {
return;
}
Logger.log(`[\x1b[32mINFO_\x1b[0m]`, ...args);
}
public static warn(...args: (string | number)[]): void {
if (!forcedDebug && Config.Server.Log.level < LogLevel.warn) {
return;
}
Logger.log(`[\x1b[33mWARN_\x1b[0m]`, ...args);
}
public static error(...args: (string | number)[]): void {
Logger.log(`[\x1b[31mERROR\x1b[0m]`, ...args);
}
private static log(tag: string, ...args: (string | number)[]): void {
const date = (new Date()).toLocaleString();
let LOG_TAG = '';
if (args.length > 0 && typeof args[0] === 'string' && args[0].startsWith('[') && args[0].endsWith(']')) {
LOG_TAG = args[0];
args.shift();
}
console.log(date + tag + LOG_TAG, ...args);
}
}