public class SimpleFormatter
extends Formatter
| java.lang.Object | ||
| java.util.logging.Formatter | ||
| java.util.logging.SimpleFormatter | ||
以可读的格式打印LogRecord的简要摘要。 摘要通常是1或2行。
Configuration:的SimpleFormatter初始化为format string在指定java.util.logging.SimpleFormatter.format属性format日志消息。 该属性可以在logging properties配置文件中定义或作为系统属性定义。 如果在日志记录属性和系统属性中都设置了此属性,则将使用系统属性中指定的格式字符串。 如果此属性未定义或给定的格式字符串为illegal ,则默认格式为特定于实现的。
也可以看看:
Public constructors |
|
|---|---|
SimpleFormatter() |
|
公共方法(Public methods) |
|
|---|---|
String |
format(LogRecord record) 格式化给定的LogRecord。 |
继承方法(Inherited methods) |
|
|---|---|
java.util.logging.Formatter
|
|
java.lang.Object
|
|
String format (LogRecord record)
格式化给定的LogRecord。
可以通过在java.util.logging.SimpleFormatter.format属性中指定format string来自定义格式。 给定的LogRecord将被格式化,就好像通过调用:
String.format(format, date, source, logger, level, message, thrown);
where the arguments are:
format - the java.util.Formatter format string specified in the java.util.logging.SimpleFormatter.format property or the default format.date - a Date object representing event time of the log record.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 the formatMessage(LogRecord) method. It uses java.text formatting and does not use the java.util.Formatter format argument.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.一些示例格式:
java.util.logging.SimpleFormatter.format="%4$s: %5$s [%1$tc]%n" 这会在方括号中打印1行日志级别( 4$ ),日志消息( 5$ )和时间戳( 1$ )。
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" 这将打印2行,其中第一行包括时间戳记( 1$ )和来源( 2$ ); 第二行包括日志级别( 4$ )和日志消息( 5$ ),后面跟着throwable及其回溯( 6$ ),如果有的话:
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" 这将打印2行与上面的示例类似的不同日期/时间格式,并不打印throwable及其回溯:
Mar 22, 2011 1:11:31 PM MyClass fatal
SEVERE: several message with an exception
这个方法也可以在子类中重写。 建议使用formatMessage(LogRecord)便捷方法来定位和格式化消息字段。
| 参数(Parameters) | |
|---|---|
record |
LogRecord: the log record to be formatted. |
| 返回(Returns) | |
|---|---|
String |
a formatted log record |