Logger

Nest comes with a default implementation of internal Logger which is used during the instantiation process as well as in several different situations, such as occurred exception, and so forth. However, sometimes you might want to either disable logging entirely or provide a custom implementation and handle messages on your own. In order to turn off a logger, we use the Nest application options object.

  1. const app = await NestFactory.create(ApplicationModule, {
  2. logger: false,
  3. });
  4. await app.listen(3000);

You can also enable only certain types of logging:

  1. const app = await NestFactory.create(ApplicationModule, {
  2. logger: ['error', 'warn'],
  3. });
  4. await app.listen(3000);

In some scenarios, we might want to use a different logger under the hood. In order to do that, we have to pass an object that fulfills LoggerService interface. An example could be a built-in console.

  1. const app = await NestFactory.create(ApplicationModule, {
  2. logger: console,
  3. });
  4. await app.listen(3000);

But it’s not an apt idea. However, we can create our own logger easily.

  1. import { LoggerService } from '@nestjs/common';
  2. export class MyLogger implements LoggerService {
  3. log(message: string) {}
  4. error(message: string, trace: string) {}
  5. warn(message: string) {}
  6. debug(message: string) {}
  7. verbose(message: string) {}
  8. }

Then, we can apply MyLogger instance directly:

  1. const app = await NestFactory.create(ApplicationModule, {
  2. logger: new MyLogger(),
  3. });
  4. await app.listen(3000);

Extend built-in logger

Lot of use cases require creating your own logger. You don’t have to entirely reinvent the wheel though. Simply extend built-in Logger class to partially override the default implementation, and use super to delegate the call to the parent class.

  1. import { Logger } from '@nestjs/common';
  2. export class MyLogger extends Logger {
  3. error(message: string, trace: string) {
  4. // add your tailored logic here
  5. super.error(message, trace);
  6. }
  7. }

Dependency injection

If you want to enable dependency injection in your logger, you have to make the MyLogger class a part of the real application. For instance, you can create a LoggerModule.

  1. import { Module } from '@nestjs/common';
  2. import { MyLogger } from './my-logger.service.ts';
  3. @Module({
  4. providers: [MyLogger],
  5. exports: [MyLogger],
  6. })
  7. export class LoggerModule {}

Once LoggerModule is imported anywhere, the framework will take charge of creating an instance of your logger. Now, to use the same instance of a logger across the whole app, including bootstrapping and error handling stuff, use following construction:

  1. const app = await NestFactory.create(ApplicationModule, {
  2. logger: false,
  3. });
  4. app.useLogger(app.get(MyLogger));
  5. await app.listen(3000);

The only downside of this solution is that your first initialization messages won’t be handled by your logger instance, though, it shouldn’t really matter at this point.