Java Logger:无法在logger.properties文件中设置日志级别 [英] Java Logger: Can't set log level in logger.properties file
问题描述
我有一个java项目,我使用maven来构建。
我在我的项目中使用 java.util.logging.Logger
,并希望使用 logger.properties
文件(不是命令行)。
I have a java project that I use maven to build.
I am using java.util.logging.Logger
in my project and would like to configure it using a logger.properties
file (not command line).
我创建了一个 logger.properties
这样的文件:
I created a logger.properties
file like this:
handlers=java.util.logging.FileHandler, java.util.logging.ConsoleHandler
.level=WARNING
java.util.logging.SimpleFormatter.format=[%4$s] %5$s %n
java.util.logging.config.file="../../results/api.log"
以下是我遇到的问题:
-
java.util.logging.SimpleFormatter.format
正在工作。当我在这里更改格式时,我会在项目中立即看到更改。所以我知道至少我正在正确导入文件。 -
.level
无法正常工作。我已经尝试将其更改为info
,最好的
,警告
等等,但在我更改之后,即使我告诉它不显示INFO,我仍然会看到所有INFO消息。 -
java.util .logging.config.file
无法正常工作。我有点期望这个不起作用,因为它是一个相对的路径。有人知道如何解析属性文件中的相对路径名吗?
java.util.logging.SimpleFormatter.format
is WORKING. When I change the format here, I see the change immediately in my project. So I know that at least I am importing the file correctly..level
is NOT working. I've tried changing it toinfo
,finest
,warning
, etc. but after I change it, I still see all the INFO messages even if I told it not to show INFO.java.util.logging.config.file
is NOT working. I kind of expected this one not to work because it is a relative path. Anyone know how I can resolve the relative path name in the properties file?
解决方案
我需要将 .level
移到顶部,如下所示:
Solution
I needed to move the .level
to the top, like this:
logger.properties
.level=WARNING
handlers=java.util.logging.FileHandler, java.util.logging.ConsoleHandler
java.util.logging.SimpleFormatter.format=[%4$s] %5$s %n
然后,为了将结果保存到api.log,我在我的代码中执行了此操作:
Then, in order to save results to "api.log", I did this in my code:
RequestHelper.java
import org.apache.commons.io.FilenameUtils;
public class RequestHelper {
private static final Logger LOGGER = Logger.getLogger( RequestHelper.class.getName() );
protected RequestHelper() {
//SET LOGGER TO OUTPUT TO "../../results/api.log"
//Logs will ALSO output to console.
String result_file = getNormalizedAbsolutePath("../../results/api.log");
Handler fh = new FileHandler(result_file);
Logger.getLogger("").addHandler(fh);
}
public static String getNormalizedAbsolutePath(String fileName) {
String path;
File file = new File(fileName);
try {
path = file.getCanonicalPath();
} catch (IOException e) {
LOGGER.warning("Error while computing the canonical path of file: " + fileName);
path = file.getAbsolutePath();
}
return FilenameUtils.normalize(path);
}
}
推荐答案
一个猜测。从文档中:
假设名称以.level结尾的所有属性都定义了Loggers的日志级别。因此,foo.level为命名层次结构中的任何子项定义了一个名为foo和(递归)的记录器的日志级别。 日志级别按属性文件中定义的顺序应用。因此,树中子节点的级别设置应该在其父级的设置之后。
尝试设置 .level
首先,在定义任何处理程序之前。如果先设置根记录器级别,之后定义的记录器将继承根日志级别。
Try setting .level
first, before you define any of the handlers. If you set the root logger level first, the loggers you define afterwards will inherit the root log level.
另外:
属性config。此属性旨在允许运行任意配置代码。
A property "config". This property is intended to allow arbitrary configuration code to be run.
java.util.logging.config.file
是系统属性,并且不会在配置文件中工作。尝试使用config。
java.util.logging.config.file
is a system property, and won't work in the config file. Try just using "config".
这篇关于Java Logger:无法在logger.properties文件中设置日志级别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!