Introduction To Loggers

Loggers vs System.out.println
Loggers has mulitple levels for logging. If we are writing a real short program, just for learning purposes System.out.println is fine but when we are developing a quality software project, we should use professional logger and SOPs should be avoided.
  • A professional loggers like log4j, Logback, Jakarta Commons-Logging (JCL),java.util.logging etc provides different levels for logging and flexibility. We can get the log message accordingly. For example, group X messages should be printed only on PRODUCTION, group Y messages should be printed on ERROR, etc.
  • Loggers are Configurable as in just one parameter change we can switch off all the logging statements.
  • With loggers maintenance is easy, imagine if we have thousands of System.out.println statements all through the application, it would be difficult to maintain the program over a period.
  • In an application, every single class can have a different logger and controlled accordingly.
  • We have limited option for redirecting the messages in System.out, but in case of a logger you have appenders which provides numbers of options. We can even create a custom output option and redirect it to that.
Ways Of Logging In Java
There are many java Api that are used for logging in java, some of the popular loggers are :
  • Log4J
  • Logback
  • Jakarta Commons-Logging(JCL)
  • java.util.logging(JUL)
  • SLF4J
  • Perf4J

Difference Between Loggers

Log4j - Log4J is an Open Source logging framework from the apache foundation. It's a Java based framework and can be used in any java program. It was originally developed by Ceki Gülcü and now its part of Apache Software project. Log4j is a Reliable, Fast and Flexible Logging Framework which is highly configurable through external configuration files at runtime.

Logback - Logback is intended as a successor to the popular log4j project. It was designed by Ceki Gülcü, log4j's founder. It builds upon a decade of experience gained in designing industrial-strength logging systems. The resulting product, i.e. logback, is faster and has a smaller footprint than all existing logging systems, sometimes by a wide margin. Just as importantly, logback offers unique and rather useful features missing in other logging systems.

Jakarta Commons Logging(JCL) - The Jakarta Commons Logging (JCL) provides a Log interface that is intended to be both light-weight and independent of numerous logging toolkits. There's a good thing about JCL that you won't need to do any configuration.

java.util.logging(JUL) - The JDK contains the Java Logging API. with logger we can save text to a central place to report on errors, provide additional information about our program, etc. JCL logging API allows to configure how messages are written by which class with priority.

SLF4J - SLF4J(Simple Logging Facade) for java is an abstraction of different logging frameworks (eg. log4j, java.util.logging, commons logging, logback etc.). SLF4J provide the end-user to plug in the desired logging framework at deployment time i.e we can choose the particular logging framework at application runtime.

Perf4J - Perf4J is a set of utilities for calculating and displaying performance statistics for Java code. It is useful framework for those who are familier with logging framework like Log4j, Logback etc.