标题翻译
Weird behaviour of global logging level setting of java.util.logging
问题
我有一个相当简单的 logging.properties
配置:
handlers = java.util.logging.ConsoleHandler
.level = INFO
java.util.logging.ConsoleHandler.level = ALL
java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter
java.util.logging.SimpleFormatter.format=[%1$tF %1$tT] [%4$s] %5$s %n
如果将 .level
设置为 INFO,我可以如预期地获取所有 INFO 及更高级别的日志消息。但如果我将 .level
设置为 FINE、FINER、FINEST 或 ALL,则无法显示来自我的代码的任何消息(包括 WARNING 或 SEVERE)。所有“内部”的 FINE、FINER 等消息都可以正常显示,但我的代码中的消息却不能。
我尝试为我的包添加特定的日志级别:
org.example.mypackage.level = INFO
但这对问题没有影响。一旦全局的 .level
被设置为 FINE 或更低,或者设置为 ALL,我的代码产生的所有消息都不会显示,无论其级别如何。
这里有什么问题吗?是否存在这样的规则(我在网上没有找到任何相关信息)。
我使用 Maven 运行我的测试:
mvn test -Djava.util.logging.config.file=/path/to/my/logging.properties
我产生日志消息的代码如下:
public class MyClass {
private static final Logger logger = Logger.getLogger(MyClass.class.getName());
public void someMethod() {
logger.log(Level.WARNING, "Warning message");
}
}
更新:
这可能与 Maven 相关。一旦应用程序被编译并独立运行(纯 Java SE - 无应用服务器、无容器),问题就不再是问题。只有在使用 mvn ...
运行时,日志记录行为才会如上所述。
英文翻译
I have a pretty simple logging.properties
config:
handlers = java.util.logging.ConsoleHandler
.level = INFO
java.util.logging.ConsoleHandler.level = ALL
java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter
java.util.logging.SimpleFormatter.format=[%1$tF %1$tT] [%4$s] %5$s %n
If .level
is set to INFO I got all my INFO and higher messages as expected. But if I set the .level
to FINE, FINER, FINEST or ALL. None of messages from my code is displayed (neither WARNING or SEVERE). All "internal" FINE, FINER etc. messages are displayed properly, but not those from my code.
I tried to add a specific level for my packages:
org.example.mypackage.level = INFO
but it has no effect to the issue. Once the global .level
is set to FINE or lower or ALL, none of message produced by my code are displayed regardless their level.
What is wrong here? Is there any such a rule (I haven't find anything on the web).
I'm running my tests using maven:
mvn test -Djava.util.logging.config.file=/path/to/my/logging.properties
And my code that produces log messages looks like this:
public class MyClass {
private static final logger = Logger.getLogger(MyClass.class.getName());
public void someMethod() {
logger.log(Level.WARNING, "Warning message");
}
}
UPDATE:
This must be related to maven. Once the app is compiled and run on its own (pure java SE - no app server, no containers) the issue is no more a problem. Only when run using mvn ...
the logging behaves as described above.
答案1
得分: 0
这与Maven无关。更有可能与org.example.mypackage
有关。在运行单元测试时可能会出现日志记录器不存在的问题。
请将以下内容添加到您的单元测试代码顶部:
private static final Object PINS = new Object[]{Logger.getLogger("org.example.mypackage")};
正如上面的链接中所解释的,这将强制创建一个日志记录器,该记录器现在是应用程序中所有子记录器的新父记录器。这允许logging.properties
起作用。当您运行完整的应用程序时,您可能有创建此包日志记录器的代码,这就是为什么一切都正常工作的原因。
英文翻译
This is not related to Maven. More likely this has to do with org.example.mypackage
logger not existing when you run your unit tests.
Add the following to the top your unit test code:
private static final Object PINS = new Object[]{Logger.getLogger("org.example.mypackage")};
As explained in the link above this will force create a logger that is now the new parent of all of the child loggers in your application. This allows the logging.properties
to work. When you run your full application you probably have code that is creating this package logger which is why it all works.
专注分享java语言的经验与见解,让所有开发者获益!
评论