I have some code that is sometimes called from my own Main method and sometimes it is embedded within someone else’s program. When configuring logging, I want to be able to detect that someone has already configured Java Util Logging and if they have then I want to skip my configuration. I have this method:
public static boolean isLoggingAlreadyConfigured(){
Logger logger = Logger.getLogger("");
Handler[] handlers = logger.getHandlers();
return handlers != null && handlers.length > 1;
}
But I think it is unreliable.
Can you suggest something better?
The simple answer is that java.util logging is always configured.
The javadoc for
LogManagersays that it is a global object that is created automatically, and that it is configured automatically based either on what a couple of system properties say, or (if the properties are not set) using a built-in logging properties configuration. (Refer to the javadoc for details). The bottom line is that you won’t ever see the logger in an “unconfigured” state.You could figure out what a JRE’s built-in logging properties configuration says by default, and test to see if the current configuration is different. But this approach has some problems:
and most important