Class XMLFormatter
public class XMLFormatter extends Formatter
The DTD specification is provided as Appendix A to the Java Logging APIs specification.
The XMLFormatter can be used with arbitrary character encodings, but it is recommended that it normally be used with UTF-8. The character encoding can be set on the output Handler.
- Implementation Requirements:
- Since JDK 9, instances of LogRecord contain an
Instantwhich can have nanoseconds below the millisecond resolution. The DTD specification has been updated to allow for an optional<nanos>element. By default, the XMLFormatter will compute the nanosecond adjustment below the millisecond resolution (usingLogRecord.getInstant().getNano() % 1000_000) - and if this is not 0, this adjustment value will be printed in the new<nanos>element. The event instant can then be reconstructed usingInstant.ofEpochSecond(millis/1000L, (millis % 1000L) * 1000_000L + nanos)wheremillisandnanosrepresent the numbers serialized in the<millis>and<nanos>elements, respectively.
The<date>element will now contain the whole instant as formatted by theDateTimeFormatter.ISO_INSTANTformatter.For compatibility with old parsers, XMLFormatters can be configured to revert to the old format by specifying a
<xml-formatter-fully-qualified-class-name>.useInstant = falseproperty in the logging configuration. WhenuseInstantisfalse, the old formatting will be preserved. WhenuseInstantistrue(the default), the<nanos>element will be printed and the<date>element will contain the formatted instant.For instance, in order to configure plain instances of XMLFormatter to omit the new
<nano>element,java.util.logging.XMLFormatter.useInstant = falsecan be specified in the logging configuration. - Since:
- 1.4
Constructor Summary
| Constructor | Description |
|---|---|
XMLFormatter() |
Creates a new instance of XMLFormatter. |
Method Summary
Methods declared in class Formatter
formatMessage | Modifier and Type | Method | Description |
|---|---|---|
String |
formatMessage |
Localize and format the message string from a log record. |
Methods declared in class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait | Modifier and Type | Method | Description |
|---|---|---|
protected Object |
clone() |
Creates and returns a copy of this object. |
boolean |
equals |
Indicates whether some other object is "equal to" this one. |
protected void |
finalize() |
Deprecated, for removal: This API element is subject to removal in a future version. Finalization is deprecated and subject to removal in a future release. |
final Class |
getClass() |
Returns the runtime class of this Object. |
int |
hashCode() |
Returns a hash code value for this object. |
final void |
notify() |
Wakes up a single thread that is waiting on this object's monitor. |
final void |
notifyAll() |
Wakes up all threads that are waiting on this object's monitor. |
String |
toString() |
Returns a string representation of the object. |
final void |
wait() |
Causes the current thread to wait until it is awakened, typically by being notified or interrupted. |
final void |
wait |
Causes the current thread to wait until it is awakened, typically by being notified or interrupted, or until a certain amount of real time has elapsed. |
final void |
wait |
Causes the current thread to wait until it is awakened, typically by being notified or interrupted, or until a certain amount of real time has elapsed. |
Constructor Details
XMLFormatter
public XMLFormatter()
- Implementation Requirements:
- Since JDK 9, the XMLFormatter will print out the record event time as an Instant. This instant has the best resolution available on the system. The
<date>element will contain the instant as formatted by theDateTimeFormatter.ISO_INSTANT. In addition, an optional<nanos>element containing a nanosecond adjustment will be printed if the instant contains some nanoseconds below the millisecond resolution.This new behavior can be turned off, and the old formatting restored, by specifying a property in the logging configuration. If
LogManager.getLogManager().getProperty( this.getClass().getName()+".useInstant")is"false"or"0", the old formatting will be restored.
Method Details
format
public String format(LogRecord record)
This method can be overridden in a subclass. It is recommended to use the Formatter.formatMessage(LogRecord) convenience method to localize and format the message field.
getHead
getTail
© 1993, 2025, Oracle and/or its affiliates. All rights reserved.
Documentation extracted from Debian's OpenJDK Development Kit package.
Licensed under the GNU General Public License, version 2, with the Classpath Exception.
Various third party code in OpenJDK is licensed under different licenses (see Debian package).
Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.