DEV Community

Micael Levi L. C.
Micael Levi L. C.

Posted on • Updated on

NestJS tip: how to disable initialization logging

for NestJS v8, v9 and v10

For big NestJS apps we might see a bunch of initialization logs like the following:

sample

useless typeorm logs

In some environments those messages are nothing but noise to the log management tool. Note that I'm referring to those emitted with the .log method of the built-in Logger class from @nestjs/common

By following the nestjs docs on Logger: https://docs.nestjs.com/techniques/logger we can find out on how to omit such logs, as follows:

Create a custom logger that inherits from the built-in ConsoleLogger and declare that it will do nothing if the supplied context is a internal one (ie, from @nestjs/core)

// ...
import { ConsoleLogger } from '@nestjs/common'

/**
 * A custom logger that disables all logs emitted by calling `log` method if
 * they use one of the following contexts:
 * - `InstanceLoader`
 * - `RoutesResolver`
 * - `RouterExplorer`
 * - `NestFactory`
 */
export class InternalDisabledLogger extends ConsoleLogger {
  static contextsToIgnore = [
    'InstanceLoader',
    'RoutesResolver',
    'RouterExplorer',
    'NestFactory', // I prefer not including this one
  ]

  log(_: any, context?: string): void {
    if (!InternalDisabledLogger.contextsToIgnore.includes(context)) {
      super.log.apply(this, arguments)
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

Then, at your bootstrap function, use that logger instead

// ...
const app = await NestFactory.create(AppModule, {
  logger: new InternalDisabledLogger(),
})
Enter fullscreen mode Exit fullscreen mode

Top comments (0)