nestjs-winston-logger
v1.1.11
Published
[Nest](https://github.com/nestjs/nest) framework TypeScript starter repository.
Downloads
1,444
Readme
Description
Nest framework TypeScript starter repository.
Installation
$ yarn add nestjs-winston-logger
QuickStart
To use it globally and log any response
import { NestFactory } from "@nestjs/core";
import { AppModule } from "./app.module";
import {
NestjsWinstonLoggerService,
appendRequestIdToLogger,
LoggingInterceptor,
morganRequestLogger,
morganResponseLogger,
appendIdToRequest
} from "nestjs-winston-logger";
import { format, transports } from "winston";
import * as helmet from "helmet";
async function bootstrap() {
const app = await NestFactory.create(AppModule);
app.use(helmet());
const globalLogger = new NestjsWinstonLoggerService({
format: format.combine(
format.timestamp({ format: "isoDateTime" }),
format.json(),
format.colorize({ all: true }),
),
transports: [
new transports.File({ filename: "error.log", level: "error" }),
new transports.File({ filename: "combined.log" }),
new transports.Console(),
],
});
app.useLogger(globalLogger);
// append id to identify request
app.use(appendIdToRequest);
app.use(appendRequestIdToLogger(globalLogger));
app.use(morganRequestLogger(globalLogger));
app.use(morganResponseLogger(globalLogger));
app.useGlobalInterceptors(new LoggingInterceptor(globalLogger));
const port = process.env.PORT || 4000;
await app.listen(port).then(() => {
console.log(`🚀 Server ready at ${port}`);
});
}
bootstrap();
To use it inside module
Inside demo.module.ts
import { Module } from "@nestjs/common";
import { NestjsWinstonLoggerModule } from "nestjs-winston-logger";
import { format, transports } from "winston";
import { DemoService } from "./demo.service";
@Module({
imports: [
NestjsWinstonLoggerModule.forRoot({
format: format.combine(
format.timestamp({ format: "isoDateTime" }),
format.json(),
format.colorize({ all: true }),
),
transports: [
new transports.File({ filename: "error.log", level: "error" }),
new transports.File({ filename: "combined.log" }),
new transports.Console(),
],
}),
],
providers: [DemoService],
})
export class DemoModule {}
Inside demo.service.ts
import { Injectable } from "@nestjs/common";
import { NestjsWinstonLoggerService, InjectLogger } from "nestjs-winston-logger";
@Injectable()
export class DemoService {
constructor(@InjectLogger(DemoService.name) private logger: NestjsWinstonLoggerService) {}
}
License
Nest is MIT licensed.