10.1.1 • Published 9 months ago

@hodfords/nestjs-logger v10.1.1

Weekly downloads
-
License
UNLICENSED
Repository
github
Last release
9 months ago

Installation 🤖

To begin using it, we first install the required dependencies.

npm install @hodfords/nestjs-logger

Configuration 🚀

To activate storage, import the LoggerModule into the root AppModule and run the forRoot() static method as shown below:

  • src/config/logger.config.ts
import { LoggerModule } from '@hodfords/nestjs-logger';
import { env } from './env.config';

export const loggerConfig = LoggerModule.forRoot({ enableLog: ['development', 'qa'].includes(env.APP_ENV) });
  • src/app.module.ts
import { Module } from '@nestjs/common';
import { loggerConfig } from '~config/logger.config';

@Module({
    imports: [loggerConfig]
})
export class AppModule {}

Database Configuration for Log Storage

To store logs, you need to configure the database. Follow this configuration to set up the database for logging:

  • src/config/orm.config.ts
import { initLoggerDatabase } from '@hodfords/nestjs-logger';

const config = {
    cli: {
        migrationsDir: [`${env.ROOT_PATH}/**/databases/migrations/*.${isTest ? 'ts' : 'js'}`]
    },
}

initLoggerDatabase(config);

export = config;

Microservice Communication

When using microservices to communicate between services, this interceptor can help store the logs effectively.

Microservice Logging Interceptor

To use the MicroserviceLoggingInterceptor, follow these steps

  • src/modules/http/*.microservice.ts
import { MicroserviceLoggingInterceptor } from '@hodfords/nestjs-logger/interceptors/microservice-logging.interceptor';
import { Controller, UseInterceptors } from '@nestjs/common';
import { MessagePattern, Payload } from '@nestjs/microservices';

@Controller()
@UseInterceptors(MicroserviceLoggingInterceptor)
export class MicroserviceController {}

CLI Application

To start the application and save logs using the logger service, follow this configuration:

  • src/cli.ts
async function startApp() {
    const app = await NestFactory.create(AppModule);
    const loggerService = app.select(loggerConfig).get(LoggerService, { strict: true });
    const loggerEntity = await loggerService.save({
        method: MethodEnum.TRACE,
        path: '',
        status: 0,
        type: LoggerTypeEnum.COMMAND,
        command: process.argv.map((cmd) => cmd.split('/').slice(-1)).join(' ')
    });

    requestClsNamespace.run(async () => {
        requestClsNamespace.set(REQUEST_ID, loggerEntity.id);
        const commandService: CommandService = app.select(CommandModule).get(CommandService, { strict: true });

        await commandService.exec();
    });

    await app.close();
}

License

This project is licensed under the MIT License

10.1.1

9 months ago

10.1.0

10 months ago

10.0.0

12 months ago