2017-04-06 84 views
0

当我使用logback作为日志框架时,我在运行时添加记录器配置以进行调试。例如设置“org.springframework”为调试级别。从配置中删除记录器

找到我需要的数据后,我想在不重新启动应用程序的情况下删除此配置。

我该怎么做?

回答

2

根据您想要如何修改日志级别,您有两个选项。

第一种选择 - 使用logback.xml

的logback XML提供了一个选项,以周期性地扫描的配置。通过设置,您要求记录器框架定期更新其内存设置以与XML配置同步。如果要启用调试模式,XML配置看起来如下:

<configuration scan="true" scanPeriod="30 seconds"> 

<!-- Appender configurations here --> 

<logger name="org.springframework" level="debug" additivity="false"> 
    <appender-ref ref="YOUR_APPENDER_NAME"/> 
</logger> 

<!-- Other logger configurations (including root) go here --> 
</configuration> 

您完成任务后,你可以删除记录条目或级别设置为错误停止记录该软件包。

第二个方案 - 在内存

如果你不想碰你logback.xml,但不知何故,在运行时处理它(比方说,使用你的应用程序的一些的REST API),你可能会得到特定的记录器实例并将其级别设置为DEBUG。完成活动后,您可以将级别设置为“空”,以便级别从父级继承。示例代码如下:

import ch.qos.logback.classic.Level; 
import ch.qos.logback.classic.Logger; 
import ch.qos.logback.classic.LoggerContext; 
import org.slf4j.LoggerFactory; 

..... 

LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory(); 
Logger logger = context.getLogger("org.springframework"); 
logger.setLevel((Level.toLevel("DEBUG")); 

// Do your task 

logger.setLevel(null); // When we do this, level will be inherited from parent. 
0

logback有一个选项,允许定期重新扫描配置文件以查找更改。

<configuration scan="true"> 
    ... 
</configuration> 
+0

是的我知道我可以编辑配置文件。有没有办法从代码做到这一点? – igreen

+0

是的。 logback文档在描述如何执行此操作时非常有用。 –

+0

我在文档中找不到这个...可以分享链接吗? – igreen