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:
parent
573d4eac9c
commit
1971a0dcfd
40
src/logger/logger.service.ts
Normal file
40
src/logger/logger.service.ts
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user