2
votes

Is there anyway , or any configuration in log4j that tell him to log a line whenever some "logging level" configurations occur ?

I mean someone changed in some package the debug level from INFO to DEBUG , I want that event being logged by log4j.

Thanks

4
Changing a log level on a package is usually done in the log4j.xml. The app would have to be re-deployed. I don't think it'd be a runtime event. - duffymo
Wrong .. you can configure it to be uddated in runtime , log4j.properties or log4j.xml - Roman

4 Answers

1
votes

I'm not sure if it's your case, but if you are reloading the configuration file using configureAndWatch you should be able to see the information you need setting the system property -Dlog4j.debug=true.

See http://logging.apache.org/log4j/1.2/apidocs/index.html?org/apache/log4j/xml/DOMConfigurator.html

0
votes

There is a LogLog, the self logging of Log4J. I have never used it but if the solution exists it is there. I'd suggest you to download the source code of log4j and try to investigate it where it "knows" that the log level is being changed. Once you found the code see whether it prints something to self log and configure it as you need.

I'd be appreciate if you can post here more concrete answer if you find it. Good luck.

0
votes

At runtime, if you have a servlet, jsp or a screen on your application where you are able to change log levels, you will most likely be doing something like this

public void changeLogLevel(String className, String logLevel){
    Logger logger = Logger.getLogger(className);
    Level level = Level.toLevel(logLevel);
    logger.setLevel(level);
}

in order to log this event, all you would have to do is add an extra logger statement for this event

private static Logger classLogger = Logger.getLogger(ThisClass.class);
public void changeLogLevel(String className, String logLevel){
    Logger logger = Logger.getLogger(className);
    Level level = Level.toLevel(logLevel);
    logger.setLevel(level);
    classLogger.debug("The Level of " + className + " has changed to " + logLevel);
}

Then each time a log level occurs, you can log it here. If you want to get fancy, just send this log to its own file. You may want to advance the method even further to include an IP/username of the user who changed the log level.

If you have control over your application, ensure this is your only point in the application where a user can change your logging levels.

0
votes

This doesn't answer your question, but the built-in java.util.logging.LogManager implementation has an addPropertyChangeListener() method that does exactly what you want.

I couldn't find anything comparable in Log4J, but I didn't look that hard...