nest icon indicating copy to clipboard operation
nest copied to clipboard

[Question] Custom logger

Open szkumorowski opened this issue 9 years ago • 62 comments

Is it possible to inject custom logger into nest components ?

szkumorowski avatar Nov 16 '17 12:11 szkumorowski

Wouldn't you use it as normal? E.g. for Winston i would import and configure it within your root main.ts and then just import it again in whatever file you need it which will give you the configured logger.

I don't think injecting is the right way. Just use import as you would do with RxJS or Moment.js for example.

MickL avatar Nov 16 '17 19:11 MickL

Hi @szkumorowski, Could you explain what do you mean?

kamilmysliwiec avatar Nov 19 '17 19:11 kamilmysliwiec

Hi,

I'm using a winston logger for application, I would like to set kind bridge for it which will resolve to use my custom logger at the end. Example with typeorm: https://github.com/typeorm/typeorm/blob/master/docs/logging.md

With above example we can implement the logger and simply pass the instance to it.

szkumorowski avatar Nov 20 '17 10:11 szkumorowski

Just import it you dont need dependency injection here

MickL avatar Nov 20 '17 11:11 MickL

You don't understand me - sorry I mean set instead of inject, I would like to set my custom logger to be used in nest components in replace for default one.

szkumorowski avatar Nov 20 '17 11:11 szkumorowski

@szkumorowski

Is it possible to inject custom logger into nest components ?

Not now

cojack avatar Nov 20 '17 12:11 cojack

Honestly, I still don't understand. Why not create a logger component and inject it into the components?

kamilmysliwiec avatar Nov 20 '17 12:11 kamilmysliwiec

@kamilmysliwiec he wanna to override your logger

cojack avatar Nov 20 '17 12:11 cojack

@cojack but why? Nest logger is used only to show the progress of bootstrapping the application. Winson has a completely different purpose.

kamilmysliwiec avatar Nov 20 '17 12:11 kamilmysliwiec

@kamilmysliwiec I have just explain the question ;)

cojack avatar Nov 20 '17 13:11 cojack

@kamilmysliwiec Not only, Its used to show errors which comes from nest components either.

szkumorowski avatar Nov 20 '17 13:11 szkumorowski

@szkumorowski only when there's no custom exception filter setup. You can create your own and handle each Error in it 🙂

kamilmysliwiec avatar Nov 20 '17 13:11 kamilmysliwiec

Btw, it might be possible, if the Logger will be part of the components, than we will be able to override it by providers ;>

@kamilmysliwiec

cojack avatar Nov 20 '17 14:11 cojack

You should be able to substitute LoggerService like this:

import { LoggerService } from '@nestjs/common';
import { CustomLogger } from './path/to/custom_logger';

And in your components (which should be renamed to providers in v5 @kamilmysliwiec )

{ provide: LoggerService, useClass: CustomLogger } 

But if this isn't possible, it should be.

wbhob avatar Nov 20 '17 15:11 wbhob

@wbhob Logger doesn't belong to any scope, so it won't be possible to provide it in the module metadata.

btw components won't be renamed to providers

kamilmysliwiec avatar Nov 20 '17 15:11 kamilmysliwiec

I should have said "could in my opinion"... I just think that either there should be explanations for why things are named as they are (that is differently from Angular, ex. @Injectable() and @Component(), or providers and components), or a review/survey of how those APIs are structured. It seems to deviate too much from Angular without good reason, but I'm sure there may be a reason that I just don't understand.

I'm sure you probably hate me for challenging you on literally everything but I honestly just want to make sure that any ambiguity is cleared up, and the transition for Angular devs to pick up Nest is clear. When writing a framework, it's critical that in addition to having well-documented APIs, that the reasoning behind them is clear. That's something Angular does very well and I'd be happy to contribute to the docs one I understand those reasonings with Nest. 👍

wbhob avatar Nov 20 '17 16:11 wbhob

@wbhob no hate at all 🙂 I have explained it here https://github.com/nestjs/nest/issues/254

kamilmysliwiec avatar Nov 20 '17 21:11 kamilmysliwiec

Yep, I saw that! That's a good point. Does Nest have support for provider objects/substitution (like useClass, useValue, useFactory)

wbhob avatar Nov 20 '17 21:11 wbhob

@wbhob yes for sure, all of them

kamilmysliwiec avatar Nov 20 '17 21:11 kamilmysliwiec

@szkumorowski has exception filter helped with your issue?

kamilmysliwiec avatar Nov 21 '17 11:11 kamilmysliwiec