npm.io
2.1.4 • Published 4 years ago

nestjs-mongolog

Licence
ISC
Version
2.1.4
Deps
6
Size
34 kB
Vulns
1
Weekly
0

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 Name Field Function
connectionString The connection uri for your mongodb database.
databaseName The name of the database to which the logs will be written.
mongoClientOptions Additional settings for mongoClient like: { useUnifiedTopology: true }
collectionName The name of the collection where this module's logs will be written.
showInConsole Enables 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.

Keywords