import { Global , Module , DynamicModule , NestModule , MiddlewareConsumer , RequestMethod , Inject , } from '@nestjs/common' ; import { Provider } from '@nestjs/common/interfaces' ; import * as express from 'express' ; import { pinoHttp } from 'pino-http' ; import { Logger } from './Logger' ; import { Params , LoggerModuleAsyncParams , PARAMS_PROVIDER_TOKEN , } from './params' ; import { PinoLogger } from './PinoLogger' ; import { createProvidersForDecorated } from './InjectPinoLogger' ; import { Store , storage } from './storage' ; const DEFAULT_ROUTES = [ { path : '*' , method : RequestMethod . ALL } ] ; @ Global ( ) @ Module ( { providers : [ Logger ] , exports : [ Logger ] } ) export class LoggerModule implements NestModule { static forRoot ( params ? ) { const paramsProvider = { provide : PARAMS_PROVIDER_TOKEN , useValue : params || { } , } ; const decorated = createProvidersForDecorated ( ) ; return { module : LoggerModule , providers : [ Logger , ... decorated , PinoLogger , paramsProvider ] , exports : [ Logger , ... decorated , PinoLogger , paramsProvider ] , } ; } static forRootAsync ( params ) { const paramsProvider = { provide : PARAMS_PROVIDER_TOKEN , useFactory : params . useFactory , inject : params . inject , } ; const decorated = createProvidersForDecorated ( ) ; const providers = [ Logger , ... decorated , PinoLogger , paramsProvider , ... ( params . providers || [ ] ) , ] ; return { module : LoggerModule , imports : params . imports , providers , exports : [ Logger , ... decorated , PinoLogger , paramsProvider ] , } ; } constructor ( @ Inject ( PARAMS_PROVIDER_TOKEN ) private readonly params ) { } configure ( consumer ) { const { exclude , forRoutes = DEFAULT_ROUTES , pinoHttp , useExisting , } = this . params ; const middlewares = createLoggerMiddlewares ( pinoHttp || { } , useExisting ) ; if ( exclude ) { consumer . apply ( ... middlewares ) . exclude ( ... exclude ) . forRoutes ( ... forRoutes ) ; } else { consumer . apply ( ... middlewares ) . forRoutes ( ... forRoutes ) ; } } } function createLoggerMiddlewares ( params , useExisting ? ) { if ( useExisting ) { return [ bindLoggerMiddleware ] ; } if ( Array . isArray ( params ) ) { const middleware = pinoHttp ( ... params ) ; PinoLogger . root = middleware . logger ; return [ middleware , bindLoggerMiddleware ] ; } const middleware = pinoHttp ( params as any ) ; PinoLogger . root = middleware . logger ; return [ middleware , bindLoggerMiddleware ] ; } function bindLoggerMiddleware ( req , _res , next ) { storage . run ( new Store ( req . log ) , next ) ; }