Java logging:formatters

Source: Internet
Author: User

The in the Handler‘s Java Logging API use a to java.util.logging.Formatter format the LogRecord ' s before writing it to an external system.

Java comes with built-in Formatter ' s (subclasses of Formatter ):

    1. Simpleformatter
    2. Xmlformatter

The various Handler ' s in the Java Logging API is use either of these Formatter , and the default, but can also set your own cust Om Formatter subclass on a Handler .

You can create your own by Formatter subclassing the java.util.logging.Formatter class. Here are a simple example:

public class MyFormatter extends Formatter {    @Override public    String format (LogRecord record) {        return Record.getlevel () + ":" + Record.getmessage ();}    }

The subclass must override the abstract format() method in the Formatter class. The String returned by the are forwarded to the format() External system by the Handler . Exactly how the string should are formatted are up to you.

The Formatter class also contains the convenience method formatMessage() which can be used to format the message using the ResourceBundle LogRecord. Here are an example:

public class MyFormatter extends Formatter {    @Override public    String format (LogRecord record) {        return FormatMessage (record);}    }

You won't often need to implement your own Formatter , but once in a while, if a specific log format is required, a custom c1/> may useful.

Java logging:formatters

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.