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".
41 lines
934 B
TypeScript
41 lines
934 B
TypeScript
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);
|
|
}
|
|
}
|