feat: Add new logging utility class

The commit introduces a LogsUtils class under src/utils. This class includes various logging methods such as info, warn, error, softError, trace, fatal, and debug. Also, it uses conditional statements to handle different context and debug scenarios.
This commit is contained in:
Mathis H (Avnyr) 2024-05-13 14:28:12 +02:00
parent 91f431837f
commit 24101f53fa
Signed by: Mathis
GPG Key ID: DD9E0666A747D126

39
src/utils/logs.util.ts Normal file
View File

@ -0,0 +1,39 @@
import * as process from "node:process";
import {ILogObj, Logger} from "tslog";
export class LogsUtils {
private Logger: Logger<ILogObj>;
constructor(contextName: string) {
this.Logger = new Logger({name: contextName})
}
info(value: any, value2?: any) {
this.Logger.info(`\n\n(i) |> ${value}\n${value2 ? '|> ' + value2 + '\n' : ''}`)
}
warn(value: any, value2?: any) {
this.Logger.warn(`\n\n(?) |> ${value}\n${value2 ? '|> ' + value2 + '\n' : ''}`)
}
error(value: any, value2?: any) {
this.Logger.error(`\n\n(!) |> ${value}\n${value2 ? '|> ' + value2 + '\n' : ''}`)
if (process.env["CONTEXT"] === 'prod') process.exit(-1)
}
softError(value: any, value2?: any) {
this.Logger.error(`\n\n(!) |> ${value}\n${value2 ? '|> ' + value2 + '\n' : ''}`)
}
trace(value: any, value2?: any) {
if (process.env["DEBUG"] === 'true') this.Logger.trace(`\n\n(*) |> ${value}\n${value2 ? '|> ' + value2 + '\n' : ''}`)
}
fatal(value: any, value2?: any) {
this.Logger.fatal(`\n\n(X) |> ${value}\n${value2 ? '|> ' + value2 + '\n' : ''}`)
if (process.env["CONTEXT"] === 'prod') process.exit(-1)
}
debug(value: any, value2?: any) {
if (process.env["DEBUG"] === 'true') this.Logger.debug(`\n\n(~) |> ${value}\n${value2 ? '|> ' + value2 + '\n' : ''}`)
}
}