1

While working on a web (JEE based) application I saw some different ways people have instantiated loggers in different classes. First way is classic way like,

private static final Logger logger = LoggerFactory.getLogger(AbstractPersistenceObjectDAO.class);

but as JEE applications are CDI enabled in some managed classes it was injected like

@Inject
private Logger logger;

is there any advantage if using logger with CDI, in terms of performance (time or memory)? Are there any downsides of any of these approaches? While @inject can only be used in managed environments, does it offer any advantage over other?

Prateek Jain
  • 149
  • 5
  • 1
    Possible duplicate of [Is micro-optimisation important when coding?](https://softwareengineering.stackexchange.com/questions/99445/is-micro-optimisation-important-when-coding) – gnat Feb 09 '18 at 12:26
  • @gnat this is not about micro-optimization. this is about the recommended way of doing certain things and looking for reasons behind any diversions. at last, there should be consistency in code :-) – Prateek Jain Feb 09 '18 at 13:39
  • Take a look [here](https://dzone.com/articles/logger-injection-with-springs-injectionpoint). Seems to me, the difference is DRY and getting centralised the Loggers initialization, so that you can programme their configuration (more convention over configuration). In terms of efficency I don't think CDI makes a big deal improvement better the performance, just the opposite. – Laiv Feb 09 '18 at 14:56
  • https://stackoverflow.com/questions/10345109/whats-the-overhead-of-creating-a-slf4j-loggers-in-static-vs-non-static-context – ℛɑƒæĿᴿᴹᴿ Feb 10 '18 at 00:10

1 Answers1

0

techtrainer,

  • Use injection sparingly and only when needed
  • In my opinion there's no point in injecting logger (I use static)
  • Both costs should be negligible unless many many objects are created

Reference:

What's the overhead of creating a SLF4J loggers in static vs. non-static contexts