1
0
mirror of https://github.com/bpatrik/pigallery2.git synced 2024-12-23 01:27:14 +02:00

Merge pull request #843 from mblythe86/log_funcs

Add logging elision
This commit is contained in:
Patrik J. Braun 2024-03-03 15:49:45 +01:00 committed by GitHub
commit f5f34c55f3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 23 additions and 17 deletions

View File

@ -1,7 +1,7 @@
import {Config} from '../common/config/private/Config';
import {LogLevel} from '../common/config/private/PrivateConfig';
export type logFN = (...args: (string | number)[]) => void;
export type logFN = (...args: (string | number | (() => string))[]) => void;
const forcedDebug = process.env['NODE_ENV'] === 'debug';
@ -11,7 +11,8 @@ if (forcedDebug === true) {
);
}
export type LoggerFunction = (...args: (string | number)[]) => void;
export type LoggerArgs = (string | number | (() => string))
export type LoggerFunction = (...args: LoggerArgs[]) => void;
export interface ILogger {
silly: LoggerFunction;
@ -23,67 +24,67 @@ export interface ILogger {
}
export const createLoggerWrapper = (TAG: string): ILogger => ({
silly: (...args: (string | number)[]) => {
silly: (...args: LoggerArgs[]) => {
Logger.silly(TAG, ...args);
},
debug: (...args: (string | number)[]) => {
debug: (...args: LoggerArgs[]) => {
Logger.debug(TAG, ...args);
},
verbose: (...args: (string | number)[]) => {
verbose: (...args: LoggerArgs[]) => {
Logger.verbose(TAG, ...args);
},
info: (...args: (string | number)[]) => {
info: (...args: LoggerArgs[]) => {
Logger.info(TAG, ...args);
},
warn: (...args: (string | number)[]) => {
warn: (...args: LoggerArgs[]) => {
Logger.warn(TAG, ...args);
},
error: (...args: (string | number)[]) => {
error: (...args: LoggerArgs[]) => {
Logger.error(TAG, ...args);
}
});
export class Logger {
public static silly(...args: (string | number)[]): void {
public static silly(...args: LoggerArgs[]): void {
if (!forcedDebug && Config.Server.Log.level < LogLevel.silly) {
return;
}
Logger.log(`[\x1b[35mSILLY\x1b[0m]`, ...args);
}
public static debug(...args: (string | number)[]): void {
public static debug(...args: LoggerArgs[]): void {
if (!forcedDebug && Config.Server.Log.level < LogLevel.debug) {
return;
}
Logger.log(`[\x1b[34mDEBUG\x1b[0m]`, ...args);
}
public static verbose(...args: (string | number)[]): void {
public static verbose(...args: LoggerArgs[]): void {
if (!forcedDebug && Config.Server.Log.level < LogLevel.verbose) {
return;
}
Logger.log(`[\x1b[36mVERBS\x1b[0m]`, ...args);
}
public static info(...args: (string | number)[]): void {
public static info(...args: LoggerArgs[]): void {
if (!forcedDebug && Config.Server.Log.level < LogLevel.info) {
return;
}
Logger.log(`[\x1b[32mINFO_\x1b[0m]`, ...args);
}
public static warn(...args: (string | number)[]): void {
public static warn(...args: LoggerArgs[]): void {
if (!forcedDebug && Config.Server.Log.level < LogLevel.warn) {
return;
}
Logger.log(`[\x1b[33mWARN_\x1b[0m]`, ...args);
}
public static error(...args: (string | number)[]): void {
public static error(...args: LoggerArgs[]): void {
Logger.log(`[\x1b[31mERROR\x1b[0m]`, ...args);
}
private static log(tag: string, ...args: (string | number)[]): void {
private static log(tag: string, ...args: LoggerArgs[]): void {
const date = new Date().toLocaleString();
let LOG_TAG = '';
if (
@ -95,6 +96,11 @@ export class Logger {
LOG_TAG = args[0];
args.shift();
}
args.forEach((element:LoggerArgs, index:number) => {
if(typeof element === "function"){
args[index] = element(); //execute function, put resulting string in the array
}
});
console.log(date + tag + LOG_TAG, ...args);
}
}

View File

@ -67,14 +67,14 @@ export class Server {
await ConfigDiagnostics.runDiagnostics();
Logger.verbose(
LOG_TAG,
'using config from ' +
() => 'using config from ' +
(
ConfigClassBuilder.attachPrivateInterface(Config)
.__options as ConfigClassOptions<ServerConfig>
).configPath +
':'
);
Logger.verbose(LOG_TAG, JSON.stringify(Config.toJSON({attachDescription: false}), (k, v) => {
Logger.verbose(LOG_TAG, () => JSON.stringify(Config.toJSON({attachDescription: false}), (k, v) => {
const MAX_LENGTH = 80;
if (typeof v === 'string' && v.length > MAX_LENGTH) {
v = v.slice(0, MAX_LENGTH - 3) + '...';