poc-nest-winston v1.3.3
Installation
npm install --save nest-winston winston
Quick Start
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:
import { Module } from '@nestjs/common';
import { WinstonModule } from 'nest-winston';
import * as winston from 'winston';
@Module({
imports: [
WinstonModule.forRoot({
// options
}),
],
})
export class AppModule {}
Afterward, the winston instance will be available to inject across entire project using the winston
injection token:
import { Controller, Inject } from '@nestjs/common';
import { WINSTON_MODULE_PROVIDER } from 'nest-winston';
import { Logger } from 'winston';
@Controller('cats')
export class CatsController {
constructor(@Inject(WINSTON_MODULE_PROVIDER) private readonly logger: Logger) { }
}
Note that WinstonModule
is a global module, it will be available in all you feature modules.
Async configuration
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:
import { Module } from '@nestjs/common';
import { WinstonModule } from 'nest-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.
Alternatively, you can use the useClass
syntax:
WinstonModule.forRootAsync({
useClass: WinstonConfigService,
})
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.
Use as the main Nest logger
If you want to use winston logger across the whole app use the following:
import { WINSTON_MODULE_NEST_PROVIDER } from 'nest-winston';
async function bootstrap() {
const app = await NestFactory.create(AppModule);
app.useLogger(app.get(WINSTON_MODULE_NEST_PROVIDER));
}
bootstrap();
The module also provides a custom Nest-like special formatter for console transports:
import { Module } from '@nestjs/common';
import { utilities as nestWinstonModuleUtilities, WinstonModule } from 'nest-winston';
import * as winston from 'winston';
@Module({
imports: [
WinstonModule.forRoot({
transports: [
new winston.transports.Console({
format: winston.format.combine(
winston.format.timestamp(),
nestWinstonModuleUtilities.format.nestLike(),
),
}),
// other transports...
],
// other options
}),
],
})
export class AppModule {}
When creating loggers from WinstonModule
, Nest has to bootstrap the application first. This means instantiating all the modules and the providers, injecting dependencies, etc. During this "bootstrapping" process, instances of WinstonLogger
are not available which means Nest falls back to an internal logger.
In order to have a winston logger used during bootstrapping, the logger has to created outside of the application lifecycle and passed to NestFactory.create
as an option:
import { WinstonModule } from 'nest-winston';
async function bootstrap() {
const app = await NestFactory.create(AppModule, {
logger: WinstonModule.createLogger({
// options (same as WinstonModule.forRoot() options)
})
});
}
bootstrap();
The bootstrapping logger will be used unless another logger is set (i.e. when app.useLogger()
is called).
Contributing
New features and bugfixes are always welcome! In order to contribute to this project, follow a few easy steps:
- Fork this repository, clone it on your machine and run
npm install
- Open your local repository with Visual Studio code and install all the suggested extensions
- Create a branch
my-awesome-feature
and commit to it - Run
npm run lint
,npm run test
andnpm run build
and verify that they complete without errors - Push
my-awesome-feature
branch to GitHub and open a pull request
4 years ago