nestjs-mongolog
v2.1.4
Published
Package for logging things into mongodb.
Downloads
2
Maintainers
Readme
nestjs-mongolog
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.