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

@payk/nestjs-winston

v1.2.1

Published

Winston for Nest has never been this easy

Downloads

585

Readme

Installation

npm install --save @payk/nestjs-winston

What does it do?

This package not only wraps winston into a Nest Module like other packages, it also creates a Nest LoggerService, so you can keep using the default NestJS logger, which enjoying winston. But that's not all, it also takes those great things from the NestJS Logger. It adds the context into the winston meta (so it can later be search and indexed in your ELK/Datadog).

Another great feature is the winston formatter add to the class that in local mode allows easy and readable logs to the console.

Quick Start

Import WinstonModule and use the forRoot() method to configure it. This method accepts the same options object as createLogger() function from the winston package:

import { Module } from '@nestjs/common';
import { WinstonModule } from '@payk/nestjs-winston';
import * as winston from 'winston';

@Module({
  imports: [
    WinstonModule.forRoot({
      // options here
    }),
  ],
})
export class AppModule {}

Async configuration

Caveats: because the way Nest works, you can't inject dependencies exported from the root module itself (using exports). If you use forRootAsync() and need to inject a service, that service must be either imported using the imports options or exported from a global module.

Maybe you need to asynchronously pass your module options, for example when you need a configuration service. In such case, use the forRootAsync() method, returning an options object from the useFactory method:

import { Module } from '@nestjs/common';
import { WinstonModule } from '@payk/nestjs-winston';
import * as winston from 'winston';

@Module({
  imports: [
    WinstonModule.forRootAsync({
      useFactory: () => ({
        // options
      }),
      inject: [],
    }),
  ],
})
export class AppModule {}

The factory might be async, can inject dependencies with inject option and import other modules using the imports option.

Use as the main Nest Logger (preferred way)

If you want to use winston logger across the whole app, including bootstrapping and error handling, use the following:

Define:

import { WINSTON_MODULE_NEST_PROVIDER } from '@payk/nestjs-winston';

async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  app.useLogger(app.get(WINSTON_MODULE_NEST_PROVIDER));
}
bootstrap();

Use:

import { WinstonLogger } from '@payk/nestjs-winston';

export class ClassName {
  private readonly logger = new WinstonLogger(ClassName.name);
}

Nest Winston Formatter

To allow a better visibility a unique formatter is provided

import { winstonConsoleFormat } from '@payk/nestjs-winston';

WinstonModule.forRoot({
  level: 'info',
  //format: winston.format.json(),
  defaultMeta: { service: 'user-service' },
  transports: [

    new winston.transports.Console({
      format: winston.format.combine(
                winston.format.timestamp(),
                winston.format.colorize({ all: true }),
                winstonConsoleFormat
              )
    })
  ]
})