2

I have a java command line tool that turns logging off at the very beginning, then parses the command line and later possibly turns logging on again, depending on the result of command line processing.

The main method begins like this:

public static void main(final String[] args){
    java.util.logging.LogManager.getLogManager().reset();
    java.util.logging.Logger globalLogger = java.util.logging.Logger
            .getLogger(java.util.logging.Logger.GLOBAL_LOGGER_NAME);
    globalLogger.setLevel(java.util.logging.Level.OFF);
    [... parse command line and possibly turn logging back on]

Now Findbugs give a "Troubling" warning that says:

Changes to logger could be lost in com.bmw.fnw.DBMainDialog.main(String[])

From the Find Bugs Description I understand that the logger could possilby on when it shouldn't.

What can I do to properly turn the logger off?

bbuser
  • 918
  • 11
  • 33

3 Answers3

4

Just hold a strong reference to the logger as long as you need it (probably for the lifetime of your program).

The link you gave already explains why: In OpenJDk, the logManager internally only keeps weak references. So the logger becomes eligible for garbage collection as soon as there is no strong reference. So you set the configuration, but don't hold a strong reference. It may be garbage collected. When you get the Logger again, a new one is instantiated. Garbage collection is prevented by holding a strong reference.

Polygnome
  • 7,639
  • 2
  • 37
  • 57
  • 1
    I have made my logger static, but now FindBugs complains about `globalLogger.setLevel(java.util.logging.Level.OFF);`. – bbuser Aug 14 '12 at 14:43
1

Are you using OpenJDK as stated by the Find Bugs description?

Whatever since they talk about Weak Reference, what comes to my mind is that you may create a static reference somewhere to this logger, to avoid losing it during garbage collection.

To test the problem you may call yourself the garbage collector in your code, see if it changes anything.

Michael Laffargue
  • 10,116
  • 6
  • 42
  • 76
  • 2
    It does not matter which JDK YOU are actually using. Someone who runs that code could be using OpenJDK. Therefore you should fix that "bug" regardless of the JDK you are using. – Polygnome Aug 14 '12 at 09:57
  • 1
    Thanks for your answer. I made my logger `private final static`, but now FindBugs complains about the `globalLogger.setLevel(java.util.logging.Level.OFF);` with the same error message. What do I do now? – bbuser Aug 14 '12 at 14:23
0

I had the exact same problem and also couldn't get rid of the warning. I have no idea if FindBugs is just messing with us, but holding a strong reference does not seem to help.

To get rid of the warning I simply included log4j. You can then set -Dlog4j.configuration=< FILE_PATH > as part of the jvm arguments. FILE_PATH is the path to the log4j.properties file. Reference: log4j configuration via JVM argument(s)?

This will stop giving you the warning and you can still configure the debug level. Hope this helps!

A~

Community
  • 1
  • 1
aZen
  • 223
  • 1
  • 8