Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/pay-k/nestjs-winston
https://github.com/pay-k/nestjs-winston
nest nestjs-logger winston
Last synced: about 2 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/pay-k/nestjs-winston
- Owner: pay-k
- Created: 2019-09-17T15:51:57.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2022-12-30T18:40:49.000Z (about 2 years ago)
- Last Synced: 2024-11-15T03:54:20.518Z (2 months ago)
- Topics: nest, nestjs-logger, winston
- Language: TypeScript
- Size: 168 KB
- Stars: 10
- Watchers: 2
- Forks: 2
- Open Issues: 11
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
nestjs-winston
Winston for NestJS has never been this easy!
## Installation
```bash
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()`](https://github.com/winstonjs/winston#usage) function from the winston package:
```typescript
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](https://docs.nestjs.com/modules#global-modules).
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:
```typescript
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:
```typescript
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:
```typescript
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
```typescript
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
)
})
]
})
```