SimpleFormatter in Java Logging API
Last Updated :
06 Jan, 2023
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.
Java
java.util.logging.ConsoleHandler.formatter
= java.util.logging.SimpleFormatter
java.util.logging.SimpleFormatter.format
= [ % 1 $tF % 1 $tT ][% 4 $ - 7s] % 5 $s
% n
java.util.logging.SimpleFormatter.format
= "%1$tc %2$s%n%4$s: %5$s%6$s%n"
|
Features of SimpleFormatter:
- Only via plain text messages, SimpleFormatter displays log messages
- From Java 7 onwards, SimpleFormatter 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.
Implementation:
Example 1: Sample mylogging.properties
Java
java.util.logging.FileHandler.pattern = %h/java%u.log
java.util.logging.FileHandler.limit = 50000
java.util.logging.FileHandler.count = 1
java.util.logging.FileHandler.formatter = java.util.logging.XMLFormatter
java.util.logging.ConsoleHandler.level = INFO
|
Example 2: Sample Java file that uses mylogging.properties which in turn displays the log messages in plain text
Java
import java.io.FileInputStream;
import java.io.IOException;
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;
public class SimpleFormatterLoggingExample {
static Logger loggerInformation = Logger.getLogger(
SimpleFormatterLoggingExample. class .getName());
public static void main(String[] args)
{
try {
LogManager.getLogManager().readConfiguration(
new FileInputStream(
"mylogging.properties" ));
}
catch (SecurityException | IOException e1) {
System.out.println(
"SecurityException message ,, "
+ e1.getMessage());
}
loggerInformation.setLevel(Level.FINE);
loggerInformation.addHandler( new ConsoleHandler());
loggerInformation.addHandler( new MyHandler());
try {
Handler fileHandler
= new FileHandler( "logger.log" , 2000 , 3 );
fileHandler.setFormatter( new MyFormatter());
fileHandler.setFilter( new MyFilter());
loggerInformation.addHandler(fileHandler);
for ( int i = 0 ; i < 5 ; i++) {
loggerInformation.log(
Level.INFO,
"Printing logger information " + i);
}
loggerInformation.log(Level.CONFIG,
"Config data is set" );
}
catch (SecurityException | IOException e) {
System.out.println(
"SecurityException message ,, "
+ e.getMessage());
}
}
}
|
As java.util.logging.SimpleFormatter.format=[%1$tF %1$tT] [%4$-7s] %5$s %n is set in “mylogging.properties” file,
Output:
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.
Example:
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.
Example:
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.
Example:
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
Java
import java.util.logging.Logger;
public class SimpleFormatterUsingSystemProperty {
private static Logger LOGGER = null ;
static
{
System.setProperty(
"java.util.logging.SimpleFormatter.format" ,
"[%1$tF %1$tT] [%4$-7s] %5$s %n" );
LOGGER = Logger.getLogger(
SimpleFormatterUsingSystemProperty
. class .getName());
}
public static void main(String[] args)
{
LOGGER.info(
"Inside main method of SimpleFormatterUsingSystemProperty" );
LOGGER.warning(
"a general warning message for the security" );
}
}
|
Output :
Programmatic way of setting format:
Java
import java.util.Date;
import java.util.logging.ConsoleHandler;
import java.util.logging.LogRecord;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;
public class SimpleFormatterUsingProgramaticway {
private static Logger LOGGERINFORMATION = null ;
static
{
Logger mainLogger
= Logger.getLogger( "com.journaldev" );
mainLogger.setUseParentHandlers( false );
ConsoleHandler handler = new ConsoleHandler();
handler.setFormatter( new SimpleFormatter() {
private static final String format
= "[%1$tF %1$tT] [%2$-7s] %3$s %n" ;
@Override
public synchronized String format(
LogRecord logRecord)
{
return String.format(
format, new Date(logRecord.getMillis()),
logRecord.getLevel().getLocalizedName(),
logRecord.getMessage());
}
});
mainLogger.addHandler(handler);
LOGGERINFORMATION = Logger.getLogger(
SimpleFormatterUsingProgramaticway
. class .getName());
}
public static void main(String[] args)
{
LOGGERINFORMATION.info(
"in SimpleFormatterUsingProgramaticway" );
LOGGERINFORMATION.warning( "a general warning" );
}
}
|
Output :
Conclusion:
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.
Share your thoughts in the comments
Please Login to comment...