Skip to content

Byndyusoft/node-pino-logger-factory

Repository files navigation

node-pino-logger-factory

npm@latest test code style: prettier semantic-release

🌲 logger factory for pino 🌲

Requirements

  • Node.js v14 LTS or later
  • npm or yarn

Install

npm install @byndyusoft/pino-logger-factory pino pino-http && npm install -D pino-pretty

or

yarn add @byndyusoft/pino-logger-factory pino pino-http && yarn add -D pino-pretty

Environment

You must initialize process.env before creating pino logger:

process.env.npm_package_name;
process.env.npm_package_version;
process.env.CONFIG_ENV ?? process.env.NODE_ENV;
process.env["BUILD_*"];

Usage

Create pino logger:

import { PinoLoggerFactory } from "@byndyusoft/pino-logger-factory";

const logger = new PinoLoggerFactory().create();

Create pino-http logger:

import { PinoHttpLoggerFactory } from "@byndyusoft/pino-logger-factory";

const httpLogger = new PinoHttpLoggerFactory().create();

Usage with nestjs-pino

Import and configure modules:

import {
  PinoHttpLoggerOptionsBuilder,
  PinoLoggerFactory,
} from "@byndyusoft/pino-logger-factory";
import { Module } from "@nestjs/common";
import { LoggerModule } from "nestjs-pino";

@Module({
  imports: [
    LoggerModule.forRootAsync({
      useFactory: () => ({
        pinoHttp: new PinoHttpLoggerOptionsBuilder()
          .withLogger(new PinoLoggerFactory().create())
          .build(),
      }),
    }),
  ],
})
export class InfrastructureModule {}

Custom serializers

  • debugObjectSerializer

Under the hood uses util.inspect() for return human-readable a string representation values of object

  • jsonDebugObjectSerializer

Return JSON string representation values of object

Usage custom serializers

Configure modules

import {
  PinoHttpLoggerOptionsBuilder,
  PinoLoggerFactory,
  debugObjectSerializer,
  jsonDebugObjectSerializer,
} from "@byndyusoft/pino-logger-factory";
import { Module } from "@nestjs/common";
import { LoggerModule } from "nestjs-pino";

@Module({
  imports: [
    LoggerModule.forRootAsync({
      useFactory: () => ({
        pinoHttp: new PinoHttpLoggerOptionsBuilder()
          .withLogger(new PinoLoggerFactory().create())
          .withSerializers({
            // you can use any name for key
            debugData: debugObjectSerializer,
            debugJsonData: jsonDebugObjectSerializer,
          })
          .build(),
      }),
    }),
  ],
})
export class InfrastructureModule {}
logger.info({
  msg: "some message",
  // the object whose fields you want to serialize to human-readable string representation
  debugData: {
    entity: {
      id: 1,
      orders: [1, 2],
    },
  },
  // the object whose fields you want to serialize to JSON string representation
  debugJsonData: {
    entity: {
      id: 1,
      orders: [1, 2],
    },
  },
});

Maintainers

License

This repository is released under version 2.0 of the Apache License.