# nest-elastic-logger

> Customized logging solution, powered by Winston, adopts the Elasticsearch format for easy parsing of log entries.

Latest version **2.1.1** (published 2023-11-19) · ISC license · 0 weekly downloads

## Install

```sh
npm install nest-elastic-logger
pnpm add nest-elastic-logger
yarn add nest-elastic-logger
bun add nest-elastic-logger
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.1.1 |
| Published | 2023-11-19 |
| First published | 2023-11-16 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 8 |
| Unpacked size | 73.3 KB |
| Known vulnerabilities | 0 (+4 in 3 direct dependencies) |
| Install scripts | no |
| GitHub stars | 0 |
| Author | JonnyBoy |
| Maintainers | yoniyegorov.dev |

## Links

- npm: https://www.npmjs.com/package/nest-elastic-logger
- Repository: https://github.com/yoni123100/nest-elastic-logger
- Homepage: https://github.com/yoni123100/nest-elastic-logger#readme
- Issues: https://github.com/yoni123100/nest-elastic-logger/issues
- npm.io page: https://npm.io/package/nest-elastic-logger

## Dependencies (8)

- [tsup](https://npm.io/package/tsup.md) ^7.2.0
- [colorts](https://npm.io/package/colorts.md) ^0.1.63
- [express](https://npm.io/package/express.md) ^4.18.2
- [winston](https://npm.io/package/winston.md) ^3.11.0
- [@nestjs/core](https://npm.io/package/@nestjs/core.md) ^8.0.0
- [@nestjs/common](https://npm.io/package/@nestjs/common.md) ^8.0.0
- [winston-daily-rotate-file](https://npm.io/package/winston-daily-rotate-file.md) ^4.7.1
- [@elastic/ecs-winston-format](https://npm.io/package/@elastic/ecs-winston-format.md) ^1.5.0

## Recent versions

- 2.1.1 (latest) — 2023-11-19
- 2.0.1 — 2023-11-16
- 2.0.0 — 2023-11-16

## README

<h1 align="center">Nest Elastic Logger ( BetterLogger )</h1>

<p align="center">
  <a href="https://nestjs.com/" target="blank"><img src="https://nestjs.com/img/logo-small.svg" width="120" alt="Nest Logo" /></a>
</p>

### 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:

```sh
npm i elastic-nest-logger
```

## Setup

`main.ts`

```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`

```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`

```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

```js
[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     |

---
_Source: https://npm.io/package/nest-elastic-logger · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
