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 🙏

© 2024 – Pkg Stats / Ryan Hefner

nestjs-logsage

v1.0.3

Published

Powerful logger module for NestJS, seamlessly integrating Pino and Winston for flexible logging with easy configuration.

Downloads

29

Readme

nestjs-logsage

nestjs-logsage is an innovative logging solution designed specifically for Nest.js applications. This pioneering logger package facilitates distributed logging within the Nest.js ecosystem, built on top of both Winston and Pino. This unique combination offers unparalleled flexibility, allowing developers to seamlessly switch between Pino and Winston as the underlying logging mechanism, and customize logging behavior to suit their needs.

Inspired by Java's Mapped Diagnostic Context (MDC) pattern, nestjs-logsage revolutionizes distributed tracing in Nest.js applications. Similar to how the MDC pattern empowers Java developers with comprehensive logging capabilities, this logger package extends similar capabilities to the Nest.js environment, enabling efficient management of contextual information across asynchronous operations.

Workflow

  • Application Code: Emits log messages using the logger provided by the logging framework.
  • Logger: Intercepts log messages generated by the application code, checks the MDC for any contextual information associated with the current thread, and includes it in the log message.
  • Mapped Diagnostic Context (MDC): A thread-local map provided by the logging framework that allows developers to store and retrieve contextual information specific to the current thread.

Features

  • Seamless integration with Nest.js applications
  • Option to choose between Pino and Winston as the logging library
  • Easy configuration management for fine-tuning logging behavior
  • Supports various configuration options such as log levels, output formats, and log destinations
  • Enhanced debugging capabilities for gaining insights into application behavior and performance
  • Distributed logging for managing contextual information across asynchronous operations

Trace ID Management

  • Trace ID Injection: The LoggerMiddleware automatically manages trace IDs within incoming requests. If a trace ID (x-trace-id header, body, query params) is found, it is used throughout the request lifecycle. If not, a unique trace ID is generated and attached to the request, ensuring each request is associated with a trace ID for distributed tracing.

Installation

Install the package via npm:

npm install nestjs-logsage

Usage

Initialize the logger service. You have two LoggerType options: PINO and WINSTON.

//app.module.ts

import { Module } from '@nestjs/common';

import { LoggerType, LoggerModule, } from 'nestjs-logsage';

import { AppController } from './app.controller';
import { AppService } from './app.service';

@Module({
  imports: [
    LoggerModule.forRoot({
      type: LoggerType.PINO,
      options: {
        transport: {
          targets: [
            {
              target: 'pino-pretty',
              options: {
                destination: 'app.log',
                singleLine: true,
                colorize: false,
                levelFirst: false,
                translateTime: 'dd-mm-yyyy hh:mm:ss TT',
              },
            },
            {
              target: 'pino-pretty',
              options: {
                singleLine: true,
                colorize: true,
                levelFirst: false,
                translateTime: 'dd-mm-yyyy hh:mm:ss TT',
              },
            },
          ],
        },
      },
    }),
  ],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}

Now you can inject the loggerService in any of service of nestjs as shown below:

// app.controller.ts

import { Controller, Get } from '@nestjs/common';
import { AppService } from './app.service';
import { LoggerService } from 'nestjs-logsage';

@Controller()
export class AppController {
  constructor(
    private readonly appService: AppService,
    private readonly loggerService: LoggerService,
  ) {}

  @Get()
  getHello() {
    this.loggerService.info('Hello from Controller!');
    return this.appService.getWorld();
  }
}

Example output:

[31-05-2024 04:05:35 AM] INFO: [123]:{"method":"GET","url":"/?x-trace-id=123","headers":{"host":"localhost:3000","user-agent":"curl/8.4.0","accept":"*/*","x-trace-id":"123"},"query":{"x-trace-id":"123"},"body":{}} {"x-trace-id":"123"}
[31-05-2024 04:05:35 AM] INFO: [123]:Hello from Controller! {"x-trace-id":"123"}
[31-05-2024 04:05:35 AM] INFO: [123]:Hello from Controller! {"x-trace-id":"123"}
[31-05-2024 04:05:40 AM] INFO: [123]:[timer: 5012 ms]:Inside app route after 5s {"EXECUTION_LOG_START_TIME":1717131035412,"EXECUTION_LOG_CALLER":"timer"} {"x-trace-id":"123"}

# [time] [level]: [traceId]:[methodName: execution time in ms]: message

Contributing

If you have suggestions for improvements, bug reports, or other contributions, please feel free to open an issue or create a pull request.

License

This project is licensed under the MIT License.