如何配置 jdk14 日志记录模式

发布于 2024-10-25 22:53:18 字数 88 浏览 4 评论 0原文

我想我可以通过添加 java.util.logging.ConsoleHandler.pattern 行来更改模式,但是在哪里检查模式信息,例如 %u %h 等?

I guess I can chnage pattern by adding the line java.util.logging.ConsoleHandler.pattern, however where to check the pattern information like %u %h etc?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

塔塔猫 2024-11-01 22:53:18

这个问题已经有人回答了,但我想提供一些新信息:

从 Java 7 开始,可以使用 SimpleFormatter 配置日志消息的输出模式。

您可以在日志记录属性文件中使用此属性:

java.util.logging.SimpleFormatter.format = [%1$tc] %4$s: %2$s - %5$s %6$s%n

如果您需要有关模式语法的更多信息,请查看此处:
http://docs.oracle.com/javase/ 7/docs/api/java/util/Formatter.html

上面属性值中的数字是指提供给格式化程序的参数。
更多信息请参考Java官方文档:
http://docs.oracle.com/ javase/7/docs/api/java/util/logging/SimpleFormatter.html

示例配置文件 logging.properties

handlers = java.util.logging.ConsoleHandler
java.util.logging.ConsoleHandler.level = ALL
java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter
# Pattern works since Java 7
java.util.logging.SimpleFormatter.format = [%1$tc] %4$s: %2$s - %5$s %6$s%n

# Configure logging levels
# Available log levels are:
# OFF, SEVERE, WARNING, INFO, CONFIG, FINE, FINER, FINEST, ALL

# root logger
.level = WARNING

# child logger
org.example.level = ALL

当您调用 java 程序时,您可以指定配置文件作为参数:

java -Djava.util.logging.config.file=logging.properties -jar myProgram.jar

This question has already been answered by somebody, but i want to provide some new information:

Since Java 7 it is possible to configure the output pattern for log messages with the SimpleFormatter.

You can use this property in your logging properties file:

java.util.logging.SimpleFormatter.format = [%1$tc] %4$s: %2$s - %5$s %6$s%n

If you need more information on the pattern syntax have a look here:
http://docs.oracle.com/javase/7/docs/api/java/util/Formatter.html

The digits in the property value above refer to parameters provided to the formatter.
Please refer to the official Java docs for more information:
http://docs.oracle.com/javase/7/docs/api/java/util/logging/SimpleFormatter.html

Example configuration file logging.properties:

handlers = java.util.logging.ConsoleHandler
java.util.logging.ConsoleHandler.level = ALL
java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter
# Pattern works since Java 7
java.util.logging.SimpleFormatter.format = [%1$tc] %4$s: %2$s - %5$s %6$s%n

# Configure logging levels
# Available log levels are:
# OFF, SEVERE, WARNING, INFO, CONFIG, FINE, FINER, FINEST, ALL

# root logger
.level = WARNING

# child logger
org.example.level = ALL

When you call your java program you can specify your configuration file as parameter:

java -Djava.util.logging.config.file=logging.properties -jar myProgram.jar
那小子欠揍 2024-11-01 22:53:18

编辑:下面是当时为 Java 6 编写的。对于 7 及更高版本,请参阅下面 David 的回答。

AFAIK 没有这样的财产。有一个 java.util.logging.FileHandler.pattern ,但这是为了设置输出文件名的模式,而不是日志记录格式的模式。

在 util 日志记录 API 中配置输出格式的方法是设置 格式化程序。默认情况下,SimpleFormatter 已附加到您的 ConsoleHandler。此格式化程序只是对模式进行硬编码,并且不允许您设置它。

如果您需要不同的输出格式,则必须实现自己的 Formatter,或使用不同的日志记录框架,例如

Edit: The below was written at the time for Java 6. For 7 and later, refer to David's answer below.

AFAIK there is no such property. There is a java.util.logging.FileHandler.pattern but this is to set the pattern of the output filename, not of the logging format.

The way you configure the output format in the util logging API is by setting the Formatter. By default, a SimpleFormatter is attached to your ConsoleHandler. This formatter simply hardcodes the pattern and doesn't allow you to set it.

If you need a different output format, you'll have to either implement your own Formatter, or use a different logging framework, such as logback.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文