Java Logging API 中的 XMLFormatter
在软件开发周期中,最好记录正在完成的操作集。记录操作称为记录。通过使用Java .util.logging包(默认)在Java日志记录的数据。另外我们还有Log4j、Logback、tinylog等第三方框架,根据需求不同,选择日志框架的偏好也不同。在Java, Java.util包具有日志记录实用程序,以下导入对于日志记录非常必要,如下所示:
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;
现在,让我们通过“属性文件”来查看日志消息显示的规范。在项目内部,我们可以指定一个属性文件,它们会跟踪维护日志记录功能。其中,“Java.util.logging.ConsoleHandler.formatter”指定了要使用的Formatter类的名称,默认设置为Java.util.logging.SimpleFormatter,它只是以纯文本形式显示日志条目。在这里,我们将使用LogRecord介绍 XMLFormatter,如下所示:
LogRecord logRecordInformation = new LogRecord(Level.INFO, “XMLFormatterTest”);
LogRecord 包含以下 getter 方法:
getLevel() –可以是信息/警告等,(消息的日志级别)
All - 1
FINEST - 2
FINER - 3
FINE - 4
CONFIG - 5
INFO - 6
WARNING - 7
SEVERE. - 8
OFF - 9
getMessage() –在这里它将显示我们给出的示例的 XMLFormatterTest
LogRecord logRecordInformation = new LogRecord(Level.INFO, "XMLFormatterTest");
getMillis() – 1616767447995
getSequenceNumber() – 0
我们还有其他方法,如下所示: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.
示例 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);
}
}
}
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();
}
}
输出:
示例 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();
}
}
输出:我们可以在 XML 文件中打印相同的内容,而不是在控制台中打印,如下所示
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.