@samokat/nest-logger
TypeScript icon, indicating that this package has built-in type declarations

1.2.1 • Public • Published

nest-logger

Provides simple NestJS-module to create logs in cool, grafana-friendly format.

Why this library:

  • simple way — common NestJS module
  • uniform — json can be parsed from stdin/stderr
  • trace_id — it provides uniq id for every request

Usage

Install it

yarn add @samokat/nest-logger

Add it to AppModule

// app.module.ts
import { Module, NestModule } from '@nestjs/common';
import { LoggerModule } from '@samokat/nest-logger';

@Module({
  imports: [
    LoggerModule.forRoot('my-favorite-project'),
  ],
})
export class AppModule implements NestModule {
  public configure() {
    // pass
  }
}

Setup TraceId genrator:

// main.ts
import { NestFactory } from '@nestjs/core';
import { expressMiddleware } from 'cls-rtracer';
import { AppModule } from './app.module';

async function bootstrap() {
  const app = await NestFactory.create(AppModule);

  app.use(expressMiddleware());

  await app.listen(3000);
}

bootstrap();

Use it in any place of your app

// any-file.ts
import { Logger } from '@samokat/nest-logger';

@Injectable()
export class AnyService {
  constructor(
    private readonly logger: Logger,
  ) {}

  async doAnything(someData: any): Promise<void> {
    this.logger.log('hello from #doAnything', someData)

    if (someData.length === 0) {
      this.logger.error('error =(', someData)
    }
  }
}

Details

TraceID Generation

Every log bounded for request, it can help to trace logs by requests. We use great cls-rtracer library for generation TraceID, please install and setup it separately. If you using Express-adapter, you should just do instruction from the first block. Otherwise, please refer to cls-rtracer documentation.

Readme

Keywords

none

Package Sidebar

Install

npm i @samokat/nest-logger

Weekly Downloads

1

Version

1.2.1

License

MIT

Unpacked Size

22.3 kB

Total Files

26

Last publish

Collaborators

  • akrylov.samokat
  • okorovin-samokat