Gathering detailed insights and metrics for nest-winston-module
Gathering detailed insights and metrics for nest-winston-module
Gathering detailed insights and metrics for nest-winston-module
Gathering detailed insights and metrics for nest-winston-module
nest-winston
A Nest module wrapper for winston
@encoderinc/nest-js-module-winston-logdna
@globaluy/winston
Nest logger module.
logstash-winston-3
This NPM module provides a LogService class for centralized logging in microservices. It uses Winston library to format and send logs to Logstash for further processing by Elasticsearch. This class can be used in your microservices to centralize and stand
npm install nest-winston-module
Typescript
Module System
Node Version
NPM Version
60.5
Supply Chain
80.3
Quality
72.7
Maintenance
50
Vulnerability
99.6
License
Total Downloads
3,586
Last Day
1
Last Week
2
Last Month
27
Last Year
239
Minified
Minified + Gzipped
Latest Version
0.1.3
Package Id
nest-winston-module@0.1.3
Unpacked Size
43.49 kB
Size
8.68 kB
File Count
20
NPM Version
6.14.8
Node Version
12.19.0
Cumulative downloads
Total Downloads
Last Day
0%
1
Compared to previous day
Last Week
100%
2
Compared to previous week
Last Month
-15.6%
27
Compared to previous month
Last Year
-24.4%
239
Compared to previous year
4
3
A Nest module wrapper for winston , provides multi type of loggers.
Inspired by nest-winston
1npm install --save nest-winston-module winston
Import WinstonModule
into the root AppModule
and use the forRoot()
method to configure it. This method accepts the same options object as createLogger()
function from the winston package:
1import { Module } from '@nestjs/common'; 2import { WinstonModule } from 'nest-winston-module'; 3import * as winston from 'winston'; 4 5@Module({ 6 imports: [ 7 WinstonModule.forRoot({ 8 // options 9 }), 10 ], 11}) 12export class AppModule {}
Afterward, the winston instance will be available to inject across entire project using the winston
injection token,
nest-winston-module
provide a token enum for you, to make handling different type of logs easier. here is what the injection token enum looks like:
1enum WinstonProviderEnum { 2 /** 3 * @description for replacing nest core logger 4 */ 5 coreProvider = 'winstonCoreProvider', 6 /** 7 * @description application level logger 8 */ 9 appProvider = 'winstonAppProvider', 10 /** 11 * @description controller logger 12 */ 13 controllerProvider = 'winstonControllerProvider', 14 /** 15 * @description graphQL resolver logger 16 */ 17 resolverProvider = 'winstonResolverProvider', 18 /** 19 * @description service logger 20 */ 21 serviceProvider = 'winstonServiceProvider', 22 /** 23 * @description console logger 24 */ 25 consoleProvider = 'winstonConsoleProvider', 26 /** 27 * @description all logger providers map 28 */ 29 loggersProvider = 'winstonLoggersProvider', 30}
Here is an example using winston logger in Nest Controller
:
1import { Controller, Inject } from '@nestjs/common'; 2import { WinstonProviderEnum, NestWinstonLogger } from 'nest-winston-module'; 3 4@Controller('cats') 5export class CatsController { 6 constructor(@Inject(WinstonProviderEnum.controllerProvider) private readonly logger: NestWinstonLogger) { } 7}
Note that WinstonModule
is a global module, it will be available in all your feature modules.
Caveats: because the way Nest works, you can't inject dependencies exported from the root module itself (using
exports
). If you useforRootAsync()
and need to inject a service, that service must be either imported using theimports
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:
1import { Module } from '@nestjs/common'; 2import { WinstonModule } from 'nest-winston-module'; 3import * as winston from 'winston'; 4 5@Module({ 6 imports: [ 7 WinstonModule.forRootAsync({ 8 useFactory: () => ({ 9 // options 10 }), 11 inject: [], 12 }), 13 ], 14}) 15export class AppModule {}
The factory might be async, can inject dependencies with inject
option and import other modules using the imports
option.
Alternatively, you can use the useClass
syntax:
1WinstonModule.forRootAsync({ 2 useClass: WinstonConfigService, 3})
With the above code, Nest will create a new instance of WinstonConfigService
and its method createWinstonModuleOptions
will be called in order to provide the module options.
Apart from application logging, this module also provides the WinstonLogger
custom implementation, for use with the Nest logging system. Example main.ts
file:
1import { WinstonProviderEnum } from 'nest-winston-module'; 2 3async function bootstrap() { 4 const app = await NestFactory.create(AppModule); 5 app.useLogger(app.get(WinstonProviderEnum.coreProvider)); 6} 7bootstrap();
Here the get()
method on the NestApplication instance is used to retrieve the singleton instance of WinstonLogger
class, which is still configured using either WinstonModule.forRoot
or WinstonModule.forRootAsync
methods.
When using this technique, you can only inject the logger using the WinstonProviderEnum.coreProvider
token. Because winston
logger interface is different from Nest
logger interface.
Using the dependency injection has one minor drawback. Nest has to bootstrap the application first (instantiating modules and providers, injecting dependencies, etc) and during this process the instance of WinstonLogger
is not yet available, which means that Nest falls back to the internal logger.
One solution is to create the logger outside of the application lifecycle, using the createLogger
function, and pass it to NestFactory.create
:
1import { WinstonModule } from 'nest-winston-module'; 2 3async function bootstrap() { 4 const app = await NestFactory.create(AppModule, { 5 logger: WinstonModule.createLogger({ 6 // options (same as WinstonModule.forRoot() options) 7 }) 8 }); 9} 10bootstrap();
By doing this, you give up the dependency injection, meaning that WinstonModule.forRoot
and WinstonModule.forRootAsync
are not needed anymore.
To use the logger also in your application, change your main module to provide the Logger
service from @nestjs/common
:
1import { Logger, Module } from '@nestjs/common'; 2 3@Module({ 4 providers: [Logger], 5}) 6export class AppModule {}
Then simply inject the Logger
:
1import { Controller, Inject, Logger, LoggerService } from '@nestjs/common'; 2 3@Controller('cats') 4export class CatsController { 5 constructor(@Inject(Logger) private readonly logger: LoggerService) { } 6}
This works because Nest Logger
wraps our winston logger (the same instance returned by the createLogger
method) and will forward all calls to it.
The module also provides a custom Nest-like special formatter for console transports:
1import { Module } from '@nestjs/common'; 2import { 3 coreOptions, 4 appOptions, 5 controllerOptions, 6 resolverOptions, 7 serviceOptions, 8 consoleOptions, 9} from 'nest-winston-module'; 10import * as winston from 'winston'; 11 12@Module({ 13 imports: [ 14 WinstonModule.forRoot({ 15 core: coreOptions, 16 app: appOptions, 17 resolver: resolverOptions, 18 controller: controllerOptions, 19 service: serviceOptions, 20 console: consoleOptions, 21 directory: process.cwd() + '/logs', 22 }), 23 ], 24}) 25export class AppModule {}
No vulnerabilities found.
No security vulnerabilities found.