Slf4j+Logback实现分包分路径打印记录Log日志
随着项目代码增加,业务增多,把所有日志Log日志记录到一个文件里面,这样排查问题也挺苦难的
提示:以下是本篇文章正文内容,下面案例可供参考
Slf4j类似于Commons Logging,也是一个日志接口,而Logback类似于Log4j,是一个日志的实现。
Logback是由log4j创始人设计的另一个开源日志组件
代码如下(示例):
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
dependency>
代码如下(示例):
<configuration>
<appender name="consoleLog" class="ch.qos.logback.core.ConsoleAppender">
<layout class="ch.qos.logback.classic.PatternLayout">
<pattern>
%d - %msg%n
pattern>
layout>
appender>
<appender name="fileInfoLog" class="ch.qos.logback.core.rolling.RollingFileAppender">
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>INFOlevel>
<onMatch>ACCEPTonMatch>
<onMismatch>DENYonMismatch>
filter>
<encoder>
<pattern>
%d - %msg%n
pattern>
encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>xxxxx/info/%d.logfileNamePattern>
rollingPolicy>
appender>
<appender name="fileErrorLog" class="ch.qos.logback.core.rolling.RollingFileAppender">
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>ERRORlevel>
filter>
<encoder>
<pattern>
%d - %msg%n
pattern>
encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>xxxxx/error/%d.logfileNamePattern>
rollingPolicy>
appender>
<appender name="xxxxxInfo"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<append>trueappend>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>INFOlevel>
<onMatch>ACCEPTonMatch>
<onMismatch>DENYonMismatch>
filter>
<encoder>
<pattern>
%d - %msg%n
pattern>
encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>xxxxx/xxxxx/info/%d.logfileNamePattern>
rollingPolicy>
appender>
<appender name="xxxxxError" class="ch.qos.logback.core.rolling.RollingFileAppender">
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>ERRORlevel>
filter>
<encoder>
<pattern>
%d - %msg%n
pattern>
encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>xxxxx/xxxxx/error/%d.logfileNamePattern>
rollingPolicy>
appender>
<logger name="com.xxxxx.service.log.xxxxx.LogAppInfo" level="info" additivity="false">
<appender-ref ref="xxxxxInfo"/>
<appender-ref ref="xxxxxError"/>
logger>
<appender name="xxxxxInfo"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<append>trueappend>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>INFOlevel>
<onMatch>ACCEPTonMatch>
<onMismatch>DENYonMismatch>
filter>
<encoder>
<pattern>
%d - %msg%n
pattern>
encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>xxxxx/xxxxx/info/%d.logfileNamePattern>
rollingPolicy>
appender>
<appender name="xxxxxError" class="ch.qos.logback.core.rolling.RollingFileAppender">
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>ERRORlevel>
filter>
<encoder>
<pattern>
%d - %msg%n
pattern>
encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>xxxxx/xxxxx/error/%d.logfileNamePattern>
rollingPolicy>
appender>
<logger name="com.xxxxx.service.log.xxxxx.xxxxxInfo" level="info" additivity="false">
<appender-ref ref="xxxxxInfo"/>
<appender-ref ref="xxxxxError"/>
logger>
<appender name="xxxxxInfo"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<append>trueappend>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>INFOlevel>
<onMatch>ACCEPTonMatch>
<onMismatch>DENYonMismatch>
filter>
<encoder>
<pattern>
%d - %msg%n
pattern>
encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>xxxxx/xxxxx/info/%d.logfileNamePattern>
rollingPolicy>
appender>
<appender name="xxxxxError" class="ch.qos.logback.core.rolling.RollingFileAppender">
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>ERRORlevel>
filter>
<encoder>
<pattern>
%d - %msg%n
pattern>
encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>xxxxx/xxxxx/error/%d.logfileNamePattern>
rollingPolicy>
appender>
<logger name="com.xxxxx.service.log.xxxxx.xxxxxInfo" level="info" additivity="false">
<appender-ref ref="xxxxxInfo"/>
<appender-ref ref="xxxxxError"/>
logger>
<appender name="xxxxxInfo"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<append>trueappend>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>INFOlevel>
<onMatch>ACCEPTonMatch>
<onMismatch>DENYonMismatch>
filter>
<encoder>
<pattern>
%d - %msg%n
pattern>
encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>xxxxx/xxxxx/info/%d.logfileNamePattern>
rollingPolicy>
appender>
<appender name="xxxxxError" class="ch.qos.logback.core.rolling.RollingFileAppender">
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>ERRORlevel>
filter>
<encoder>
<pattern>
%d - %msg%n
pattern>
encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>xxxxx/xxxxx/error/%d.logfileNamePattern>
rollingPolicy>
appender>
<logger name="com.xxxxx.service.log.xxxxx.xxxxxInfo" level="info" additivity="false">
<appender-ref ref="xxxxxInfo"/>
<appender-ref ref="xxxxxError"/>
logger>
<appender name="xxxxxInfo"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<append>trueappend>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>INFOlevel>
<onMatch>ACCEPTonMatch>
<onMismatch>DENYonMismatch>
filter>
<encoder>
<pattern>
%d - %msg%n
pattern>
encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>xxxxx/xxxxx/info/%d.logfileNamePattern>
rollingPolicy>
appender>
<appender name="xxxxxError" class="ch.qos.logback.core.rolling.RollingFileAppender">
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>ERRORlevel>
filter>
<encoder>
<pattern>
%d - %msg%n
pattern>
encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>xxxxx/xxxxx/error/%d.logfileNamePattern>
rollingPolicy>
appender>
<logger name="com.xxxxx.service.log.xxxxx.xxxxxInfo" level="info" additivity="false">
<appender-ref ref="xxxxxInfo"/>
<appender-ref ref="xxxxxError"/>
logger>
<root level="info">
<appender-ref ref="consoleLog"/>
<appender-ref ref="fileInfoLog"/>
<appender-ref ref="fileErrorLog"/>
<appender-ref ref="xxxxxInfo"/>
<appender-ref ref="xxxxxInfo"/>
<appender-ref ref="xxxxxInfo"/>
<appender-ref ref="xxxxxInfo"/>
<appender-ref ref="xxxxxError"/>
<appender-ref ref="xxxxxError"/>
<appender-ref ref="xxxxxError"/>
<appender-ref ref="xxxxxError"/>
<appender-ref ref="xxxxxInfo"/>
<appender-ref ref="xxxxxError"/>
root>
configuration>
以上是配置XML
直接复制下来可以使用