1
0
mirror of https://github.com/bpatrik/pigallery2.git synced 2024-12-25 02:04:15 +02:00
pigallery2/src/backend/Logger.ts

66 lines
1.8 KiB
TypeScript
Raw Normal View History

import {Config} from '../common/config/private/Config';
import {LogLevel} from '../common/config/private/PrivateConfig';
export type logFN = (...args: (string | number)[]) => void;
2020-09-06 12:17:05 +02:00
const forcedDebug = process.env.NODE_ENV === 'debug';
2020-09-06 13:14:28 +02:00
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);
}
}