java - Java Util Logging 中的格式化模式语义是什么?

标签 java java.util.logging

我试图理解 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:

  1. format - the java.util.Formatter format string specified in the java.util.logging.SimpleFormatter.format property or the default format.
  2. date - a ZonedDateTime object representing event time of the log record in the ZoneId.systemDefault() system time zone.
  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.

请注意,%[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/

相关文章:

Java:二维数组的For循环打印语句

Java:如何从 NIO Server 向所有 channel 发送消息

java - 如何整合 java.util.logging、System.(out|err) 和 Jetty 日志记录?

java - XMLFormatter 中的 getTail() 方法如何工作?

java - 如何使用 Spring Security 禁用 @Controller 内单个路径的授权

java - 无法解析方法ComputeDistanceBetween

java - 如何使用 Simple Formatter 修改日志格式?

java - 如何使用系统属性值作为记录器处理程序模式的一部分

java - 是否有强大的 java.util.logging 系统日志处理程序实现?

java jpcap java.lang.UnsatisfiedLinkError