Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to customize the log Formatter of the JDK logging framework

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces the JDK log framework how to customize the log Formatter, the article introduces in great detail, has a certain reference value, interested friends must read it!

JDK logs can be formatted as text, or they can be formatted as standard formats such as XML or Html. It depends on the implementation of the Formatter class. The Formatter abstract class provides format member functions for extension. A typical custom Formatter class implementation is shown in listing 3:

Listing 3 LogRecord class definition

Public class MyFormatter extends Formatter {private final String lineSeparator = System.getProperty ("line.separator"); @ Override public String format (LogRecord record) {StringBuffer sb = new StringBuffer (); String message = formatMessage (record); sb.append (record.getLevel (). GetLocalizedName ()); sb.append (message); sb.append (lineSeparator); if (record.getThrown ()! = null) {try {StringWriter sw = new StringWriter (); PrintWriter pw = new PrintWriter (sw) Record.getThrown (). PrintStackTrace (pw); pw.close (); sb.append (sw.toString ());} catch (Exception ex) {}} return sb.toString ();}}

The formatMessage method provides the default way to localize and format log records. It can also support java.text-style text formatting by calling the setMessage method of the Logger object to set the java.text-style format string and setting parameters through the setParameters method, so that formatMessage will format the log message according to the set java.text-style format string. In summary, the formatMessage method makes it easy for subclasses to format strings. So that subclasses only need to define the format of the output text without considering localization and other issues.

Custom log message level

The JDK logging framework provides several logging levels such as SEVERE,WARNING,INFO,CONFIG,FINE,FINER,FINEST by default. If we need to define more log levels, we just need to inherit the java.util.logging.Level class and declare the custom level as a static member variable. A typical custom message class is shown in listing 4.

Listing 4 Custom Level class

Public class MyLevel extends Level {protected MyLevel (String name, int value) {super (name, value);} public static final Level Level1 = new MyLevel ("Level1", 123);. / / other custom levels}

The weight value value is an integer. In the default JDK log level, the weight of SEVERE is 1000 and finest is 300, and the weight of each custom level can be defined according to specific requirements. For example, add a new level to the WARNING and INFO levels, which must have a weight between 800,900.

Free log configuration

Like other logging frameworks, the JDK logging framework also provides powerful logging configuration capabilities. You can not only configure dynamically through the code, but also through the configuration file to achieve free and flexible configuration. Through the dynamic configuration of the code, the application can change the configuration of the log class during the running process and change the different configuration combinations dynamically. A simple dynamic configuration code is shown in listing 5.

Listing 5 dynamically configuring the Logger object

Public static void main (String [] args) {Handler fh = new FileHandler ("% t/wombat.log"); Logger.getLogger ("logname") .addHandler (fh); Logger.getLogger ("com.wombat"). SetLevel ("com.wombat", Level.FINEST);...}

The configuration method of the configuration file is equally flexible. It mainly sets the log object according to a specified configuration file when the application starts. In the configuration file, the log object is identified by its name. A typical log configuration file is shown in listing 6.

Listing 6 jdk Logger configuration file

# set the Handler of the log object, whose name is com.xyz.fOO

Com.xyz.fOO.handlers= java.util.logging.FileHandler, java.util.logging.ConsoleHandler

# set the basic output level of the log object

Com.xyz.fOO.level = INFO

# FileHandler only allows output of JDK logs above SEVERE level

Java.util.logging.ConsoleHandler.level = SEVERE

# ConsoleHandler allows output of JDK logs above INFO level

Java.util.logging.ConsoleHandler.level = INFO

After setting up a log configuration file, we can define the configuration file path by adding the-Djava.util.logging.config.file parameter in the startup parameters of the Java program. A typical Java command line is as follows:

Java-Djava.util.logging.config.file=logger.properties-cp. Mainclass

This is the end of the custom logging Formatter of the JDK logging framework. To use these components, you only need to be able to find them in classpath.

The above is all the content of the article "how to customize the log formatter of the JDK log framework". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report