1

I'm making a console application that returns a lot of information directly to the console. I do this with a custom logger. Actually my logger implementation is just a frontend to print (with some extras).

The application is quite complex and I made it in DDD style, but one thing doesn't work for me - almost every business class that does something, I pass an instance of Logger through DI. First of all, this is very cumbersome, and secondly it obscures the code. Business classes have a Logger dependency that might as well be turned off, because if the action executes correctly, it doesn't return anything anyway.

The question is - do you pass the logger instance to your business classes explicitly via DI or do you have a global logger object that you use directly in the code? Or any other way? Maybe you have some rule of thumb for when DI and when global?

etroby
  • 19
  • 2
  • One option is to use the Decorator pattern for the loggers - you create a transparent wrapper that logs around relevant operations, then instead of your regular instances, you inject wrapped instances. – Filip Milovanović Aug 15 '23 at 14:48
  • 1
    Does this answer your question? [Should I use a global logging variable?](https://softwareengineering.stackexchange.com/q/446541) – Doc Brown Aug 15 '23 at 15:07
  • Does this answer your question? [Should I use a global logging variable?](https://softwareengineering.stackexchange.com/questions/446541/should-i-use-a-global-logging-variable) – Greg Burghardt Aug 15 '23 at 17:05
  • You might want to review: https://web.archive.org/web/20120826171609/http://www.mockobjects.com:80/2007/04/test-smell-logging-is-also-feature.html – VoiceOfUnreason Aug 15 '23 at 22:42
  • Welcome. The question in its current state looks like a "poll-style question", which is likely to be closed. Express the question part differently. – Tulains Córdova Aug 17 '23 at 09:30

1 Answers1

0

The safe default is to never use globals.

Sometimes, after finishing something it turns out that a global (aka singleton) would have sufficed, of course. And yes, passing dependencies seems like a chore. But the costs of always using DI are small, in comparison to the benefits.

fdreger
  • 258
  • 1
  • 4