Reputation: 41
I have a question about java.util.logging. It's mostly about understanding it because I already got it to work. But I'm still not quite sure why it works.
So i have a big and very old application with many threads without any synchronization (not invented by me, i'm just the poor maintainer). I ported the proprietary logging to java.util.logging. At startup i read a configuration file:
String logname = Ini.getProperty("BoxLog", "boxlog.properties");
logManager = LogManager.getLogManager();
try {
logManager.readConfiguration(new FileInputStream(logname));
LOGGER.info("Read log configuration file " + logname);
} catch (SecurityException e) { ... }
I stepped through it with a debugger, and everything is ok here. No exception, the correct file is read, everything seems to be ok. But the format for the log handler is not updated. So the log line here is output in the format given as default in the system-wide configuration.
Due to the many threads, there are a few log outputs in another object that use the default configuration, they are output before my configuration is read. After I removed these loggings everything worked ok.
The documentation says for readConfiguration(): Reinitialize the logging properties and reread the logging configuration. So I assumed that after these few lines of logging happened, and then after the correct configuration was read, every further logging would be in the given format. But it was not. Every further logging in any class was still in the default format. It seems to me that on reading the configuration any already instantiated loggers and their handlers are not reinitialized.
Now this is the question. How does this work really? Did I miss something here? Or did I wrongly understand something here? Or is there a bug in the documentation? Or what else?
Upvotes: 0
Views: 111
Reputation: 11085
The documentation says for readConfiguration(): Reinitialize the logging properties and reread the logging configuration. So I assumed that after these few lines of logging happened, and then after the correct configuration was read, every further logging would be in the given format.
You are running in to JDK-8033661 readConfiguration does not cleanly reinitialize the logging system and JDK-5035854 LogManager.readConfiguration does not properly modify existing loggers.
This is resolved in JDK9 by adding LogManager.updateConfiguration(InputStream, Function<String,BiFunction<String,String,String>>)
This is covered in detail in Use of readConfiguration method in logging activities.
If you don't have access to JDK9 then you can use the java.util.logging.config.class
option along with the java.util.logging.config.file
option to create custom code that works around limitation of the bugs listed above.
Every further logging in any class was still in the default format
If you are using the SimpleFormatter
then that is covered in SimpleFormatter ignoring the java.util.logging.SimpleFormatter.format property.
Upvotes: 1