I am trying to set the java util logging config file at runtime to avoid having to set it as a VM parameter. But this just doesn't work. Whenever I am trying to reread the configuration, logging is disabled at all.
See the following code snippet:
package test;
import java.io.FileInputStream;
import java.util.logging.Level;
import java.util.logging.LogManager;
import java.util.logging.Logger;
public class A {
private static final Logger LOGGER= Logger.getLogger(A.class.getName());
public static void main(String[] args) throws Exception {
System.out.println("--- start");
LOGGER.log(Level.SEVERE, "SEVERE 1");
LOGGER.log(Level.FINEST, "FINEST 1");
LogManager.getLogManager().readConfiguration();
LOGGER.log(Level.SEVERE, "SEVERE 2");
LOGGER.log(Level.FINEST, "FINEST 2");
LogManager.getLogManager().readConfiguration(new FileInputStream("/tmp/logging.properties"));
LOGGER.log(Level.SEVERE, "SEVERE 3");
LOGGER.log(Level.FINEST, "FINEST 3");
System.out.println("--- end");
}
}
This is the output if I run the class without any VM argument:
--- start
09.11.2012 09:59:25 test.A main
SCHWERWIEGEND: SEVERE 1
09.11.2012 09:59:25 test.A main
SCHWERWIEGEND: SEVERE 2
--- end
As you can see, only the SEVERE levels are logged, as this is the default of the JREs logging.properties. Calling LogManager#readConfiguration()
doesn't change anything. But when trying to read the configuration from my logging.properties, absolutely nothing is logged. There is no difference in calling LogManager#readConfiguration(InputStream)
or setting the java.util.logging.config.file
property and calling LogManager#readConfiguration()
.
Now see the next output, when I run the same code with the VM property -Djava.util.logging.config.file=/tmp/logging.properties
:
--- start
2012-11-09 10:03:44.0838 SEVERE [test.A#main()] - SEVERE 1
2012-11-09 10:03:44.0843 FINEST [test.A#main()] - FINEST 1
--- end
As you can see, both the SEVERE and the FINEST levels are logged and they are logged in a different format. Both is specified in my custom logging.properties
.
But logging stops here after calling LogManager#readConfiguration()
! This is different from the example above and I don't understand it. Also, just as in the example above, calling LogManager#readConfiguration(InputStream)
doesn't work.
So what is the problem? According to the javadoc setting the java.util.logging.config.file property at runtime should work. Also both readConfiguration() methods should work as I expect. So what is the problem?
readConfiguration()
methods don't seem to do what they are meant to do. – CappadociareadConfiguration()
reads configuration and after this, the new logger config is used. JDK 1.6.0_27. – Havildar