1.2.1 • Published 4 years ago

@samokat/nest-logger v1.2.1

Weekly downloads
19
License
MIT
Repository
github
Last release
4 years ago

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.

1.2.0

4 years ago

1.1.1

4 years ago

1.2.1

4 years ago

1.1.0

4 years ago

1.0.0

4 years ago