Java : XMLFormatter (logging) with Examples

XMLFormatter (Java SE 17 & JDK 17) API Examples.
You will find code examples on most XMLFormatter methods.


Summary

Format a LogRecord into a standard XML format.

Class diagram

final var logger = Logger.getLogger("com.example.logging");
logger.setUseParentHandlers(false);

final var pattern = Path.of("R:", "java-work", "log.txt");

// The FileHandler has the XMLFormatter by default.
final var handler = new FileHandler(pattern.toString());
System.out.println(handler.getFormatter()); // java.util.logging.XMLFormatter@2c83f1d1

logger.addHandler(handler);

logger.info("TEST LOG : 1");
logger.info("TEST LOG : 2");

System.out.print(Files.readString(pattern));

// Result
// ↓
//<?xml version="1.0" encoding="UTF-8" standalone="no"?>
//<!DOCTYPE log SYSTEM "logger.dtd">
//<log>
//<record>
//  <date>2022-03-18T07:23:03.832551900Z</date>
//  <millis>1647588183832</millis>
//  <nanos>551900</nanos>
//  <sequence>1</sequence>
//  <logger>com.example.logging</logger>
//  <level>INFO</level>
//  <class>com.example.logging.XMLFormatterTest$1</class>
//  <method>method</method>
//  <thread>1</thread>
//  <message>TEST LOG : 1</message>
//</record>
//<record>
//  <date>2022-03-18T07:23:03.838548100Z</date>
//  <millis>1647588183838</millis>
//  <nanos>548100</nanos>
//  <sequence>2</sequence>
//  <logger>com.example.logging</logger>
//  <level>INFO</level>
//  <class>com.example.logging.XMLFormatterTest$1</class>
//  <method>method</method>
//  <thread>1</thread>
//  <message>TEST LOG : 2</message>
//</record>

Constructors

XMLFormatter ()

Creates a new instance of XMLFormatter.

final var logger = Logger.getLogger("com.example.logging");
logger.setUseParentHandlers(false);

final var handler = new ConsoleHandler();
logger.addHandler(handler);

System.out.println(handler.getFormatter()); // java.util.logging.SimpleFormatter@84e9fc3

handler.setFormatter(new XMLFormatter());

System.out.println(handler.getFormatter()); // java.util.logging.XMLFormatter@63bfd328

logger.info("TEST LOG!");

// Result
// ↓
//<?xml version="1.0" encoding="UTF-8" standalone="no"?>
//<!DOCTYPE log SYSTEM "logger.dtd">
//<log>
//<record>
//  <date>2022-03-18T07:25:20.352589Z</date>
//  <millis>1647588320352</millis>
//  <nanos>589000</nanos>
//  <sequence>1</sequence>
//  <logger>com.example.logging</logger>
//  <level>INFO</level>
//  <class>com.example.logging.XMLFormatterTest$2</class>
//  <method>method</method>
//  <thread>1</thread>
//  <message>TEST LOG!</message>
//</record>

Methods

String format (LogRecord record)

Format the given message to XML.

class MyFormatter extends XMLFormatter {
    @Override
    public String format(LogRecord logRecord) {
        logRecord.setMessage(logRecord.getMessage() + " : override format!");
        return super.format(logRecord);
    }
}

final var logger = Logger.getLogger("com.example.logging");
logger.setUseParentHandlers(false);

final var handler = new ConsoleHandler();
logger.addHandler(handler);

handler.setFormatter(new MyFormatter());

logger.info("TEST LOG!");

// Result
// ↓
//<?xml version="1.0" encoding="UTF-8" standalone="no"?>
//<!DOCTYPE log SYSTEM "logger.dtd">
//<log>
//<record>
//  <date>2022-03-18T07:28:47.638092600Z</date>
//  <millis>1647588527638</millis>
//  <nanos>92600</nanos>
//  <sequence>1</sequence>
//  <logger>com.example.logging</logger>
//  <level>INFO</level>
//  <class>com.example.logging.XMLFormatterTest$3</class>
//  <method>method</method>
//  <thread>1</thread>
//  <message>TEST LOG! : override format!</message>
//</record>

String getHead (Handler h)

Return the header string for a set of XML formatted records.

class MyFormatter extends XMLFormatter {
    @Override
    public String getHead(Handler h) {
        return "<root>\n";
    }

    @Override
    public String getTail(Handler h) {
        return "</root>\n";
    }
}

final var logger = Logger.getLogger("com.example.logging");
logger.setUseParentHandlers(false);

final var pattern = Path.of("R:", "java-work", "log.txt");
final var handler = new FileHandler(pattern.toString());
logger.addHandler(handler);

handler.setFormatter(new MyFormatter());

logger.info("TEST LOG : 1");
logger.info("TEST LOG : 2");

logger.removeHandler(handler);
handler.close();

System.out.print(Files.readString(pattern));

// Result
// ↓
//<root>
//<record>
//  <date>2022-03-18T07:36:49.173484100Z</date>
//  <millis>1647589009173</millis>
//  <nanos>484100</nanos>
//  <sequence>1</sequence>
//  <logger>com.example.logging</logger>
//  <level>INFO</level>
//  <class>com.example.logging.XMLFormatterTest$4</class>
//  <method>method</method>
//  <thread>1</thread>
//  <message>TEST LOG : 1</message>
//</record>
//<record>
//  <date>2022-03-18T07:36:49.177482200Z</date>
//  <millis>1647589009177</millis>
//  <nanos>482200</nanos>
//  <sequence>2</sequence>
//  <logger>com.example.logging</logger>
//  <level>INFO</level>
//  <class>com.example.logging.XMLFormatterTest$4</class>
//  <method>method</method>
//  <thread>1</thread>
//  <message>TEST LOG : 2</message>
//</record>
//</root>

String getTail (Handler h)

Return the tail string for a set of XML formatted records.

Please see getHead(Handler h).

Methods declared in Formatter

formatMessage

Please see the link below.


Related posts

To top of page