gpt4 book ai didi

java - 为 log4j 添加微秒级

转载 作者:行者123 更新时间:2023-11-29 08:28:30 24 4
gpt4 key购买 nike

引用this

如何添加微秒级日志记录,如何为此创建自定义模式格式化程序?

我也尝试过 log4j2,但最终给出了

12:42:05,972803200 [main] DEBUG Log4j2Test - Debug Message Logged !!!
12:42:05,975803200 [main] INFO Log4j2Test - Info Message Logged !!!
12:42:05,975803200 [main] ERROR Log4j2Test - Error Message Logged !!!

如您所见,它始终给出 803200 微秒和纳秒。

我的 log4j2.xml 看起来像:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="INFO">
<Appenders>
<Console name="console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{ABSOLUTE_NANOS} [%t] %-5level %logger{36} - %msg%n"/>
<!-- <PatternLayout
pattern="[%-5level] %d{ABSOLUTE_NANOS} [%t] %c{1} - %msg%n" /> -->
</Console>
</Appenders>
<Loggers>
<Root level="debug" additivity="false">
<AppenderRef ref="console" />
</Root>
</Loggers>

最佳答案

这取决于您正在使用的 log4j。仅当您使用 JDK9 和新版本的 log4j (2.x) 时才支持此功能。

您正在搜索的格式是这样的:%d{HH:mm:ss,nnnnnn}

另请注意官方文档的内容,因为有很多“陷阱和陷阱”。即:

Log4j 2.11 adds limited support for timestamps more precise than milliseconds when running on Java 9. Note that not all DateTimeFormatter formats are supported. Only timestamps in the formats mentioned in the table above may use the "nano-of-second" pattern letter n instead of the "fraction-of-second" pattern letter S. Users may revert back to a millisecond-precision clock when running on Java 9 by setting system property log4j2.Clock to SystemMillisClock

有关以下链接的更多信息:Log4j 2.x layouts

关于java - 为 log4j 添加微秒级,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50287453/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com