nestjs-pino-batch-http-transport
v1.3.3
Published
A NestJS module providing a Pino transport to batch logs and send them via HTTP, compatible with NestJS v11.
Downloads
148
Maintainers
Readme
NestJS Pino Batch HTTP Transport
A NestJS module that configures nestjs-pino to use a custom Pino transport for batching logs and sending them via HTTP. It uses axios directly for HTTP requests and pino-abstract-transport for efficient, out-of-process log handling.
Features
- Batches logs before sending them via HTTP POST using
axios. - Log processing occurs in a separate thread via
pino-abstract-transport. - Conditionally adds
pino-prettyto the transport pipeline in non-production environments if no other transport is specified by the user. - Highly configurable batch transport:
url: The target HTTP endpoint for logs.batchSize: Maximum number of logs to send in a single batch (default: 100).batchInterval: Maximum time in milliseconds to wait before sending a batch (default: 5000).headers: Static headers to include in every HTTP request (e.g., for API keys).axiosRequestTimeout: Timeout for the Axios HTTP request in milliseconds (default: 10000).
- Graceful shutdown: Attempts to send any pending logs before the application exits.
- Easy integration with NestJS applications via
NestJsPinoBatchHttpModule.forRoot()for static configuration andNestJsPinoBatchHttpModule.forRootAsync()for dynamic/asynchronous configuration.
Installation
npm install nestjs-pino-batch-http-transport
# or
yarn add nestjs-pino-batch-http-transportEnsure you also have nestjs-pino (and its peer pino-http) installed as per the nestjs-pino documentation. If you want to use pino-pretty in development (which is the default behavior of this module if the batch transport is enabled and no other transport is specified), install it as a dev dependency:
npm install --save-dev pino-pretty
# or
yarn add --dev pino-prettyUsage
Import NestJsPinoBatchHttpModule into your application's root module. This module configures and provides the necessary options to nestjs-pino's LoggerModule internally.
Static Configuration
// app.module.ts
import { Module } from '@nestjs/common';
import { NestJsPinoBatchHttpModule } from 'nestjs-pino-batch-http-transport';
@Module({
imports: [
NestJsPinoBatchHttpModule.forRoot({
// Optional pino-http options (passed to nestjs-pino)
pinoHttp: {
level: process.env.NODE_ENV !== 'production' ? 'debug' : 'info',
// autoLogging: true, // Other pino-http options
},
// Configuration for the batch HTTP transport
batchTransportConfig: {
url: 'YOUR_LOGGING_ENDPOINT_URL', // Required
batchSize: 100, // Optional, default: 100
batchInterval: 5000, // Optional, default: 5000ms
headers: { 'X-API-KEY': 'YOUR_OPTIONAL_API_KEY' }, // Optional
axiosRequestTimeout: 10000, // Optional, default: 10000ms
},
enableTransport: process.env.NODE_ENV === 'production', // Default is false. Set to true for non local environments.
enablePinoPretty: process.env.NODE_ENV !== 'production', // Default is true. Set to false for non local environments.
}),
// ... other modules
],
})
export class AppModule {}Async Configuration (e.g., with NestJS ConfigService)
// app.module.ts
import { Module } from '@nestjs/common';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { NestJsPinoBatchHttpModule, BatchHttpTransportConfig } from 'nestjs-pino-batch-http-transport';
@Module({
imports: [
ConfigModule.forRoot({ isGlobal: true }), // Your ConfigModule setup
NestJsPinoBatchHttpModule.forRootAsync({
imports: [ConfigModule], // Make ConfigModule available
inject: [ConfigService],
useFactory: async (configService: ConfigService) => {
const batchConfig: BatchHttpTransportConfig = {
url: configService.get<string>('LOGGING_ENDPOINT_URL'),
batchSize: configService.get<number>('LOGGING_BATCH_SIZE', 100),
batchInterval: configService.get<number>('LOGGING_BATCH_INTERVAL_MS', 5000),
axiosRequestTimeout: configService.get<number>('LOGGING_AXIOS_TIMEOUT_MS', 10000),
headers: {
'X-App-Version': configService.get<string>('APP_VERSION', '1.0.0'),
'X-API-KEY': configService.get<string>('LOGGING_API_KEY'),
},
};
return {
pinoHttp: {
level: configService.get<string>('LOG_LEVEL', 'info'),
// Other pino-http options...
},
batchTransportConfig: batchConfig,
enableTransport: configService.get<boolean>('ENABLE_BATCH_LOGGING', true),
};
},
}),
// ... other modules
],
})
export class AppModule {}