2.1.4 • Published 2 years ago

nestjs-mongolog v2.1.4

Weekly downloads
-
License
ISC
Repository
github
Last release
2 years ago

nestjs-mongolog

Maintainability Rating Code Smells Technical Debt Reliability Rating Vulnerabilities Bugs Security Rating

Description

This package was created with the intention of facilitating and standardizing the writing of nestjs application logs in mongodb databases.

Instalation

npm i --save nestjs-mongolog

Quick Start

Import MongodbLogModule and use forRoot or forRootAsync static methods on your Module for initial configuration:

MongodbLogModule.forRootAsync({
  imports: [ConfigModule],

  useFactory: async (configService: ConfigService) => ({
    connectionString: configService.get<string>('DATABASE_URI'),
    databaseName: configService.get<string>('DATABASE_NAME'),
    collectionName: configService.get<string>('LOGS_COLLECTION_NAME'),
  }),

  inject: [ConfigService],
});

The fields to be passed in the configuration are as follows:

Field NameField Function
connectionStringThe connection uri for your mongodb database.
databaseNameThe name of the database to which the logs will be written.
mongoClientOptionsAdditional settings for mongoClient like: { useUnifiedTopology: true }
collectionNameThe name of the collection where this module's logs will be written.
showInConsoleEnables or disables the display of log messages in the application console.

Import MongodbLogService on your service or controller and use the logging methods provided.

import { Controller, Get } from '@nestjs/common';

@Controller('cats')
export class CatsController {
  constructor(private readonly logger: MongodbLogService) {}

  @Get()
  findAll(): string {
    this.logger.info('listing all cats.', this.findAll.name);
    return 'This action returns all cats';
  }
}

Usage:

await this.logger.info('MESSAGE');
await this.logger.info({ fo: bar, ov: av });
await this.logger.info({ fo: bar, ov: av }, 'currentFunctionName ou any ContextData');
await this.logger.error('MESSAGE');
await this.logger.warn('MESSAGE');
await this.logger.debug('MESSAGE');
await this.logger.silly('MESSAGE');
await this.logger.fatal('MESSAGE');

You don't need to await writing logs if you don't want to.