简体   繁体   English

java.util.logging 全局日志记录级别设置的奇怪行为

[英]Weird behaviour of global logging level setting of java.util.logging

I have a pretty simple logging.properties config:我有一个非常简单的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

If .level is set to INFO I got all my INFO and higher messages as expected.如果.level设置为 INFO,我会按预期收到所有 INFO 和更高级别的消息。 But if I set the .level to FINE, FINER, FINEST or ALL.但是,如果我将.level设置为 FINE、FINER、FINEST 或 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.所有“内部”FINE、FINER 等消息都正确显示,但不是来自我的代码的消息。

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.一旦全局.level设置为 FINE 或更低或 ALL,无论级别如何,我的代码生成的任何消息都不会显示。

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:我正在使用 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.更新:这一定与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.一旦应用程序被编译并自行运行(纯 Java SE - 没有应用程序服务器,没有容器),问题就不再是问题了。 Only when run using mvn ... the logging behaves as described above.仅当使用mvn ...运行时mvn ...日志记录的行为如上所述。

This is not related to Maven.这与Maven无关。 More likely this has to do with org.example.mypackage logger not existing when you run your unit tests .这更有可能与运行单元测试时org.example.mypackage logger 不存在有关

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.这允许logging.properties工作。 When you run your full application you probably have code that is creating this package logger which is why it all works.当你运行你的完整应用程序时,你可能有创建这个包记录器的代码,这就是为什么它一切正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM