How to use nestjs Logging service?

Answer

Better than accessing the Logger statically is to create an instance for your class:

@Controller()
export class AppController {
private readonly logger = new Logger(AppController.name);

@Get()
async get() {
this.logger.log('Getting stuff');
}
}

Why is this better?

You can provide a context in the constructor like new Logger(AppController.name) so that the class name (or anything else) will be part of all log messages in this class.

If you at some point want to extend or replace the default LoggerService, you do not need to change any of your application code besides setting the new logger. Your new logger will automatically be used. If you access it statically it will continue to take the default implementation.

const app = await NestFactory.create(AppModule, {logger: new MyLogger()});
You can mock the Logger in your tests:
module.useLogger(new NoOpLogger());

All react js Questions

Ask your interview questions on react-js

Write Your comment or Questions if you want the answers on react-js from react-js Experts
Name* :
Email Id* :
Mob no* :
Question
Or
Comment* :
 





Disclimer: PCDS.CO.IN not responsible for any content, information, data or any feature of website. If you are using this website then its your own responsibility to understand the content of the website

--------- Tutorials ---