NestJS framework provides Inbuilt native logger to log the text based message to the console.
Logs are used to log the useful information and runtime error.
Logger can be logged with different logging levels
- log: log the message log level
- error: log error messages
- warn: warning messages
- debug: Debug messages
- verbose : Writes verbose level log message
How to configure Logging in NestJS application
NestFactory.create() passed with optional object that contains different parameters logger and bufferLogs.
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
async function bootstrap() {
const app = await NestFactory.create(AppModule, {
logger: console,
bufferLogs: true,
});
await app.listen(3000);
}
bootstrap();
How to use logging in NestJS application
Configure the logging levels to enable logging in application.
The below code enables logging message with error,warn, and debug.
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
async function bootstrap() {
const app = await NestFactory.create(AppModule, {
logger: ['error', 'warn',"debug"],
});
await app.listen(3000);
}
bootstrap();
How to disable the logging in NestJS APplication
To disable the logging in NestJS application
optional object is passed to NestFactory.create() method.
The object contains logger: false
to disable logging in entire application
main.ts:
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
async function bootstrap() {
const app = await NestFactory.create(AppModule, {
logger: false,
});
await app.listen(3000);
}
bootstrap();