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
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;
public class GFG {
public static void main(String[] args)
{
Locale englishLocale = Locale.getDefault();
try {
Locale.setDefault(Locale.ENGLISH);
GregorianCalendar calendar
= new GregorianCalendar();
int calendarYear = calendar.get(Calendar.YEAR);
LogRecord logRecordInformation = new LogRecord(
Level.INFO, "XMLFormatterTest" );
System.out.println(
"--------------------------------------" );
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());
System.out.println(
"--------------------------------------" );
XMLFormatter formatter = new XMLFormatter();
String xmlFormatted
= formatter.format(logRecordInformation);
System.out.println(xmlFormatted);
}
finally {
Locale.setDefault(englishLocale);
}
}
}
|
Output:
Example 2:
Java
import java.util.logging.Level;
import java.util.logging.LogRecord;
import java.util.logging.XMLFormatter;
public class GFG {
public static void main(String[] args) throws Exception
{
XMLFormatter xmlFormatter = new XMLFormatter();
LogRecord logRecord = new LogRecord(
Level.INFO,
"Logrecord message to be printed in xml file.." );
FileHandler fileHandler
= new FileHandler( "logrecordxml.xml" );
fileHandler.setFormatter(xmlFormatter);
fileHandler.publish(logRecord);
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.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...