NestJs Logger shows differents formats logs

NestJs Logger shows different formats logs

If you are using NestJs for your TypeScript projects, you might have encountered a situation where the logs generated by the NestJs Logger are displayed in different formats. This can be confusing and make it difficult to read and analyze the logs. In this blog post, we will explore two solutions to ensure that the NestJs Logger consistently shows logs in the same format.

Solution 1: Customizing the Logger

NestJs provides a Logger class that can be extended to create a custom logger. By extending the Logger class, we can override the log method to ensure that all logs are displayed in the desired format.

Here’s an example of how you can create a custom logger:

import { Logger } from '@nestjs/common';

export class CustomLogger extends Logger {
  log(message: string) {
    // Customize the log format here
    super.log(`[Custom] ${message}`);
  }
}

To use the custom logger in your application, you need to replace the default logger with the custom logger. This can be done by providing the custom logger as a provider in your module:

import { Module } from '@nestjs/common';
import { CustomLogger } from './custom-logger';

@Module({
  providers: [
    {
      provide: 'Logger',
      useClass: CustomLogger,
    },
  ],
})
export class AppModule {}

With this approach, all logs generated by the NestJs Logger will be displayed in the format specified in the custom logger’s log method.

Solution 2: Configuring the Logger

Another way to ensure consistent log formats is by configuring the NestJs Logger. NestJs provides a LoggerService that can be used to configure the logger settings.

Here’s an example of how you can configure the logger:

import { LoggerService } from '@nestjs/common';

export class LoggerConfigService implements LoggerService {
  log(message: string) {
    // Customize the log format here
    console.log(`[Config] ${message}`);
  }

  error(message: string, trace: string) {
    // Customize the error log format here
    console.error(`[Config] ${message}`, trace);
  }

  warn(message: string) {
    // Customize the warning log format here
    console.warn(`[Config] ${message}`);
  }

  debug(message: string) {
    // Customize the debug log format here
    console.debug(`[Config] ${message}`);
  }

  verbose(message: string) {
    // Customize the verbose log format here
    console.log(`[Config] ${message}`);
  }
}

To use the configured logger in your application, you need to replace the default logger with the configured logger. This can be done by providing the configured logger as a provider in your module:

import { Module } from '@nestjs/common';
import { LoggerConfigService } from './logger-config-service';

@Module({
  providers: [
    {
      provide: 'Logger',
      useClass: LoggerConfigService,
    },
  ],
})
export class AppModule {}

By configuring the logger, you can control the format of each log level separately.

Conclusion

In this blog post, we explored two solutions to ensure that the NestJs Logger consistently shows logs in the same format. By customizing the logger or configuring it, you can have full control over the log format and make it easier to read and analyze the logs in your TypeScript projects.

Remember to choose the solution that best fits your requirements and coding style. Happy logging!


Posted

in

by

Tags:

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *