日志管理
原创2026/3/5小于 1 分钟
安装
npm install --save nest-winston winston winston-daily-rotate-file配置
import { NestFactory } from '@nestjs/core'
import { AppModule } from './app.module'
import { WinstonModule } from 'nest-winston'
import * as winston from 'winston'
import * as DailyRotateFile from 'winston-daily-rotate-file'
async function bootstrap() {
const app = await NestFactory.create(AppModule, {
logger: WinstonModule.createLogger({
level: 'info',
format: winston.format.combine(winston.format.timestamp(), winston.format.json()),
transports: [
new DailyRotateFile({
filename: 'src/logs/info-%DATE%.log',
datePattern: 'YYYY-MM-DD',
zippedArchive: true,
maxSize: '20m',
maxFiles: '15d',
level: 'info',
}),
new DailyRotateFile({
filename: 'src/logs/error-%DATE%.log',
datePattern: 'YYYY-MM-DD',
zippedArchive: true,
maxSize: '20m',
maxFiles: '15d',
level: 'error',
}),
],
}),
})
await app.listen(3000)
}
bootstrap()至此,本章节的学习就到此结束了,如有疑惑,可对接技术客服进行相关咨询。