### Install NestJS Pino Module Source: https://github.com/mekh/nestjs-pino/blob/main/README.md Installs the @toxicoder/nestjs-pino package using npm. This is the first step to integrate Pino logging into your NestJS application. ```bash npm install @toxicoder/nestjs-pino ``` -------------------------------- ### Basic NestJS Pino Module Setup Source: https://github.com/mekh/nestjs-pino/blob/main/README.md Demonstrates the simplest way to integrate the PinoModule into a NestJS application by importing it directly into the AppModule. ```typescript import { Module } from '@nestjs/common'; import { PinoModule } from '@toxicoder/nestjs-pino'; @Module({ imports: [PinoModule], }) export class AppModule {} ``` -------------------------------- ### Plain Text Log Output Example Source: https://github.com/mekh/nestjs-pino/blob/main/README.md An example of log output when the logger is configured for plain text and pretty printing. This format is human-readable and useful during development. ```json [12:34:56.789] INFO: Application started [12:35:01.234] ERROR: Failed to connect to database error: Connection refused ``` -------------------------------- ### JSON Log Output Example Source: https://github.com/mekh/nestjs-pino/blob/main/README.md An example of log output when the logger is configured for JSON format. This format is machine-readable and suitable for log aggregation systems. ```json {"level":"info","time":"2023-11-15T12:34:56.789Z","message":"Application started"} {"level":"error","time":"2023-11-15T12:35:01.234Z","message":"Failed to connect to database","error":"Connection refused"} ``` -------------------------------- ### TypeORM Log Output Examples Source: https://github.com/mekh/nestjs-pino/blob/main/README.md Provides examples of TypeORM log outputs in both JSON and pretty-print formats when using `PinoService`. It showcases 'debug' level for queries and 'error' level for database connection issues. ```json {"level":"debug","time":"2023-11-15T12:34:56.789Z","context":"typeorm","message":"QUERY: SELECT * FROM users WHERE id = $1 [1]"} {"level":"error","time":"2023-11-15T12:35:01.234Z","context":"typeorm","message":"ERROR: connection refused\nquery: SELECT * FROM users\nparameters: []"} ``` ```bash [12:34:56.789] DEBUG (typeorm): QUERY: SELECT * FROM users WHERE id = $1 [1] [12:35:01.234] ERROR (typeorm): ERROR: connection refused query: SELECT * FROM users parameters: [] ``` -------------------------------- ### Configuring NestJS Pino Module with forRoot Source: https://github.com/mekh/nestjs-pino/blob/main/README.md Shows how to customize the Pino logger's behavior using the `forRoot` method with various configuration options like log level, call site tracking, and formatting. ```typescript import { Module } from '@nestjs/common'; import { PinoModule } from '@toxicoder/nestjs-pino'; @Module({ imports: [ PinoModule.forRoot({ level: 'debug', callsites: true, json: false, pretty: true, color: true, }), ], }) export class AppModule {} ``` -------------------------------- ### TypeORM Integration with PinoService Source: https://github.com/mekh/nestjs-pino/blob/main/README.md Details the configuration for using `PinoService` as a TypeORM logger. It shows how to enable database logging via `dbLogging` option or `LOG_DB` environment variable and maps TypeORM log events to Pino log levels. ```typescript import { Module } from '@nestjs/common'; import { TypeOrmModule } from '@nestjs/typeorm'; import { PinoModule, PinoService } from '@toxicoder/nestjs-pino'; @Module({ imports: [ PinoModule.forRoot({ dbLogging: 'debug', // or true to use the same level as the main logger }), TypeOrmModule.forRootAsync({ imports: [PinoModule], inject: [PinoService], useFactory: (logger: PinoService) => ({ type: 'postgres', host: 'localhost', port: 5432, username: 'postgres', password: 'postgres', database: 'test', entities: [], logger, // Use PinoService as TypeORM logger }), }), ], }) export class AppModule {} ``` -------------------------------- ### NestJS Application Logger Integration Source: https://github.com/mekh/nestjs-pino/blob/main/README.md Illustrates how to set the `PinoService` as the global logger for a NestJS application in `main.ts`. This ensures all framework logs are handled by Pino, with `bufferLogs: true` to manage logs before the logger is ready. ```typescript import { NestFactory } from '@nestjs/core'; import { PinoService } from '@toxicoder/nestjs-pino'; import { AppModule } from './app.module'; async function bootstrap() { const app = await NestFactory.create(AppModule, { bufferLogs: true, // Buffer logs until logger is available }); // Get the PinoService from the application context const logger = app.get(PinoService); // Set the logger as the application logger app.useLogger(logger); await app.listen(3000); logger.log(`Application is running on: ${await app.getUrl()}`); } bootstrap(); ``` -------------------------------- ### Log Output with Call Sites Source: https://github.com/mekh/nestjs-pino/blob/main/README.md Demonstrates the JSON and pretty-print formats for logs when call site information is enabled (`LOG_CALLSITES=true`). Includes log level, timestamp, message, and the caller's file and line number. ```json { "level": "info", "time": "2023-11-15T12:34:56.789Z", "message": "Application started", "caller": "src/main.ts:15" } ``` ```bash [12:34:56.789] INFO (src/main.ts:15): Application started ``` -------------------------------- ### Async Configuration for NestJS Pino Module Source: https://github.com/mekh/nestjs-pino/blob/main/README.md Illustrates dynamic configuration of the PinoModule using `forRootAsync`. This method allows injecting services like `ConfigModule` and `ClsService` to provide configuration values and custom mixins, such as adding a `traceId` to logs. ```typescript import { Module } from '@nestjs/common'; import { PinoModule, PinoOptions } from '@toxicoder/nestjs-pino'; import { ClsModule, ClsService } from 'nestjs-cls'; import { ConfigModule, LoggerConfig } from '~config'; @Module({ imports: [ ClsModule.forRoot({ global: true, middleware: { mount: true, generateId: true, idGenerator: uuid, }, }), PinoModule.forRootAsync({ imports: [ConfigModule], inject: [LoggerConfig, ClsService], useFactory: (config: LoggerConfig, cls: ClsService) => ({ ...config.getConfig(), relativeTo: path.resolve(process.cwd()), stackAdjustment: 2, mixin: () => ({ traceId: cls.getId(), }), }), }), ], }) export class AppModule {} ``` -------------------------------- ### Direct Pino Access with Custom Levels Source: https://github.com/mekh/nestjs-pino/blob/main/README.md Shows how to access the underlying Pino instance from `PinoService` to create child loggers with custom log levels like 'critical', 'security', and 'business'. This allows for more granular logging within specific services. ```typescript import { Injectable } from '@nestjs/common'; import { PinoService } from '@toxicoder/nestjs-pino'; @Injectable() export class MonitoringService { private logger; constructor(private readonly pinoService: PinoService) { this.logger = this.pinoService.pino.child( { context: 'MonitoringService', }, { customLevels: { critical: 60, security: 55, business: 35, }, }, ); } monitorSystem() { // Use standard levels this.logger.info('System check started'); // Use custom levels this.logger.business('Business event occurred'); this.logger.security('Security event detected'); this.logger.critical('Critical system failure'); } } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.