@hodfords/nestjs-custom-logger
v0.0.1
Published
Provide logging for NestJS applications
Downloads
222
Readme
Installation 🤖
To begin using it, we first install the required dependencies.
npm install @hodfords/nestjs-custom-logger
Configuration 🚀
Create logging.config.ts
import { createLoggingConfig, LoggingModule } from '@hodfords/nestjs-custom-logger';
import { env, isLocal } from './env.config';
export const loggingConfig = createLoggingConfig({
serviceName: env.APP_NAME,
isLocal // if True, it will print as NestJS like, otherwise it will prints as json format
});
export const loggingModuleConfig = LoggingModule.forRoot();
Config to NestJS app
this.app = await NestFactory.create<NestExpressApplication>(AppModule, {
rawBody: true,
logger: loggingConfig
});
Config for microservice
Client side
import { setGrpcLoggingMetadata } from '@hodfords/nestjs-custom-logger';
MicorserviceModule.register({
url: env.MICROSERVICES.SERVICE.URL,
timeout: 5000,
requestInitializer: (metadata: Metadata) => {
setGrpcLoggingMetadata(metadata);
}
}),
Server side
You can add GrpcLoggingInterceptor into your app interceptor like this
import { GrpcLoggingInterceptor } from '@hodfords/nestjs-logging';
export function GrpcMicroservice(description?: string): any {
return applyDecorators(
Controller(),
UseFilters(GrpcExceptionFilter),
UsePipes(validateConfig),
UseInterceptors(
ClassSerializerInterceptor,
new GrpcTranslationInterceptor([LANGUAGE_KEY]),
new GrpcLoggingInterceptor() // add interceptor here
),
RegisterGrpcMicroservice(description),
UseResponseInterceptor()
);
}
Usage 🚀
class AppController {
private logger = new Logger(APpController.name);
@Get()
async getHello(): Promise<string> {
this.logger.log('Hello World!');
return 'Hello World!';
}
}
License
This project is licensed under the MIT License