### Navigate to Quick Start Sample Source: https://www.npmjs.com/package/nest-winston?activeTab=dependencies Navigate to the quick-start sample directory and install dependencies. ```bash cd sample/quick-start npm install npm run start:dev ``` -------------------------------- ### Install nest-winston and winston Source: https://www.npmjs.com/package/nest-winston?activeTab=dependencies Install the nest-winston and winston packages using npm. ```bash npm install --save nest-winston winston ``` -------------------------------- ### Install nest-winston Source: https://www.npmjs.com/package/nest-winston?activeTab=versions Use npm to install the nest-winston package. This command fetches and installs the latest stable version of the package into your project. ```bash npm i nest-winston ``` -------------------------------- ### Configure WinstonModule with Console Transport Source: https://www.npmjs.com/package/nest-winston?activeTab=dependencies Demonstrates how to configure the WinstonModule with a console transport using the `nestLike` formatter. This setup is useful for application-wide logging with custom formatting options. ```typescript import { Module } from '@nestjs/common'; import { utilities as nestWinstonModuleUtilities, WinstonModule } from 'nest-winston'; import * as winston from 'winston'; @Module({ imports: [ WinstonModule.forRoot({ transports: [ new winston.transports.Console({ format: winston.format.combine( winston.format.timestamp(), winston.format.ms(), nestWinstonModuleUtilities.format.nestLike('MyApp', { colors: true, prettyPrint: true, processId: true, appName: true, }), ), }), // other transports... ], // other options }), ], }) export class AppModule {} ``` -------------------------------- ### Using Logger Methods in a NestJS Controller Source: https://www.npmjs.com/package/nest-winston?activeTab=dependencies Example demonstrating how to inject and use various logger methods (log, debug, verbose, warn, error) within a NestJS controller. Ensure the Logger is injected in the constructor. ```typescript import { Controller, Get, Logger } from '@nestjs/common'; import { AppService } from './app.service'; @Controller() export class AppController { constructor( private readonly appService: AppService, private readonly logger: Logger, ) {} @Get() getHello(): string { this.logger.log('Calling getHello()', AppController.name); this.logger.debug('Calling getHello()', AppController.name); this.logger.verbose('Calling getHello()', AppController.name); this.logger.warn('Calling getHello()', AppController.name); try { throw new Error() } catch (e) { this.logger.error('Calling getHello()', e.stack, AppController.name); } return this.appService.getHello(); } } ``` -------------------------------- ### Bootstrap with Direct Winston Instance Source: https://www.npmjs.com/package/nest-winston?activeTab=dependents Provide a Winston instance directly to WinstonModule.createLogger. This allows you to keep a reference to the instance and interact with it. ```typescript import { createLogger } from 'winston'; import { WinstonModule } from 'nest-winston'; async function bootstrap() { // createLogger of Winston const instance = createLogger({ // options of Winston }); const app = await NestFactory.create(AppModule, { logger: WinstonModule.createLogger({ instance, }), }); await app.listen(3000); } bootstrap(); ``` -------------------------------- ### Async Configuration with useFactory Source: https://www.npmjs.com/package/nest-winston?activeTab=dependencies Use `forRootAsync` with `useFactory` to asynchronously provide module options, especially when configuration services are needed. Dependencies can be injected using the `inject` option. ```typescript import { Module } from '@nestjs/common'; import { WinstonModule } from 'nest-winston'; import * as winston from 'winston'; @Module({ imports: [ WinstonModule.forRootAsync({ useFactory: () => ({ // options }), inject: [], }), ], }) export class AppModule {} ``` -------------------------------- ### Async Configuration with useClass Source: https://www.npmjs.com/package/nest-winston?activeTab=dependencies Use `forRootAsync` with `useClass` to specify a configuration service class that will provide the module options. Nest will instantiate the class and call its option-creating method. ```typescript WinstonModule.forRootAsync({ useClass: WinstonConfigService, }) ``` -------------------------------- ### Bootstrap with Winston Logger Source: https://www.npmjs.com/package/nest-winston?activeTab=dependents Use this method to create the logger outside the application lifecycle and pass it to NestFactory.create. Nest will wrap the winston logger instance. ```typescript import { WinstonModule } from 'nest-winston'; async function bootstrap() { const app = await NestFactory.create(AppModule, { logger: WinstonModule.createLogger({ // options (same as WinstonModule.forRoot() options) }) }); await app.listen(3000); } bootstrap(); ``` -------------------------------- ### Provide Winston Instance Directly to WinstonModule Source: https://www.npmjs.com/package/nest-winston?activeTab=dependencies Create a Winston logger instance using `createLogger` and provide it directly to `WinstonModule.createLogger`. This allows for direct interaction with the Winston instance. ```typescript import { createLogger } from 'winston'; import { WinstonModule } from 'nest-winston'; async function bootstrap() { // createLogger of Winston const instance = createLogger({ // options of Winston }); const app = await NestFactory.create(AppModule, { logger: WinstonModule.createLogger({ instance, }), }); await app.listen(3000); } bootstrap(); ``` -------------------------------- ### Configure WinstonModule in AppModule Source: https://www.npmjs.com/package/nest-winston?activeTab=dependencies Import and configure WinstonModule using forRoot() in your root AppModule. This method accepts winston logger options. ```typescript import { Module } from '@nestjs/common'; import { WinstonModule } from 'nest-winston'; import * as winston from 'winston'; @Module({ imports: [ WinstonModule.forRoot({ // options }), ], }) export class AppModule {} ``` -------------------------------- ### Provide Logger Service in NestJS Module Source: https://www.npmjs.com/package/nest-winston?activeTab=dependencies Configure the AppModule to provide the `Logger` service. This is a necessary step when using Winston as the logger. ```typescript import { Logger, Module } from '@nestjs/common'; @Module({ providers: [Logger], }) export class AppModule {} ``` -------------------------------- ### Replacing Nest Logger Source: https://www.npmjs.com/package/nest-winston?activeTab=versions Replace Nest's default logger with Winston's logger by calling `app.useLogger()` with the `WINSTON_MODULE_NEST_PROVIDER` token. ```typescript import { WINSTON_MODULE_NEST_PROVIDER } from 'nest-winston'; async function bootstrap() { const app = await NestFactory.create(AppModule); app.useLogger(app.get(WINSTON_MODULE_NEST_PROVIDER)); await app.listen(3000); } bootstrap(); ``` -------------------------------- ### Logger Method Signatures Source: https://www.npmjs.com/package/nest-winston?activeTab=dependencies These are the method signatures for the logger instance. Note the different arguments each method accepts. ```typescript debug(message: any, context?: string) log(message: any, context?: string) error(message: any, stack?: string, context?: string) fatal(message: any, stack?: string, context?: string) verbose(message: any, context?: string) warn(message: any, context?: string) ``` -------------------------------- ### Create Winston Logger and Pass to NestFactory Source: https://www.npmjs.com/package/nest-winston?activeTab=dependencies Create a Winston logger instance outside the NestJS application lifecycle and pass it to NestFactory.create. This ensures the logger is available during bootstrapping. ```typescript import { WinstonModule } from 'nest-winston'; async function bootstrap() { const app = await NestFactory.create(AppModule, { logger: WinstonModule.createLogger({ // options (same as WinstonModule.forRoot() options) }) }); await app.listen(3000); } bootstrap(); ``` -------------------------------- ### Inject Winston Logger in Controller Source: https://www.npmjs.com/package/nest-winston?activeTab=dependencies Inject the winston logger instance into your controllers using the WINSTON_MODULE_PROVIDER token. ```typescript import { Controller, Inject } from '@nestjs/common'; import { WINSTON_MODULE_PROVIDER } from 'nest-winston'; import { Logger } from 'winston'; @Controller('cats') export class CatsController { constructor(@Inject(WINSTON_MODULE_PROVIDER) private readonly logger: Logger) { } } ``` -------------------------------- ### Inject Logger with LoggerService and @Inject Source: https://www.npmjs.com/package/nest-winston?activeTab=dependents An alternative syntax for injecting the logger using the LoggerService typing and the @Inject decorator. ```typescript import { Controller, Inject, Logger, LoggerService } from '@nestjs/common'; @Controller('cats') export class CatsController { constructor(@Inject(Logger) private readonly logger: LoggerService) {} } ``` -------------------------------- ### Replacing Nest Logger Source: https://www.npmjs.com/package/nest-winston?activeTab=dependents Replace Nest's default logger with Winston's `WinstonLogger` in your `main.ts` to ensure consistent logging behavior. This forwards all calls to the configured Winston logger instance. ```typescript import { NestFactory } from '@nestjs/core'; import { AppModule } from './app.module'; import { WINSTON_MODULE_NEST_PROVIDER } from 'nest-winston'; async function bootstrap() { const app = await NestFactory.create(AppModule); app.useLogger(app.get(WINSTON_MODULE_NEST_PROVIDER)); await app.listen(3000); } bootstrap(); ``` -------------------------------- ### Inject Logger into Controller (Type Hinting) Source: https://www.npmjs.com/package/nest-winston?activeTab=dependencies Inject the logger into a NestJS controller by type hinting it with the `Logger` class from `@nestjs/common`. ```typescript import { Controller, Logger } from '@nestjs/common'; @Controller('cats') export class CatsController { constructor(private readonly logger: Logger) {} } ``` -------------------------------- ### Injecting Winston Logger in Controller Source: https://www.npmjs.com/package/nest-winston?activeTab=dependencies Inject the Winston logger into a controller using the `WINSTON_MODULE_NEST_PROVIDER` token and `LoggerService` typing. This allows you to use the configured Winston logger for application-specific logging within your controllers. ```typescript import { Controller, Inject, LoggerService } from '@nestjs/common'; import { WINSTON_MODULE_NEST_PROVIDER } from 'nest-winston'; @Controller('cats') export class CatsController { constructor(@Inject(WINSTON_MODULE_NEST_PROVIDER) private readonly logger: LoggerService) { } } ``` -------------------------------- ### Replacing Nest Logger Source: https://www.npmjs.com/package/nest-winston?activeTab=dependencies Replace the default Nest logger with Winston's logger by calling `app.useLogger()` in your `main.ts`. This ensures consistent logging behavior across the application. ```typescript import { NestFactory } from '@nestjs/core'; import { AppModule } from './app.module'; async function bootstrap() { const app = await NestFactory.create(AppModule); app.useLogger(app.get(WINSTON_MODULE_NEST_PROVIDER)); await app.listen(3000); } bootstrap(); ``` -------------------------------- ### Inject Logger into Controller (Inject Decorator) Source: https://www.npmjs.com/package/nest-winston?activeTab=dependencies Inject the logger into a NestJS controller using the `@Inject` decorator and typing it with `LoggerService` from `@nestjs/common`. ```typescript import { Controller, Inject, Logger, LoggerService } from '@nestjs/common'; @Controller('cats') export class CatsController { constructor(@Inject(Logger) private readonly logger: LoggerService) {} } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.