Logging: Add LogLevels to ConsoleLoggerService

Signed-off-by: Philip Molares <philip.molares@udo.edu>
This commit is contained in:
Philip Molares 2021-04-13 23:13:47 +02:00
parent a039b85ff4
commit 327206d60c

View file

@ -4,17 +4,24 @@
* SPDX-License-Identifier: AGPL-3.0-only * SPDX-License-Identifier: AGPL-3.0-only
*/ */
import { Injectable, Optional, Scope } from '@nestjs/common'; import { Inject, Injectable, Optional, Scope } from '@nestjs/common';
import { isObject } from '@nestjs/common/utils/shared.utils'; import { isObject } from '@nestjs/common/utils/shared.utils';
import clc = require('cli-color'); import clc = require('cli-color');
import DateTimeFormatOptions = Intl.DateTimeFormatOptions; import DateTimeFormatOptions = Intl.DateTimeFormatOptions;
import appConfiguration, { AppConfig } from '../config/app.config';
import { Loglevel } from '../config/loglevel.enum';
import { needToLog } from '../config/utils';
@Injectable({ scope: Scope.TRANSIENT }) @Injectable({ scope: Scope.TRANSIENT })
export class ConsoleLoggerService { export class ConsoleLoggerService {
private classContext: string; private classContext: string;
private lastTimestamp: number; private lastTimestamp: number;
constructor(@Optional() context?: string) { constructor(
@Inject(appConfiguration.KEY)
private appConfig: AppConfig,
@Optional() context?: string,
) {
this.classContext = context; this.classContext = context;
} }
@ -29,10 +36,11 @@ export class ConsoleLoggerService {
this.makeContextString(functionContext), this.makeContextString(functionContext),
false, false,
); );
this.printStackTrace(trace); ConsoleLoggerService.printStackTrace(trace);
} }
log(message: unknown, functionContext?: string): void { log(message: unknown, functionContext?: string): void {
if (needToLog(this.appConfig.loglevel, Loglevel.INFO)) {
this.printMessage( this.printMessage(
message, message,
clc.green, clc.green,
@ -40,8 +48,10 @@ export class ConsoleLoggerService {
false, false,
); );
} }
}
warn(message: unknown, functionContext?: string): void { warn(message: unknown, functionContext?: string): void {
if (needToLog(this.appConfig.loglevel, Loglevel.WARN)) {
this.printMessage( this.printMessage(
message, message,
clc.yellow, clc.yellow,
@ -49,8 +59,10 @@ export class ConsoleLoggerService {
false, false,
); );
} }
}
debug(message: unknown, functionContext?: string): void { debug(message: unknown, functionContext?: string): void {
if (needToLog(this.appConfig.loglevel, Loglevel.DEBUG)) {
this.printMessage( this.printMessage(
message, message,
clc.magentaBright, clc.magentaBright,
@ -58,8 +70,10 @@ export class ConsoleLoggerService {
false, false,
); );
} }
}
verbose(message: unknown, functionContext?: string): void { verbose(message: unknown, functionContext?: string): void {
if (needToLog(this.appConfig.loglevel, Loglevel.TRACE)) {
this.printMessage( this.printMessage(
message, message,
clc.cyanBright, clc.cyanBright,
@ -67,6 +81,7 @@ export class ConsoleLoggerService {
false, false,
); );
} }
}
private makeContextString(functionContext: string): string { private makeContextString(functionContext: string): string {
let context = this.classContext; let context = this.classContext;
@ -117,7 +132,7 @@ export class ConsoleLoggerService {
return result; return result;
} }
private printStackTrace(trace: string): void { private static printStackTrace(trace: string): void {
if (!trace) { if (!trace) {
return; return;
} }