XMLFormatter in Java Logging API
Last Updated :
15 Sep, 2021
In the software development cycle, always it is good to record the set of actions that are getting done. Recording the actions are called Logging. Logging in Java by using java.util.logging package(default) logs the data. Additionally, we have third-party frameworks likeLog4j, Logback, and tinylog, etc., Depends upon the requirements, preferences of selecting logging framework differ. In Java, java.util package is having the logging utility and the following imports are much necessary for logging are as follows:
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;
Now, let us go through the specification of log message display via a 'properties file'. Here inside 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. Here we are going to cover about XMLFormatter using LogRecord which is shown below as:
LogRecord logRecordInformation = new LogRecord(Level.INFO, "XMLFormatterTest");
The LogRecord contains the following getter methods:
getLevel() - It can be Info/Warning etc., (Log level of the message)
All - 1
FINEST - 2
FINER - 3
FINE - 4
CONFIG - 5
INFO - 6
WARNING - 7
SEVERE. - 8
OFF - 9
getMessage() - Here it will display XMLFormatterTest for the example as we have given as
LogRecord logRecordInformation = new LogRecord(Level.INFO, "XMLFormatterTest");
getMillis() - 1616767447995
getSequenceNumber() - 0
We have additional methods that are as follows:
Method | Action Performed |
---|
getLoggerName() | It returns the name of the Logger. |
getParameters() | It returns the parameters to be inserted into the message of this LogRecord. |
getResourceBundle() | Displays the info If any used to localize the message of this LogRecord or else returns null. |
getResourceBundleName() | It displays the name of the ResourceBundle (if any) used to localize the message of this LogRecord or else returns null. |
getSequenceNumber() | It displays a sequence number |
getSourceClassName() | It displays the class name of the class logging the message represented by this LogRecord. |
Example 1:
Java
// Java Program demonstrating XML Formatter Logging API
// Importing required libraries
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Locale;
import java.util.logging.Level;
import java.util.logging.LogRecord;
import java.util.logging.XMLFormatter;
// Main class
// XMLFormatterExample
public class GFG {
// Main driver method
public static void main(String[] args)
{
// A Locale object represents a specific
// geographical, political, or cultural region Let
// us take Locale.ENGLISH as the default in this
// program
Locale englishLocale = Locale.getDefault();
// Try block to check for exceptions
try {
Locale.setDefault(Locale.ENGLISH);
// Creating new object of GregorianCalendar
// class
GregorianCalendar calendar
= new GregorianCalendar();
int calendarYear = calendar.get(Calendar.YEAR);
// There are different levels of setting
// loggerInformation default level is INFO.
// If the value is not specified, for our
// example we have kept as INFO
// other available levels are
// ALL - 1,
// FINEST - 2,
// FINER - 3,
// FINE - 4,
// CONFIG - 5,
// INFO - 6,
// WARNING - 7,
// SEVERE - 8,
// OFF - 9
LogRecord logRecordInformation = new LogRecord(
Level.INFO, "XMLFormatterTest");
// Display message for better readability
System.out.println(
"--------------------------------------");
// Printing logger levels, message,
// getMillis,getSequenceNumber
System.out.println(
"Logger level.."
+ logRecordInformation.getLevel());
System.out.println(
"Message.."
+ logRecordInformation.getMessage());
System.out.println(
"getMillis.."
+ logRecordInformation.getMillis());
System.out.println(
"getSequenceNumber.."
+ logRecordInformation.getSequenceNumber());
// Display message for better readability
System.out.println(
"--------------------------------------");
// As we are using XMLFormatter, it displays the
// output in XML format.
// It has higher visibility if used with UTF-8
// Now creating object of XMLFormatter class
XMLFormatter formatter = new XMLFormatter();
String xmlFormatted
= formatter.format(logRecordInformation);
System.out.println(xmlFormatted);
}
// Executing the above code no matter
// if there is exception or not
finally {
Locale.setDefault(englishLocale);
}
}
}
Output:
Example 2:
Java
// Java Program demonstrating XML Formatter Logging API
// Importing required libraries
import java.util.logging.Level;
import java.util.logging.LogRecord;
import java.util.logging.XMLFormatter;
// Main class
// XMLFormatterExample1
public class GFG {
// Main driver method
public static void main(String[] args) throws Exception
{
// Creating an object of XMLFormatter class
XMLFormatter xmlFormatter = new XMLFormatter();
// Setting level to Info
LogRecord logRecord = new LogRecord(
Level.INFO,
"Logrecord message to be printed in xml file..");
// We can see the output of LogRecord in
// logrecordxml.xml file
FileHandler fileHandler
= new FileHandler("logrecordxml.xml");
fileHandler.setFormatter(xmlFormatter);
// Prepared data is displayed in the
// logrecordxml.xml file
fileHandler.publish(logRecord);
// Lastly releasing out all the records
// using the flush() method
fileHandler.flush();
}
}
Output: Instead of printing in the console, we can print the same in the XML file as shown below

