Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/gaosong886/nestjs-winston
Logging module for nestjs with winston.
https://github.com/gaosong886/nestjs-winston
logger logging nestjs winston winston-daily-rotate-file
Last synced: 1 day ago
JSON representation
Logging module for nestjs with winston.
- Host: GitHub
- URL: https://github.com/gaosong886/nestjs-winston
- Owner: gaosong886
- License: mit
- Created: 2023-12-30T02:40:03.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2023-12-30T02:44:32.000Z (about 1 year ago)
- Last Synced: 2024-12-15T17:47:26.738Z (about 1 month ago)
- Topics: logger, logging, nestjs, winston, winston-daily-rotate-file
- Language: TypeScript
- Homepage:
- Size: 4.88 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
## nestjs-winston
[![License: MIT](https://img.shields.io/badge/License-MIT-green.svg)](https://opensource.org/licenses/MIT)
[Winston](https://github.com/winstonjs/winston) logging module for [Nestjs](https://github.com/nestjs/nest).
## Installation
```bash
$ npm i --save @gaosong886/nestjs-winston winston
```## Quick start
Import the `WinstonModule`
```ts
import { Module } from '@nestjs/common';
import { format, transports } from 'winston';
import { WinstonModule, WinstonModuleOptions } from '@gaosong886/nestjs-winston';
import 'winston-daily-rotate-file'; // Expand winston.transports with adding 'DailyRotateFile'@Module({
imports: [
WinstonModule.forRootAsync({
useFactory: async (): Promise => {
const { combine, label, timestamp, printf } = format;// Create a transport to a rotating file
// https://github.com/winstonjs/winston-daily-rotate-file
const appTransport = new transports.DailyRotateFile({
filename: 'logs/app-%DATE%.log',
datePattern: 'YYYY-MM-DD',
zippedArchive: true,
maxSize: '20m',
maxFiles: '14d',
});const options: WinstonModuleOptions = {
format: combine(
label({ label: 'MyLabel' }),
timestamp({ format: 'YYYY/MM/DD hh:mm:ss' }),
printf(({ label, pid, timestamp, level, message, context }) => {
return `[${label}] ${pid} - ${timestamp} ${level} [${context}] ${message}`;
}),
),
transports: [appTransport],
};
return options;
},
})
],
...
})
export class AppModule {}
```Inject the `WinstonService` into your class
```ts
import { Injectable } from '@nestjs/common';
import { WinstonService } from 'src/common/winston/winston.service';@Injectable()
export class MyService {
constructor(private readonly winstonService: WinstonService) {
this.winstonService.setContext(MyService.name);
}doSomething() {
this.winstonService.log('Hello');
// [MyLabel] 7972 - 2023/11/25 04:28:57 info [MyService] Hello
}
}```