开发者

Log4j notifying on logging level changes

开发者 https://www.devze.com 2023-01-28 07:53 出处:网络
Is there anyway , or any configuration in log4j that tell him to log a line whenever some \"logging level\" configurations occur ?

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


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


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.


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.


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...

0

精彩评论

暂无评论...
验证码 换一张
取 消