1.0.0 • Published 2 years ago

@kibibit/nestjs-winston v1.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago
npm install --save @kibibit/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
              )
    })
  ]
})

Features

  • Supports JSON\YAML files\env variables\cli flags as configuration inputs. See yaml-config in the examples folder
  • Supports shared configuration files (same file shared for multiple projects)
  • initialize a configuration file with --saveToFile or --init
  • save configuration files anywhere above your project's package.json
  • forced singleton for a single installation (reuse same class)
  • testable
  • The ability to create json schemas automatically and add descriptions to configuration variables
  • Get meaningfull errors when configuration is wrong!

Examples

See the examples folder for a variety of usage examples

Contributors ✨

Thanks goes to these wonderful people (emoji key):

This project follows the all-contributors specification. Contributions of any kind are welcome!

Stay in touch

1.0.0

2 years ago

1.0.0-beta.2

2 years ago

1.0.0-beta.3

2 years ago

0.6.0

3 years ago

0.5.0

3 years ago

0.4.0

3 years ago

0.3.0

3 years ago

0.2.0

3 years ago

0.1.0

3 years ago

0.0.9

3 years ago

0.0.8

3 years ago

0.0.7

3 years ago

0.0.6

3 years ago

0.0.5

3 years ago

0.0.4

3 years ago

0.0.3

3 years ago

0.0.2

3 years ago

0.0.1

3 years ago