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 committed by David Mehren
parent e9664b4aa7
commit 6fe10bab4c
No known key found for this signature in database
GPG key ID: 185982BA4C42B7C3

View file

@ -4,17 +4,24 @@
* 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 clc = require('cli-color');
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 })
export class ConsoleLoggerService {
private classContext: string;
private lastTimestamp: number;
constructor(@Optional() context?: string) {
constructor(
@Inject(appConfiguration.KEY)
private appConfig: AppConfig,
@Optional() context?: string,
) {
this.classContext = context;
}
@ -29,10 +36,11 @@ export class ConsoleLoggerService {
this.makeContextString(functionContext),
false,
);
this.printStackTrace(trace);
ConsoleLoggerService.printStackTrace(trace);
}
log(message: unknown, functionContext?: string): void {
if (needToLog(this.appConfig.loglevel, Loglevel.INFO)) {
this.printMessage(
message,
clc.green,
@ -40,8 +48,10 @@ export class ConsoleLoggerService {
false,
);
}
}
warn(message: unknown, functionContext?: string): void {
if (needToLog(this.appConfig.loglevel, Loglevel.WARN)) {
this.printMessage(
message,
clc.yellow,
@ -49,8 +59,10 @@ export class ConsoleLoggerService {
false,
);
}
}
debug(message: unknown, functionContext?: string): void {
if (needToLog(this.appConfig.loglevel, Loglevel.DEBUG)) {
this.printMessage(
message,
clc.magentaBright,
@ -58,8 +70,10 @@ export class ConsoleLoggerService {
false,
);
}
}
verbose(message: unknown, functionContext?: string): void {
if (needToLog(this.appConfig.loglevel, Loglevel.TRACE)) {
this.printMessage(
message,
clc.cyanBright,
@ -67,6 +81,7 @@ export class ConsoleLoggerService {
false,
);
}
}
private makeContextString(functionContext: string): string {
let context = this.classContext;
@ -117,7 +132,7 @@ export class ConsoleLoggerService {
return result;
}
private printStackTrace(trace: string): void {
private static printStackTrace(trace: string): void {
if (!trace) {
return;
}