< prev index next >

jdk/src/java.logging/share/classes/java/util/logging/XMLFormatter.java

Print this page




  24  */
  25 
  26 
  27 package java.util.logging;
  28 
  29 import java.nio.charset.Charset;
  30 import java.time.Instant;
  31 import java.time.format.DateTimeFormatter;
  32 import java.util.*;
  33 
  34 /**
  35  * Format a LogRecord into a standard XML format.
  36  * <p>
  37  * The DTD specification is provided as Appendix A to the
  38  * Java Logging APIs specification.
  39  * <p>
  40  * The XMLFormatter can be used with arbitrary character encodings,
  41  * but it is recommended that it normally be used with UTF-8.  The
  42  * character encoding can be set on the output Handler.
  43  *
  44  * @implSpec Since JDK 1.9, instances of {@linkplain LogRecord} contain
  45  * an {@link LogRecord#getInstant() Instant} which can have nanoseconds below
  46  * the millisecond resolution.
  47  * The DTD specification has been updated to allow for an optional
  48  * {@code <nanos>} element. By default, the XMLFormatter will compute the
  49  * nanosecond adjustment below the millisecond resolution (using
  50  * {@code LogRecord.getInstant().getNano() % 1000_000}) - and if this is not 0,
  51  * this adjustment value will be printed in the new {@code <nanos>} element.
  52  * The event instant can then be reconstructed using
  53  * {@code Instant.ofEpochSecond(millis/1000L, (millis % 1000L) * 1000_000L + nanos)}
  54  * where {@code millis} and {@code nanos} represent the numbers serialized in
  55  * the {@code <millis>} and {@code <nanos>} elements, respectively.
  56  * <br>
  57  * The {@code <date>} element will now contain the whole instant as formatted
  58  * by the {@link DateTimeFormatter#ISO_INSTANT DateTimeFormatter.ISO_INSTANT}
  59  * formatter.
  60  * <p>
  61  * For compatibility with old parsers, XMLFormatters can
  62  * be configured to revert to the old format by specifying a
  63  * {@code <xml-formatter-fully-qualified-class-name>.useInstant = false}
  64  * {@linkplain LogManager#getProperty(java.lang.String) property} in the


  67  * (the default), the {@code <nanos>} element will be printed and the
  68  * {@code <date>} element will contain the {@linkplain
  69  * DateTimeFormatter#ISO_INSTANT formatted} instant.
  70  * <p>
  71  * For instance, in order to configure plain instances of XMLFormatter to omit
  72  * the new {@code <nano>} element,
  73  * {@code java.util.logging.XMLFormatter.useInstant = false} can be specified
  74  * in the logging configuration.
  75  *
  76  * @since 1.4
  77  */
  78 
  79 public class XMLFormatter extends Formatter {
  80     private final LogManager manager = LogManager.getLogManager();
  81     private final boolean useInstant;
  82 
  83     /**
  84      * Creates a new instance of XMLFormatter.
  85      *
  86      * @implSpec
  87      *    Since JDK 1.9, the XMLFormatter will print out the record {@linkplain
  88      *    LogRecord#getInstant() event time} as an Instant. This instant
  89      *    has the best resolution available on the system. The {@code <date>}
  90      *    element will contain the instant as formatted by the {@link
  91      *    DateTimeFormatter#ISO_INSTANT}.
  92      *    In addition, an optional {@code <nanos>} element containing a
  93      *    nanosecond adjustment will be printed if the instant contains some
  94      *    nanoseconds below the millisecond resolution.
  95      *    <p>
  96      *    This new behavior can be turned off, and the old formatting restored,
  97      *    by specifying a property in the {@linkplain
  98      *    LogManager#getProperty(java.lang.String) logging configuration}.
  99      *    If {@code LogManager.getLogManager().getProperty(
 100      *    this.getClass().getName()+".useInstant")} is {@code "false"} or
 101      *    {@code "0"}, the old formatting will be restored.
 102      */
 103     public XMLFormatter() {
 104         useInstant = (manager == null)
 105             || manager.getBooleanProperty(
 106                     this.getClass().getName()+".useInstant", true);
 107     }




  24  */
  25 
  26 
  27 package java.util.logging;
  28 
  29 import java.nio.charset.Charset;
  30 import java.time.Instant;
  31 import java.time.format.DateTimeFormatter;
  32 import java.util.*;
  33 
  34 /**
  35  * Format a LogRecord into a standard XML format.
  36  * <p>
  37  * The DTD specification is provided as Appendix A to the
  38  * Java Logging APIs specification.
  39  * <p>
  40  * The XMLFormatter can be used with arbitrary character encodings,
  41  * but it is recommended that it normally be used with UTF-8.  The
  42  * character encoding can be set on the output Handler.
  43  *
  44  * @implSpec Since JDK 9, instances of {@linkplain LogRecord} contain
  45  * an {@link LogRecord#getInstant() Instant} which can have nanoseconds below
  46  * the millisecond resolution.
  47  * The DTD specification has been updated to allow for an optional
  48  * {@code <nanos>} element. By default, the XMLFormatter will compute the
  49  * nanosecond adjustment below the millisecond resolution (using
  50  * {@code LogRecord.getInstant().getNano() % 1000_000}) - and if this is not 0,
  51  * this adjustment value will be printed in the new {@code <nanos>} element.
  52  * The event instant can then be reconstructed using
  53  * {@code Instant.ofEpochSecond(millis/1000L, (millis % 1000L) * 1000_000L + nanos)}
  54  * where {@code millis} and {@code nanos} represent the numbers serialized in
  55  * the {@code <millis>} and {@code <nanos>} elements, respectively.
  56  * <br>
  57  * The {@code <date>} element will now contain the whole instant as formatted
  58  * by the {@link DateTimeFormatter#ISO_INSTANT DateTimeFormatter.ISO_INSTANT}
  59  * formatter.
  60  * <p>
  61  * For compatibility with old parsers, XMLFormatters can
  62  * be configured to revert to the old format by specifying a
  63  * {@code <xml-formatter-fully-qualified-class-name>.useInstant = false}
  64  * {@linkplain LogManager#getProperty(java.lang.String) property} in the


  67  * (the default), the {@code <nanos>} element will be printed and the
  68  * {@code <date>} element will contain the {@linkplain
  69  * DateTimeFormatter#ISO_INSTANT formatted} instant.
  70  * <p>
  71  * For instance, in order to configure plain instances of XMLFormatter to omit
  72  * the new {@code <nano>} element,
  73  * {@code java.util.logging.XMLFormatter.useInstant = false} can be specified
  74  * in the logging configuration.
  75  *
  76  * @since 1.4
  77  */
  78 
  79 public class XMLFormatter extends Formatter {
  80     private final LogManager manager = LogManager.getLogManager();
  81     private final boolean useInstant;
  82 
  83     /**
  84      * Creates a new instance of XMLFormatter.
  85      *
  86      * @implSpec
  87      *    Since JDK 9, the XMLFormatter will print out the record {@linkplain
  88      *    LogRecord#getInstant() event time} as an Instant. This instant
  89      *    has the best resolution available on the system. The {@code <date>}
  90      *    element will contain the instant as formatted by the {@link
  91      *    DateTimeFormatter#ISO_INSTANT}.
  92      *    In addition, an optional {@code <nanos>} element containing a
  93      *    nanosecond adjustment will be printed if the instant contains some
  94      *    nanoseconds below the millisecond resolution.
  95      *    <p>
  96      *    This new behavior can be turned off, and the old formatting restored,
  97      *    by specifying a property in the {@linkplain
  98      *    LogManager#getProperty(java.lang.String) logging configuration}.
  99      *    If {@code LogManager.getLogManager().getProperty(
 100      *    this.getClass().getName()+".useInstant")} is {@code "false"} or
 101      *    {@code "0"}, the old formatting will be restored.
 102      */
 103     public XMLFormatter() {
 104         useInstant = (manager == null)
 105             || manager.getBooleanProperty(
 106                     this.getClass().getName()+".useInstant", true);
 107     }


< prev index next >