Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do Log4j, commons-logging, JDK-Logging and SLF4J relate to each other?

Tags:

java

logging

Are they alternatives, dependencies, APIs or implementations of each other? And why do they exist?

like image 978
Zeemee Avatar asked Aug 17 '11 10:08

Zeemee


1 Answers

Ah, logging frameworks in Java. Your question mixes 2 different types of libraries:

  • log4j and JDK logging are libraries for handling logging
  • Commons Logging and SLF4J are logging facades: you still need a real logging implementation (like log4j)

If you are writing a library that will be used in someone else's system, then you should use a logging facade because you do not know which logging framework they will use. In this case use SLF4J (Commons Logging is older and has some classloader issues).

If you control the whole application and can dictate which logging framework to use, you are free to choose your own preference. My preferred solutions are (in order of preference):

  • Logback
  • log4j
  • JDK logging (in my opinion, a case of 'not invented here' by SUN)
like image 140
SteveD Avatar answered Oct 01 '22 00:10

SteveD