ec-nest/src/logging/logger.config.ts
liuyun bbc06ae775 feat: add pino structured logging (stdout + optional file via env)
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-10-10 21:31:05 +08:00

29 lines
958 B
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { DynamicModule } from '@nestjs/common';
import { LoggerModule } from 'nestjs-pino';
/**
* 日志配置:pino 结构化日志。
* - stdout 默认开启
* - 文件输出默认关闭,由环境变量 LOG_FILE_ENABLED=true 开启
* - 级别 LOG_LEVEL(默认 info)、路径 LOG_FILE_PATH(默认 logs/app.log)
*/
export function createLoggerModule(): DynamicModule {
const level = process.env.LOG_LEVEL ?? 'info';
const fileEnabled = process.env.LOG_FILE_ENABLED === 'true';
const filePath = process.env.LOG_FILE_PATH ?? 'logs/app.log';
const pinoHttp = fileEnabled
? {
level,
transport: {
targets: [
{ target: 'pino/file', options: { destination: 1 } }, // stdout
{ target: 'pino-roll', options: { file: filePath, size: '10m', limit: { count: 10 } } }, // 文件按大小轮转
],
},
}
: { level };
return LoggerModule.forRoot({ pinoHttp });
}