@aurelle/nestjs-http-logger
v0.0.4
Published
Logs all requests and responses the API handles, with optional details provided in error scenarios.
Downloads
2
Readme
nestjs-http-logger
A simple NestJS module that logs incoming requests and responses, including a configurable amount of details. On error, it can log the payload that is sent to the client in the console to make debugging easier.
Building the package
npm run build
Utilisation
Simply register the HttpLoggerModule
that is exported by this package, passing it the desired configuration:
/* ... */
import { HttpLoggerModule } from '@aurelle/nestjs-http-logger';
@Module({
imports: [
HttpLoggerModule.forRoot({
enableRequestDiscriminator: true,
sensitiveQueryParams: ['apiKey', 'userToken'],
shouldLogErrorPayload: true,
logIpAddress: false,
printConfigOnApplicationStartup: true,
}),
],
controllers: [AppController],
providers: [AppService],
})
export class AppModule {}
Note that none of the options listed above is mandatory. Sane defaults are provided out of the box, so that you can import the module without having to deal with configuration at all.
The list of all the available options is described by the HttpLoggerConfig
interface.
Here are the defaults:
const defaultConfig: HttpLoggerConfig = {
sensitiveQueryParams: ['apiKey', 'userToken'],
enableRequestDiscriminator: false,
isSensitiveQueryParamCaseSensitive: true,
logHttpHost: true,
logIpAddress: true,
shouldLogErrorPayload: true,
loggedErrorPayloadMaxSize: '10KB',
printConfigOnApplicationStartup: false,
isEnabled: true,
}
Async module registration
You can use the static method HttpModuleLogger.forRootAsync
to initialize the configuration asynchronously using Nest's DI system.
For example:
/* ... */
import { ConfigModule, ConfigService } from '@nestjs/config';
import { HttpLoggerModule } from '@aurelle/nestjs-http-logger';
@Module({
imports: [
ConfigModule.forRoot({
isGlobal: true,
}),
HttpLoggerModule.forRootAsync({
inject: [ConfigService],
useFactory: (configService: ConfigService) => {
return {
isEnabled: configService.get<string>('ENABLE_HTTP_LOGGER') === 'true',
logIpAddress: true,
printConfigOnApplicationStartup: true,
};
},
}),
ConfigModule,
],
controllers: [AppController],
providers: [AppService],
})
export class AppModule {}