mirror of
https://github.com/hedgedoc/hedgedoc.git
synced 2024-11-25 19:26:31 -05:00
Logging: Add LogLevels to ConsoleLoggerService
Signed-off-by: Philip Molares <philip.molares@udo.edu>
This commit is contained in:
parent
e9664b4aa7
commit
6fe10bab4c
1 changed files with 43 additions and 28 deletions
|
@ -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;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue