typescript - 如何基于 env 禁用 NestJS Logger?
问题描述
我在NestFactory.create()
方法中使用 logger 选项来控制生产模式下 Logger 的日志级别,但它不能使用ENV='production'
,仍然显示Logger.log()
消息
const bootstrap = async () => {
const app = await NestFactory.create<NestExpressApplication>(AppModule, {
logger:
env.ENV == 'development'
? ['debug', 'error', 'log', 'verbose', 'warn']
: ['error', 'warn'],
});
app.enableCors();
app.use(helmet());
app.set('trust proxy', 1);
解决方案
- 我创建了我的自定义记录器
- 将其注入根模块。
在自定义记录器的构造函数中,我使用setLogLevels函数放置了我的条件。我将级别设置为空,因此它可以忽略任何控制台标准输出。
import { ConsoleLogger, Injectable, LoggerService } from '@nestjs/common';
@Injectable()
export class AppLogger extends ConsoleLogger implements LoggerService {
constructor(
context: string,
options?: {
timestamp?: boolean;
},
) {
super(context, options);
/***
* @condition to check if it is in testing mode
*/
if (condition) {
this.setLogLevels([]);
}
}
}
推荐阅读
- haskell - 任意类型类的约束中的非类型变量参数
- javascript - 静默访问 Vue 实例上不存在的属性失败
- reactjs - React Form - 如果成功输入(MongoDB)数据库则显示消息
- javascript - 如何在 Cypress 中对带有间谍的对象进行断言?
- python - 可以在白色文本上获得黑色边框吗?
- node.js - 收不到邮件
- java - DateTimeParseException:无法在索引 2 处解析文本
- angular - Angular 6 - 当我更改页面时 reCAPTCHA v2 不工作
- json - 如何在 Ionic 上将对象转换为 Json?
- python - 如何在 WinPython 中显示完整数据?