我想定义一个能够:
的logback配置文件也就是说,当我打电话时:
logger.warn("blah"); // Logs to both info.log and debug.log
logger.debug("bleh"); // Logs to debug.log only
有可能吗?
我试过这样的事情,但似乎当logback遇到多个< root>标签,只需要最后一个:
<appender name="infoFile" class="ch.qos.logback.core.rolling.RollingFileAppender">
<File>${LOGS_FOLDER}/info.log</File>
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<appender name="debugFile" class="ch.qos.logback.core.rolling.RollingFileAppender">
<File>${LOGS_FOLDER}/debug.log</File>
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<root level="INFO">
<appender-ref ref="infoFile" />
</root>
<root level="DEBUG">
<appender-ref ref="debugFile" />
</root>
这里,info.log包含调试级日志:(
请注意,我想在我的软件包的每个上应用此行为。
答案 0 :(得分:7)
使用levelFilter解决了问题
示例:
<appender name="infoFile" class="ch.qos.logback.core.rolling.RollingFileAppender">
<File>${LOGS_FOLDER}/info.log</File>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>INFO</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<appender name="debugFile" class="ch.qos.logback.core.rolling.RollingFileAppender">
<File>${LOGS_FOLDER}/debug.log</File>
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<root level="DEBUG"><!-- Using the lowest level here -->
<appender-ref ref="debugFile" />
<appender-ref ref="infoFile" />
</root>