I tried to use the internal Logger
of nestjs (described on https://docs.nestjs.com/techniques/logger -> but with no description of how to use it)
But I had problems (tried to inject LoggerService
and so on)
Can anybody explain how to do this?
Nest comes with a built-in text-based logger which is used during application bootstrapping and several other circumstances such as displaying caught exceptions (i.e., system logging). This functionality is provided via the Logger class in the @nestjs/common package.
nest build is a wrapper on top of the standard tsc compiler (for standard projects) or the webpack compiler (for monorepos). It does not add any other compilation features or steps except for handling tsconfig-paths out of the box.
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'); } }
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()});
Logger
in your tests:module.useLogger(new NoOpLogger());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With