JSPM

nestjs-pino

0.1.0
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 679595
  • Score
    100M100P100Q181673F
  • License MIT

Pino logger for NestJS

Package Exports

  • nestjs-pino

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (nestjs-pino) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

nestjs-pino

npm Travis (.org) Coverage Status

Logging in NestJS with automatic tracing on every layer

Example

// app.controller.ts
@Controller()
export class AppController {
  constructor(
    private readonly myService: MyService,
    private readonly logger: Logger
  ) {}

  @Get()
  getHello(): string {
    this.logger.log("calling AppController.getHello");
    return `Hello ${this.myService.getWorld()}`;
  }
}

// my.service.ts
import { Logger } from 'nestjs-pino';

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

  getWorld() {
    this.logger.debug("calling MyService.getWorld");
    return "World!";
  }
}

output:

{"level":30,"time":1568720266616,"pid":25566,"hostname":"my-host","req":{"id":1,"method":"GET","url":"/","headers":{...},"remoteAddress":"::1","remotePort":53753},"msg":"calling AppController.getHello","v":1}

{"level":20,"time":1568720266616,"pid":25566,"hostname":"my-host","req":{"id":1,"method":"GET","url":"/","headers":{...},"remoteAddress":"::1","remotePort":53753},"msg":"calling MyService.getWorld","v":1}

{"level":30,"time":1568720266623,"pid":25566,"hostname":"my-host","req":{"id":1,"method":"GET","url":"/","headers":{...},"remoteAddress":"::1","remotePort":53753},"res":{"statusCode":200,"headers":{...}},"responseTime":9,"msg":"request completed","v":1}

Install

npm i nestjs-pino

Using

Setup middlewares

First of all you shoud setup middlewares, that allows to do:

  • automatic logging of every request/response
  • automatic binding each log to it's request context when using Logger service
import { createLoggerMiddlewares } from 'nestjs-pino';

const app = await NestFactory.create(MyModule);

// basic example
app.use(...createLoggerMiddlewares());

// if you want to configure logger somehow depending on your ConfigService
// you can do something like that:
const config = app.get(ConfigService);
app.use(...createLoggerMiddlewares({ level: config.logLevel }));

createLoggerMiddlewares API is the same as express-pino-logger

Providing Logger service

Just add Logger as provider to your module:

import { Provider } from "@nestjs/common";
import { Logger } from 'nestjs-pino';

@Module({
  providers: [Logger]
})
class MyModule {}

Usage as Logger service

Logger implements standard NestJS LoggerService interface. So if you are familiar with built in NestJS logger you are good to go.

Usage as NestJS app logger

const app = await NestFactory.create(MyModule, { logger: false });
app.useLogger(app.get(Logger));

FAQ

Q: How does it work?

A: It use express-pino-logger under hood, so every request has it's own child-logger, and with help of async_hooks Logger can get it while calling own methods. So your logs can be groupped by req.id.

Q: Why use async_hooks instead of REQUEST scope?

A: REQUEST scope can have perfomance issues depending on your app. TL;DR: using it will cause to instantiating every class, that injects Logger, as a result it will slow down your app.

Q: I'm using old nodejs version, will it work for me?

A: Please read this.

Q: What about pino built in methods/levels?

A: Pino built in methods are not compatible to NestJS built in LoggerService methods, so decision is to map pino methods to LoggerService methods to save Logger API:

  • trace=verbose
  • debug=debug
  • info=log
  • warn=warn
  • error=error