feat(logger): Add new Logger service

A new file logger.service.ts is created which implements the LoggerService from @nestjs/common. This service utilizes the Winston logging library and includes logging functions for different levels of messages such as log, error, warn, debug, and verbose. A logger object with timestamp and simple format is configured to output the logs to both console and a file named "application.log".
This commit is contained in:
Mathis H (Avnyr) 2024-07-09 13:45:05 +02:00
parent 573d4eac9c
commit 1971a0dcfd
Signed by: Mathis
GPG Key ID: DD9E0666A747D126

View File

@ -0,0 +1,40 @@
import { Injectable, type LoggerService, Scope } from "@nestjs/common";
import * as Winston from "winston";
@Injectable({ scope: Scope.TRANSIENT })
export class LogService implements LoggerService {
private readonly winstonLogger: Winston.Logger;
constructor() {
this.winstonLogger = Winston.createLogger({
format: Winston.format.combine(
Winston.format.timestamp(),
Winston.format.simple(),
),
transports: [
new Winston.transports.Console(),
new Winston.transports.File({ filename: "application.log" }),
],
});
}
log(message: string) {
this.winstonLogger.info(message);
}
error(message: string, trace: string) {
this.winstonLogger.error(`Message: ${message} - Trace: ${trace}`);
}
warn(message: string) {
this.winstonLogger.warn(message);
}
debug(message: string) {
this.winstonLogger.debug(message);
}
verbose(message: string) {
this.winstonLogger.verbose(message);
}
}