Introduction
Technicalities
- Commons-logging
- Jboss-logging
- Log4j
- Log4j2
- Slf4j
The basic question that always haunts us is why to use one instead of "System.out.println". Can't we customize the "println" statement available in Java? Of course, we can customize it as needed, but again does it serve the purposes of our logging? I provide below the following important reasons for which we use a logger in our application.
- Easy configuration of Logger statements
- Logging messages using various formats like text, HTML, XML and so on
- Various types of logging persistence
- Asynchronous logger
- Categorization of logging the messages
- Backup of the log message and sending to various channels
In most cases, it is observed that a novice developer always prefers to write "System.out.println()" for debugging purposes. But that creates substantial overhead when the application is moved into production. It is also observed that most of the developers are happy if their log messages appear only in the console. It is always important to maintain all log messages in the file system for further analysis. Basically it provides you comprehensive diagnostic information about the various operations performed in the application. In the case of post-production issues, the log file from the application helps to a greater extent to fix the issues. However, let us learn a few things about the Java logging API. It is always recommended to use the logger instead of "System.out.println" even if you do not have external logging APIs.
The JDK logger comes with the package java.util. Let us have a glance at the following significant classes and interfaces available as a part of logger implementation in Java.
- java.util.logging.Logger
- java.util.logging.Level
- java.util.logging.LogManager
- java.util.logging.Filter
java.util.logging.Logger
This class is the actual implementation of a logger framework in Java. It provides various convenient methods to log the messages. It also provides methods to plug in the handler implementation. Handlers log messages, either in the file system or in the pre-defined console. The Logger class also provides various message levels. Message levels allow prioritization of the messages to the log. The levels can be a warning, error or simple information.
java.util.logging.Level
- SEVERE (highest value)
- WARNING
- INFO
- CONFIG
- FINE
- FINER
- FINEST (lowest value)
java.util.logging.LogManager
Java also supports logger configuration through a properties file and a LogManager class that helps to configure the config file and the one-time configuration and subsequent changes have no impact after LogManger initialization.
java.util.logging.Filter
- package com.ddlab.logger.custom.filter;
- import java.util.logging.Filter;
- import java.util.logging.LogRecord;
- /**
- * This class is used as a log filter. You need to provide your own implementation
- * for custom logging message. In this class, I have provided implementation to skip
- * the log messages coming from a specific library.
- * @author <a href="mailto:[email protected]">Debadatta Mishra</a>
- * @since 2013
- */
- public class LogFilter implements Filter {
- /**
- * This method is used to provide the custom implementation for the logging
- * messages. In this case the implementation prevents to log the messages coming
- * from a third-party.
- * @param record
- * @return true or false
- */
- @Override
- public boolean isLoggable(LogRecord record) {
- if(record.getSourceClassName().indexOf("org.some.third") != -1)
- return false;
- else
- return true;
- }
- }

Comments
Join the conversation! Your thoughts help the community grow.