How can I set the allowed log levels by using the ConfigService?
I tried to do it through main.ts but the logLevels are set during the app
creation call which is also required to get
the ConfigService.
const app = await NestFactory.create(
AppModule.register(),
{ logger: configService.get('LOG_LEVELS') } // <-- at this point I still can't do: const configService = app.get(ConfigService);
);
Is is possible to create the app
object with the logLevels set to default and then override it? Something like this:
const app = await NestFactory.create(
AppModule.register(),
{ logger: true }
);
const configService = app.get('ConfigService');
app.setLogLevels(configService.get('LOG_LEVELS'));
Also tried a different avenue but the solution is not ideal. I tried first to set the logLevels in the constructor of MyLogger but the class member logLevels is private to Logger so I can't override it. The same seems to be the case for the method isLogLevelEnabled(level)
.
The only option I found so far is to check the logLevels allowed inside the actual logging methods: log, warn, error, etc...
@Injectable()
export class MyLogger extends Logger {
constructor(private configService: ConfigService) {
super();
MyLogger.logLevels = configService.get('LOG_LEVELS') // <-- can't set because it's private to Logger
}
isLogLevelEnabled(level) {} // <-- can't override because it's private to Logger
log(message: any, context? string): void {
const logLevels = this.configService.get('LOG_LEVELS');
// check if this method has the right level to log the message depending on logLevels before proceeding
super.log(message, context);
}
}
Is there a better way to do this?