Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Log(ger) Variable Declaration

Tags:

java

logging

In the majority of cases I see Log instances declared as follows:

public static final Log LOG = LogFactory.getLog(MyClass.class);

I assume this means that the log configuration is loaded when the MyClass is loaded and is therefore set in stone until the MyClass is either reloaded or the JVM restarted?

So, if this assumption is correct what is the best way to ensure changes to the log configuration are picked up as (or as soon after) they happen?

like image 776
Nick Holt Avatar asked May 01 '26 05:05

Nick Holt


1 Answers

I assume that you are using commons-logging from the LogFactory class? As far as I know, none of the usual logging implementations (Log4J, java.util.logging) allow you to reload a configuration file in a running application (regardless of whether the actuall Loggers are declared as static variables). (EDIT: Peter's answer below proves that I was wrong about this in the case of Log4J)

However, they do allow for the dynamic changing of logging levels (e.g. via an MBean). These level-changes will be picked up by any Logger (including those declared as static variables). If you use java.util.logging you get the MBean for free in the JConsole.

Is it just the changing of levels you care about, or do you wish to provide completely different logging configurations (e.g. files, logger definitions) on the fly?

like image 67
oxbow_lakes Avatar answered May 02 '26 18:05

oxbow_lakes



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!