npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

@relab/nestjs-logging

v8.0.1

Published

Logging configuration for Nest.js

Readme

@relab/nestjs-logging

Overview

@relab/nestjs-logging provides a robust, flexible logging solution for NestJS applications, powered by pino. It supports both human-friendly "pretty" logs for development and structured JSON logs for production, making it easy to integrate with observability tools like OpenTelemetry Collector.

This package also integrates seamlessly with distributed tracing via @relab/nestjs-trace-context, ensuring trace context is included in your logs.


Features

  • Pino-based logging: Fast, low-overhead, and highly configurable.
  • Pretty or JSON output: Choose between developer-friendly or machine-readable logs.
  • Trace context propagation: Automatically includes trace IDs and span IDs in logs.
  • Unhandled error logging: Simple helpers to log uncaught exceptions and unhandled promise rejections.
  • Custom attributes and resource metadata: Enrich logs with contextual information.
  • Per-area log levels: Fine-grained control over log verbosity for different parts of your app.

Installation

pnpm add @relab/nestjs-logging @relab/nestjs-trace-context pino pino-http pino-pretty

Usage

1. Main Application Setup

// main.ts
import { configureLogger, options as loggerOptions, processUnhandledError } from '@relab/nestjs-logging'

const app = /* create nestjs app */ createApp(/* ... */, { ...loggerOptions })

configureLogger(app)

process.on(
    'uncaughtException',
    processUnhandledError({
        level: 'info',
        layout: process.env.NODE_ENV === 'production' ? 'json' : 'pretty',
    }),
)

process.on(
    'unhandledRejection',
    processUnhandledError({
        level: 'info',
        layout: process.env.NODE_ENV === 'production' ? 'json' : 'pretty',
    }),
)

2. Module Integration

// app.module.ts
import { MiddlewareConsumer, Module, NestModule } from '@nestjs/common'
import { LoggerModule } from '@relab/nestjs-logging'
import { TraceContextMiddleware, TraceContextModule } from '@relab/nestjs-trace-context'

@Module({
    imports: [
        TraceContextModule,
        LoggerModule.configure({
            level: 'info',
            layout: process.env.NODE_ENV === 'production' ? 'json' : 'pretty',
        }),
    ],
})
export class AppModule implements NestModule {
    configure(consumer: MiddlewareConsumer) {
        consumer.apply(TraceContextMiddleware).forRoutes('*')
    }
}

API

LoggerModule.configure(options)

Registers and configures the logger for your NestJS application.

Options

| Name | Type | Required | Description | |--------------|-------------------------------------------|----------|----------------------------------------------------------------------------------------------| | level | 'fatal' \| 'error' \| 'warn' \| 'info' \| 'debug' \| 'trace' | Yes | Minimum log level to output. | | layout | 'json' | 'pretty' | Yes | Log output format. Use 'pretty' for development, 'json' for production. | | resource | Record<string, string> | No | Key-value pairs describing the service/resource (e.g., service name, version, environment). | | attributes | (entry: unknown, level: Level) => Record<string, string> | No | Function to add custom attributes to each log entry. | | levels | Record<string, Level> | No | Per-area log levels. Allows overriding log level for specific logger contexts. |

Example

LoggerModule.configure({
    level: 'info',
    layout: 'json',
    resource: { service: 'my-service', version: '1.0.0' },
    attributes: (entry, level) => ({ custom: 'value' }),
    levels: { MyController: 'debug' },
})

Other Exports

  • configureLogger(app): Sets up the logger instance for your NestJS app.
  • options: Default options for NestJS app creation ({ bufferLogs: true }).
  • processUnhandledError(options): Returns a handler for logging uncaught exceptions and unhandled rejections.
  • createLogger(options): Creates a standalone pino logger instance with the given options.

Best Practices

  • Use 'pretty' layout for local development to get colorized, readable logs.
  • Use 'json' layout in production for structured logs compatible with log aggregation and observability tools.
  • Always include TraceContextModule and TraceContextMiddleware to enable distributed tracing context in your logs.

License

MIT