1

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?

fmagno
  • 1,446
  • 12
  • 27

3 Answers3

4

You can set the log level on the app via app.useLogger(...). So it is possible to change the log level after creating the app when the ConfigService is already available:

async function bootstrap() {
   const app = await NestFactory.create(AppModule);
   const configService = app.get(ConfigService);
   const logLevels = configService.get('LOG_LEVELS')?.split(/\s*,\s*/);
   if (logLevels) {
      app.useLogger(logLevels as LogLevel[]);
   }

   // ...
}
isnot2bad
  • 24,105
  • 2
  • 29
  • 50
0

This might help. It uses an environment variable to set the log level but it also has an example of how to configure redacted fields using ConfigService that you could adapt to log level.

NestJS: How to customise log messages to include request id and name of the file the log message occurred

mh377
  • 1,656
  • 5
  • 22
  • 41
0

I have done it by defining LogLevel array before actually populating it with items.

async function bootstrap() {
 const logLevels: LogLevel[] = [];
 const app = await NestFactory.create(AppModule, {
   logger: logLevels,
 });
 const configService = app.get(ConfigService);
 const nodeEnv = configService.get<string>('NODE_ENV');
 logLevels.push(...getLogLevel(nodeEnv)); // Fill array with right LogLevels
 // ...
}
SloCompTech
  • 116
  • 1
  • 7