🌲 logger factory for pino 🌲
- Node.js v14 LTS or later
- npm or yarn
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
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_*"];
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();
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 {}
- 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
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],
},
},
});
This repository is released under version 2.0 of the Apache License.