java 如何配置 jdk14 日志记录的模式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5416769/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 11:00:50  来源:igfitidea点击:

How to configure the jdk14 logging's pattern

javajava.util.logging

提问by user496949

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?

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

采纳答案by wds

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

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

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

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

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

在 util 日志记录 API 中配置输出格式的方式是通过设置Formatter. 默认情况下, aSimpleFormatter附加到您的ConsoleHandler. 这个格式化程序只是对模式进行硬编码,不允许您设置它。

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.

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

回答by David

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.

从 Java 7 开始,可以使用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

如果您需要有关模式语法的更多信息,请查看此处: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

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

Example configuration file logging.properties:

示例配置文件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

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

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

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