简体   繁体   English

Log4j通知日志记录级别更改

[英]Log4j notifying on logging level changes

Is there anyway , or any configuration in log4j that tell him to log a line whenever some "logging level" configurations occur ? 无论如何,还是在log4j中有任何配置告诉他每当发生“记录级别”配置时就记录一行?

I mean someone changed in some package the debug level from INFO to DEBUG , I want that event being logged by log4j. 我的意思是有人在某些软件包中将调试级别从INFO更改为DEBUG,我希望该事件由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 . 我不确定是否适合您,但是如果您使用configureAndWatch重新加载配置文件,则应该能够看到设置系统属性-Dlog4j.debug=true所需的信息。

See http://logging.apache.org/log4j/1.2/apidocs/index.html?org/apache/log4j/xml/DOMConfigurator.html 参见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. 有一个LogLog,即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. 我建议您下载log4j的源代码,并尝试在“知道”日志级别正在更改的位置进行调查。 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 在运行时,如果您的应用程序上有一个servlet,jsp或屏幕,您可以在其中更改日志级别,那么您很可能会这样做

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. 您可能希望进一步改进该方法,以包括更改日志级别的用户的IP /用户名。

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. 这不能回答您的问题,但是内置的java.util.logging.LogManager实现具有完全满足您需要的addPropertyChangeListener()方法。

I couldn't find anything comparable in Log4J, but I didn't look that hard... 我在Log4J中找不到任何可比的东西,但看上去并不难...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM