使用默认Java Logging API的SimpleFormatter的默认参数是什么?

时间:2014-03-07 21:43:48

标签: java java.util.logging

我认为答案可能会有所不同,具体取决于所使用的处理程序,但我们假设使用了ConsoleHandler并使用了默认的SimpleFormatter。传递给SimpleFormatter.format()的每个参数的数量,类型和可能的含义/语义是什么?

我在另一个SO问题中找到了这个字符串:

"%1\$tY-%1\$tm-%1\$td %1\$tH:%1\$tM:%1\$tS.%1\$tL %4\$s %2\$s %5\$s%6\$s%n"

(为Groovy添加了反斜杠)

注意缺少的%3,那么这个论点是什么?是否有%7%8等......它们是什么?

我认为%1是当前时间,%4是日志级别和其他几个......我正在寻找完整的参数列表及其含义/值。

1 个答案:

答案 0 :(得分:10)

来自http://docs.oracle.com/javase/7/docs/api/java/util/logging/SimpleFormatter.html

where the arguments are:
   1 format - the java.util.Formatter format string specified in the java.util.logging.SimpleFormatter.format property or the default format.
   2 date - a Date object representing event time of the log record.
   3 source - a string representing the caller, if available; otherwise, the logger's name.
   4 logger - the logger's name.
   5 level - the log level.
   6 message - the formatted log message returned from the Formatter.formatMessage(LogRecord) method. It uses java.text formatting and does not use the java.util.Formatter format argument.
   7 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.

由于第一个参数是格式字符串,因此其余参数从索引1开始(首先是日期):

in the format string:
   1 date - a Date object representing event time of the log record.
   2 source - a string representing the caller, if available; otherwise, the logger's name.
   3 logger - the logger's name.
   4 level - the log level.
   5 message - the formatted log message returned from the Formatter.formatMessage(LogRecord) method. It uses java.text formatting and does not use the java.util.Formatter format argument.
   6 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.

(我不敢相信在发布到SO之前我在初始搜索中找不到这个: - )