2.1.1 • Published 6 months ago

nest-elastic-logger v2.1.1

Weekly downloads
-
License
ISC
Repository
github
Last release
6 months ago

Introducing nest-elastic-logger, a logging library developed to reformat default Nest logger logs and generate rotating log files for integration with log shippers (like Filebeat).

Designed to enhance logging capabilities for improved readability and efficient log management.

Useful for:

  • Easier log readably
  • Log shippers ( Tested on Filebeat )
  • Monitoring logs

Features

  • Comes with Elasticsearch and JSON format out of the box
  • Automatic rotation of Log files
  • Custom print format

Installation:

npm i elastic-nest-logger

Setup

main.ts

export const bootstrap = async () => {
  const app = await NestFactory.create(AppModule);
  const customLogger = app.get(BetterLoggerService);

  app.useLogger(customLogger); // Use as global logger
  await app.listen(3000);
};

app.module.ts

import { Module } from '@nestjs/common';
import { BetterLoggerModule } from 'nest-elastic-logger';
import { AppController } from './app.controller';

@Module({
  imports: [BetterLoggerModule.forRoot({ serviceName: 'ServiceName', serviceVersion: '1.0.0' })],
  providers: [],
  controllers: [AppController],
})
export class AppModule {}

Usage

app.controller.ts

import { Controller, OnModuleInit } from '@nestjs/common';
import { BetterLoggerService } from 'nest-elastic-logger';

@Controller()
export class AppController implements OnModuleInit {
  private logger: BetterLoggerService = new BetterLoggerService('AppController'); // AppContoller context

  onModuleInit() {
    this.logger.log(
      'Hello There', // Message
      'onModuleInit', // Function Name
      { counter: 5, test: 'Hi' }, // Optional Args
      'extra data', // Optional Args // Optional Args
      'another extra data', // Optional Args
      { device: 'Cool Device' }, // Optional Args
    );
  }
}

output

[WhatupBackend v1.0.0] [15-11-2023 05:03:17] - [AppController] - [onModuleInit] - INFO - Hello There
Addional Details:
counter: 5
test: Hi
misc: extra data,another extra data
device: Cool Device

BetterLoggerSettings Type

PropertyType
serviceNamestring
serviceVersionstring
removeNewlineFormatboolean
printFormatwinston.Logform.Format
dailyRotateOptionsLimitedDailyRoatateOptions

LimitedDailyRoatateOptions Type

PropertyType
maxSizestring
maxFilesstring

Available Functions (BetterLoggerService)

FunctionTypeLog Level
logstringINFO
warnstringWARN
errorstringERROR
debugstringDEBUG
2.1.1

6 months ago

2.0.1

6 months ago

2.0.0

6 months ago

1.0.7

6 months ago

1.0.6

6 months ago

1.0.5

6 months ago

1.0.4

6 months ago

1.0.3

6 months ago

1.0.2

6 months ago

1.0.1

6 months ago

1.0.0

6 months ago