Conclusion:
XML notation is very helpful to read and understand log messages. Hence, XMLFormatter provides an efficient way to achieve the same. Many prefer to have XMLFormatter instead of SimpleFormatter which produces the output in the plain text format.
Similar Reads
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.u
6 min read
Logger log() Method in Java with Examples
The log() method of Logger is used to Log a message. If the logger is currently enabled for the given message level which is passed as parameter then a corresponding LogRecord is created and forwarded to all the registered Output Handler objects. But in logger class, there are seven different log()
6 min read
Disable SLF4J Logging in Apache Kafka
SLF4J means that Simple Logging Facade for Java serves as a logging facade, allowing applications to use various logging frameworks such as Log4j, and Logback without being tied to a specific implementation Kafka, being a distributed streaming platform, also on SLF4J for its logging and Log4j has th
2 min read
Logger setFilter() method in Java with Examples
setFilter() method of a Logger class is used to set a filter to control output on this Logger. The filter is passed as a parameter. A Filter is useful to filter out log messages. It can be said that the filter decides the message gets to be logged or not. Filters are represented by the Java interfac
2 min read
LogManager reset() method in Java with Examples
The reset() method of java.util.logging.LogManager is used to reset the logging configuration. This method throws SecurityException if the exception condition occurs, as given below Syntax: public void reset() throws SecurityException Parameters: This method does not accepts any parameter. Return Va
1 min read
Logger removeHandler() method in Java with Examples
removeHandler() method of a Logger class is used to remove a log Handler from Logger. A Handler is a component of JVM that takes care of actual logging to the defined output writers like a file, console out etc. It returns silently if the given Handler is not found or is null.Syntax: public void rem
2 min read
Logger fine() method in Java with Examples
The fine() method of a Logger class used to Log a FINE message. This method is used to pass FINE types logs to all the registered output Handler objects. FINE, FINER and FINEST provide tracking information as when what is happening/ has happened in our application. FINE displays the most important m
3 min read
Apache XMLBeans using Java
XMLBeans is a tool developed by Apache Software Foundation that allows users to access and manipulate XML data using the Java programming language. The tool provides an easy-to-use, object-oriented approach to working with XML, making it a popular choice among Java developers. XMLBeans uses the XML
3 min read
Logger finer() method in Java with Examples
The finer() method of a Logger class used to Log an FINER message.This method is used to pass FINER types logs to all the registered output Handler objects. FINER message: FINER outputs a detailed tracing message and may include logging calls regarding method entering, exiting, throwing exceptions.
2 min read
Logger setParent() method in Java with Examples
setParent() method of a Logger class used to set the parent Logger of this current Logger.The parent Logger we want to set is passed as a parameter. LogManager use this method to update a Logger when the namespace changes. Syntax: public void setParent(Logger parent) Parameters: This method accepts
2 min read