nest-elastic-logger
v2.1.1
Published
Customized logging solution, powered by Winston, adopts the Elasticsearch format for easy parsing of log entries.
Downloads
4
Readme
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
| Property | Type | | ------------------- | -------------------------- | | serviceName | string | | serviceVersion | string | | removeNewlineFormat | boolean | | printFormat | winston.Logform.Format | | dailyRotateOptions | LimitedDailyRoatateOptions |
LimitedDailyRoatateOptions Type
| Property | Type | | -------- | ------ | | maxSize | string | | maxFiles | string |
Available Functions (BetterLoggerService)
| Function | Type | Log Level | | -------- | ------ | --------- | | log | string | INFO | | warn | string | WARN | | error | string | ERROR | | debug | string | DEBUG |