Reputation: 926
I have a desktop java application that I foresaw that Java logging would be sufficient for. To make the matter simpler I use the java global logger to get logger instead of passing any specific logger name
Logger logger = Logger.getGlobal();
String logFilePath = Config.INSTANCE.getProperty("logFilePath");
FileHandler fileHandler = new FileHandler(logFilePath);
logger.addHandler(fileHandler);
SimpleFormatter formatter = new SimpleFormatter();
fileHandler.setFormatter(formatter);
As you can see above, I am also making changes to the global logger in terms of output file path and the formatter.
My question is about the scope of the global logger, is it globally scoped in terms of the JVM? or globally scoped over the application? and if globally scoped over JVM, should I expect side effects on the applications sharing the same JVM?
Tip:
I have went through this thread In java.util.logging, what is the global logger for? but the comments were not something I could make use of.
Upvotes: 1
Views: 3353
Reputation: 4767
Yes there is only one Global Logger instance per JVM. Hence all apps within the JVM will share the same Global Logger instance.
Refer to the source of java.util.Logger
if you are keen to see how it has been declared. It has been declared as below on line 218
@Deprecated
218 public static final Logger global = new Logger(GLOBAL_LOGGER_NAME);
That said you really should not be using the Global Logger because it is prone to deadlocks. Quoting Java documentation related to the global logger field :
Deprecated: Initialization of this field is prone to deadlocks. The field must be initialized by the Logger class initialization which may cause deadlocks with the LogManager class initialization. In such cases two class initialization wait for each other to complete. As of JDK version 1.6, the preferred way to get the global logger object is via the call Logger.getLogger(Logger.GLOBAL_LOGGER_NAME).
If you are really serious about logging (which you should be) then consider declaring loggers per component which can be controlled in a granular fashion using logger settings.
Additional links related to logging :
Upvotes: 6
Reputation: 262504
If several apps shared the same JVM (which is an usual scenario for a desktop application, I don't think you really need to be concerned about that), they will share the same boot and system classloader, which includes java.*
packages (application classes can be separated).
As a result, there will be only one global Logger instance per JVM.
Upvotes: 0