I'm new to NestJS and don't understand it completely. Any help is appreciated.
I'm trying to configure Winston logger globally so I can use it in all modules without the need to import it for every module.
// main.ts
import { WinstonModule } from 'nest-winston';
import * as winston from 'winston';
async function bootstrap() {
process.env.APP_ENV !== 'test' && process.env.APP_ENV !== 'local' && require('newrelic');
const app = await NestFactory.create(AppModule, {
logger: WinstonModule.createLogger({
transports: [
new winston.transports.Console({
format: winston.format.combine(winston.format.timestamp(), winston.format.ms()),
}),
],
}),
});
}
bootstrap();
// app.module.ts
import { LoggerConfig } from './config/logger.config';
import { WinstonModule } from 'nest-winston';
const logger: LoggerConfig = new LoggerConfig();
@Module({
imports: [AppConfigModule, HealthModule, CouponModule, WinstonModule.forRoot(logger.console())],
controllers: [],
providers: [],
})
export class AppModule {}
// coupon.controller.ts
@Controller(BASE_ROUTE)
export class CouponController {
constructor(private couponService: CouponService, private readonly logger: Logger) {}
Error: Nest can't resolve dependencies of the CouponController (CouponService, ?). Please make sure that the argument Logger at index [1] is available in the CouponModule context. Potential solutions:
- If Logger is a provider, is it part of the current CouponModule?
- If Logger is exported from a separate @Module, is that module imported within CouponModule?
It works if I import Logger
in coupon.module.ts
, but I don't want to import it in all modules separately. Can anyone please tell what am I missing?
Thank you.