SimpleFormatter in Java Logging API
Logging is much useful for any software application and that helps to find out the execution of the project at multiple scenarios. In Java, java.util package is having the logging utility and the following imports are much necessary for logging.
import java.util.logging.ConsoleHandler; import java.util.logging.FileHandler; import java.util.logging.Handler; import java.util.logging.Level; import java.util.logging.LogManager; import java.util.logging.Logger;
Specification of log message display via properties file:
In a project, we can specify a properties file, and they keep track of maintaining logging functionality. Among that, “java.util.logging.ConsoleHandler.formatter” specifies the name of a Formatter class to use and the default setting is java.util.logging.SimpleFormatter, which is nothing but displaying log entries in the plain text.
Features of SimpleFormatter:
- Only via plain text messages, SimpleFormatter displays log messages
- From Java 7 onwards, SimpleFormatters can be configured using a system property.
- As it is a plain text display, anybody can understand it easily. Basic information is displayed.
- ConsoleHandler uses this formatter class and in console, it prints log messages.
Example 1: Sample mylogging.properties
Example 2: Sample Java file that uses mylogging.properties which in turn displays the log messages in plain text
As java.util.logging.SimpleFormatter.format=[%1$tF %1$tT] [%4$-7s] %5$s %n is set in “mylogging.properties” file,
At the same time, java.util.logging.SimpleFormatter.format=”%1$tc %2$s%n%4$s: %5$s%6$s%n”
Output can be:
Other example formats :
1. java.util.logging.SimpleFormatter.format=”%4$s: %5$s [%1$tc]%n”. Displays a line with the log level (4$), the log message (5$) and the timestamp (1$) in a square bracket.
WARNING: A general warning message in terms of security [Thu Jan 28 13:11:31 IST 2021]
2. java.util.logging.SimpleFormatter.format=”%1$tc %2$s%n%4$s: %5$s%6$s%n”. Displays 2 lines and in that first line includes the timestamp (1$) and the source (2$), whereas the second line includes the log level (4$) and the log message (5$) followed with the throwable and its backtrace (6$), in case if they are present.
Thu Jan 28 13:11:31 IST 2021 <your class name> fatal SEVERE: Exception raised and hence several messages are thrown with an exception java.lang.IllegalArgumentException: invalid argument
3. java.util.logging.SimpleFormatter.format=”%1$tb %1$td, %1$tY %1$tl:%1$tM:%1$tS %1$Tp %2$s%n%4$s: %5$s%n”. Displays 2 lines similar to the example as seen above but different date/time formatting is displayed. One important difference is it does not display the throwable and its backtrace.
Thu Jan 28 13:11:31 IST 2021 <your class name> fatal SEVERE: Exception raised and hence several messages are thrown with an exception
By overriding in a subclass. The method can be overridden. Via Formatter.formatMessage(java.util.logging.LogRecord), one can localize and format the message field.
Using system property also, we can do the formatting
Programmatic way of setting format:
SimpleFormatter is an easier way to understand log messages in human-readable ways with a timestamp, level of log messages like INFO, WARNING, etc., and the log messages. Exception-raised messages also displayed which is easier to track the entire flow of the project. Another format namely XMLFormatter is also available, but it displays the messages in the XML way. So, SimpleFormatter is much useful even to novice users and for common users too.
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.