Turning on/off logs at runtime

為{幸葍}努か 提交于 2019-12-11 06:49:11

问题


I am using SLF4J with Log4J for logging. So far I am using the log4j configuration from xml file. How I can turn on/off logging at runtime.

e.g. a web service turns on/off logs, so I will not have too much logs and will enable only for debugging purpose.

Thanks.


回答1:


log4j logger levels can be changed at runtime. Change the log4j logging level to error, so that all the debug and info statements will no be logged. If the application is restarted the logger level will be changed to the one defined in log4j.xml or log4j.properties.




回答2:


I found out that this disables the log4j completely:

org.apache.log4j.LogManager.resetConfiguration();
org.apache.log4j.LogManager.getRootLogger().addAppender(new NullAppender());

If you still want the logging but directed to a file you can do this:

LogManager.resetConfiguration();
LogManager.getRootLogger().addAppender(new FileAppender(new PatternLayout(), "log.log"));


来源:https://stackoverflow.com/questions/16334688/turning-on-off-logs-at-runtime

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!