@nathapp/nestjs-logging
v2.0.4
Published
nestjs-logging
Readme
nestjs-logging Library
LoggingModule
The LoggingModule provides centralized logging capabilities for your NestJS application. It can be imported into any module to enable structured and configurable logging.
Installation
npm install @nathapp/nestjs-loggingUsage
Importing the Module
import { LoggingModule } from '@nathapp/nestjs-logging';
@Module({
imports: [LoggingModule.forRoot({
level: 'info', // log level: 'debug', 'info', 'warn', 'error'
// other configuration options
})],
})
export class AppModule {}then bootstrap on app.useLogger
import { AppModule } from './app.module';
import { Logger } from '@nathapp/nestjs-logging';
async function bootstrap() {
const app = await NestFactory.create(AppModule, {
bufferLogs: true,
//..the rest setting
});
app.useLogger(app.get(Logger));
// (Optinal) flush the log immediately after setting logger
app.flushLogs();
app.listen(process.env.PORT ?? 3000);
}
bootstrap();
or using LoggerFactory
import { AppModule } from './app.module';
import { Logger } from '@nathapp/nestjs-logging';
async function bootstrap() {
const app = await NestFactory.create(AppModule, {
logger: LoggerFactory.createLogger({
type: 'pino',
level: 'debug',
file: {
enabled: false,
},
}),
});
await app.start();
}
bootstrap();
import { LoggerService } from '@nathapp/nestjs-logging';
import { Logger } from '@nestjs/common'
@Injectable()
export class MyService {
const logger = new Logger(MyService.name)
constructor() {}
doSomething() {
this.logger.log('Doing something...');
}
}Features
- Structured logging
- Customizable log levels and and log type
- Integration with NestJS dependency injection
LoggingModuleOptions
The LoggingModuleOptions interface allows you to configure the behavior of the LoggingModule. Below are the available options:
| Option | Type | Description | Default |
| -------------- | -------------- | ---------------------------------------------------------------------------------------------| ----------- |
| level | string | Minimum log level. One of 'debug', 'info', 'warn', 'error'. | 'info' |
| type | string | Logger provider type. One of 'default', 'pino', 'winston'. | 'default' |
| file | object | File logging options (enable file logging, file path, rotation, etc). | undefined |
| console | object | Console logging options (enable pretty print, etc). | undefined |
| redact | string[] | Keys or paths to redact from logs (e.g., sensitive data). | [] |
| autoLogging | object | Enable automatic HTTP request logging or configure its behavior. | { enabled: true } |
| middleware | object | Enable or configure context middleware for incoming requests. | { enableContext: true } |
Example
LoggingModule.forRoot({
level: 'debug',
type: 'pino',
file: {
enabled: true,
rotation: '1d',
},
console: {
enabled: true,
prettyPrint: true,
},
})Note:
- For pino, you must install
pino.- For winston, you must install
winston.- For file logging and rotation, you must install
rotating-file-stream.- For pretty console output with Pino, install
pino-pretty.- For centralized logging with Pino, install
pino-loki.- For centralized logging with Winston, install
winston-loki.
