Class SimpleFormatter
LogRecord in a human readable
format. The summary will typically be 1 or 2 lines.
Configuration:
The SimpleFormatter is initialized with the format string
specified in the java.util.logging.SimpleFormatter.format
property to format the log messages.
This property can be defined
in the logging properties
configuration file
or as a system property. If this property is set in both
the logging properties and system properties,
the format string specified in the system property will be used.
If this property is not defined or the given format string
is illegal,
the default format is implementation-specific.
- Since:
- 1.4
- See Also:
-
Constructor Summary
Constructors -
Method Summary
Methods declared in class Formatter
formatMessage, getHead, getTailMethods declared in class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitModifier and TypeMethodDescriptionprotected Objectclone()Answers a new instance of the same class as the receiver, whose slots have been filled in with the values in the slots of the receiver.booleanCompares the argument to the receiver, and answers true if they represent the same object using a class specific comparison.protected voidfinalize()Deprecated, for removal: This API element is subject to removal in a future version.May cause performance issues, deadlocks and hangs.getClass()Answers the unique instance of java.lang.Class which represents the class of the receiver.inthashCode()Answers an integer hash code for the receiver.final voidnotify()Causes one thread which iswaiting on the receiver to be made ready to run.final voidCauses all threads which arewaiting on the receiver to be made ready to run.toString()Answers a string containing a concise, human-readable description of the receiver.final voidwait()Causes the thread which sent this message to be made not ready to run pending some change in the receiver (as indicated bynotifyornotifyAll).final voidwait(long time) Causes the thread which sent this message to be made not ready to run either pending some change in the receiver (as indicated bynotifyornotifyAll) or the expiration of the timeout.final voidwait(long time, int frac) Causes the thread which sent this message to be made not ready to run either pending some change in the receiver (as indicated bynotifyornotifyAll) or the expiration of the timeout.
-
Constructor Details
-
SimpleFormatter
public SimpleFormatter()Create aSimpleFormatter.
-
-
Method Details
-
format
Format the given LogRecord.The formatting can be customized by specifying the format string in the
java.util.logging.SimpleFormatter.formatproperty. The givenLogRecordwill be formatted as if by calling:
where the arguments are:String.format(format, date, source, logger, level, message, thrown);
format- thejava.util.Formatterformat string specified in thejava.util.logging.SimpleFormatter.formatproperty or the default format.date- aZonedDateTimeobject representing event time of the log record in theZoneId.systemDefault()system time zone.source- a string representing the caller, if available; otherwise, the logger's name.logger- the logger's name.level- the log level.message- the formatted log message returned from theFormatter.formatMessage(LogRecord)method. It usesjava.textformatting and does not use thejava.util.Formatter formatargument.thrown- a string representing the throwable associated with the log record and its backtrace beginning with a newline character, if any; otherwise, an empty string.
Some example formats:
-
java.util.logging.SimpleFormatter.format="%4$s: %5$s [%1$tc]%n"This prints 1 line with the log level (
4$), the log message (5$) and the timestamp (1$) in a square bracket.WARNING: warning message [Tue Mar 22 13:11:31 PDT 2011] -
java.util.logging.SimpleFormatter.format="%1$tc %2$s%n%4$s: %5$s%6$s%n"This prints 2 lines where the first line includes the timestamp (
1$) and the source (2$); the second line includes the log level (4$) and the log message (5$) followed by the throwable and its backtrace (6$), if any:Tue Mar 22 13:11:31 PDT 2011 MyClass fatal SEVERE: several message with an exception java.lang.IllegalArgumentException: invalid argument at MyClass.mash(MyClass.java:9) at MyClass.crunch(MyClass.java:6) at MyClass.main(MyClass.java:3) -
java.util.logging.SimpleFormatter.format="%1$tb %1$td, %1$tY %1$tl:%1$tM:%1$tS %1$Tp %2$s%n%4$s: %5$s%n"This prints 2 lines similar to the example above with a different date/time formatting and does not print the throwable and its backtrace:
Mar 22, 2011 1:11:31 PM MyClass fatal SEVERE: several message with an exception -
java.util.logging.SimpleFormatter.format="%1$tb %1$td, %1$tY %1$tl:%1$tM:%1$tS.%1$tN %1$Tp %2$s%n%4$s: %5$s%6$s%n"Since JDK 9,
java.util.loggingusesjava.timeto create more precise time stamps. The format above can be used to add a.%1$tNto the date/time formatting so that nanoseconds will also be printed:Feb 06, 2015 5:33:10.279216000 PM example.Main main INFO: This is a test
This method can also be overridden in a subclass. It is recommended to use the
Formatter.formatMessage(LogRecord)convenience method to localize and format the message field.
-