NestJS framework provides an Inbuilt native logger to log the text-based message to the console.
Logs are used to log the useful information and runtime errors.
The 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 an 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 log in NestJS application
Configure the logging levels to enable logging in the application.
The below code enables logging messages with error, warning, 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 the NestFactory.create() method.
The object contains logger: false
to disable logging in the 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();