src/core/services/common/logger.service.ts
service for logging
Properties |
Methods |
constructor(context: string)
|
||||||
constructor for the logger
Parameters :
|
Static createlogger | ||||||||
createlogger(context: string)
|
||||||||
creates the logger
Parameters :
Returns :
LoggerService
logger |
Public debug | ||||||||||||
debug(message: string, ...args: any[])
|
||||||||||||
logs the debug message
Parameters :
Returns :
void
|
Public error | ||||||||||||||||
error(message: string, error?: string | Error, ...args: any[])
|
||||||||||||||||
logs the error message
Parameters :
Returns :
void
|
Private format | ||||||||||||
format(message: string, args?: string[])
|
||||||||||||
formats the message
Parameters :
Returns :
any
formatted message |
Public log | ||||||||||||
log(message: string, ...args: any[])
|
||||||||||||
logs the message
Parameters :
Returns :
void
|
Public verbose | ||||||||||||
verbose(message: string, ...args: any[])
|
||||||||||||
logs the verbose message
Parameters :
Returns :
void
|
Public warn | ||||||||||||
warn(message: string, ...args: any[])
|
||||||||||||
logs the warning message
Parameters :
Returns :
void
|
Private Readonly Optional context |
Type : string
|
context |
Private Readonly logger |
Type : Logger
|
logger |
import { Injectable, Logger, LoggerService as NestLoggerService } from '@nestjs/common';
import { formatWithOptions } from 'util';
/**
* service for logging
*/
@Injectable()
export class LoggerService implements NestLoggerService {
/**
* logger
*/
private readonly logger: Logger;
/**
* context
*/
private readonly context?: string;
//=============================================================================================================
/**
* constructor for the logger
* @param context
*/
constructor(context: string) {
this.logger = new Logger(context);
this.context = context;
}
//=============================================================================================================
/**
* creates the logger
* @param context context
* @returns logger
*/
static createlogger(context: string): LoggerService {
return new LoggerService(context);
}
//=============================================================================================================
/**
* logs the message
* @param message message
* @param args arguments
*/
public log(message: string, ...args: any[]) {
this.logger.log(this.format(message, args));
}
//=============================================================================================================
/**
* logs the error message
* @param message message
* @param error error
* @param args arguments
*/
public error(message: string, error?: string | Error, ...args: any[]) {
this.logger.error(this.format(message, args), error instanceof Error ? error.stack : error);
}
//=============================================================================================================
/**
* logs the warning message
* @param message message
* @param args arguments
*/
public warn(message: string, ...args: any[]) {
this.logger.warn(this.format(message, args));
}
//=============================================================================================================
/**
* logs the debug message
* @param message message
* @param args arguments
*/
public debug(message: string, ...args: any[]) {
this.logger.debug(this.format(message, args));
}
//=============================================================================================================
/**
* logs the verbose message
* @param message message
* @param args arguments
*/
public verbose(message: string, ...args: any[]) {
this.logger.verbose(this.format(message, args));
}
//=============================================================================================================
/**
* formats the message
* @param message message
* @param args arguments
* @returns formatted message
*/
private format(message: string, args?: string[]) {
if (!args || !args.length) return message;
return formatWithOptions({ colors: true, depth: 5 }, message, ...args);
}
//=============================================================================================================
}