我试图理解 Java Util Logging 中使用的格式模式。 通过实验我看到以下内容:
%1$ts epoch time: 1657069724
%1$tc Standard date/time format: Tue Jul 05 20:59:21 EDT 2022
%1$tY year - 2022
%1$tb 3 char month - Jul
%1$th 3 char month - Jul
%1$tm 2 digit month - 07
%1$td 2 digit day of month - 05
%1$tH: hours (0-23) 21:
%1$tM: minutes: 03:
%1$tS. Seconds: 55.
%1$tL Milleseconds 294
%2$s logger name and method
%3$s logger name
%4$-7s log level of this message - WARNING
(s前面的-7表示在7个字符的字段中左对齐。删除“-”右对齐)
这些记录在任何地方吗?我在 SimpleFormat API 或 Java Util Logging 文档中没有看到任何内容
最佳答案
java.util.logging.SimpleFormatter::format
解释一下:
The formatting can be customized by specifying the format string in the java.util.logging.SimpleFormatter.format property. The given LogRecord will be formatted as if by calling:
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 ZonedDateTime object representing event time of the log record in the ZoneId.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 the Formatter.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.
请注意,%[argument_index$]
与 SimpleFormatter 中的 javadoc 相差 1,因为 format
被列为第一个参数。例如。 date
被列为 2
,但实际上是 %1$
。
String::format有一个“参见”引用 java.util.Formatter
(不要与 java.util.logging.Formatter 混淆)。
“java.util.Formatter”的类级文档包含有关“日期/时间转换”和“标志”的信息。
如果您使用parameterized logging您还可以使用 java.text.MessageFormat 进行一些格式化关于 LogRecord 的参数。这可用于更改传递给 String::format
的“message”参数的行为。
关于java - Java Util Logging 中的格式化模式语义是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72877387/