-1

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.

bambula
  • 365
  • 1
  • 12

1 Answers1

0

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.

jmehrens
  • 10,580
  • 1
  • 38
  • 47