1. 程式人生 > >logback不同業務的日誌列印到不同檔案

logback不同業務的日誌列印到不同檔案

在業務邏輯較為多的系統中,為了能快速的排查線上的問題和清楚的查詢各個業務的日誌資訊,往往需要對不同業務線的日誌進行分開記錄:
比如現在系統中有對小金庫使用者發放獎勵和白條使用者進行發放獎勵。

logback.xml配置檔案如下:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>

    <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
        <encoder charset="UTF-8"
>
<pattern>[%d{yyyy-MM-dd HH:mm:ss.SSS}] %level [%thread] %file:%line - %msg%n</pattern> <charset>UTF-8</charset> </encoder> </appender> <appender name="SYS_INFO" class="ch.qos.logback.core.rolling.RollingFileAppender">
<File>${catalina.base}/program/info.log</File> <append>true</append> <!--過濾器,只打INFO級別的日誌--> <filter class="ch.qos.logback.classic.filter.LevelFilter"> <level>INFO</level> <onMatch>ACCEPT</onMatch
>
<onMismatch>DENY</onMismatch> </filter> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <fileNamePattern>${catalina.base}/program/info.log.%d</fileNamePattern> <maxHistory>30</maxHistory> </rollingPolicy> <encoder charset="UTF-8"> <pattern>[%d{yyyy-MM-dd HH:mm:ss.SSS}] %level [%thread] %file:%line - %msg%n</pattern> <charset>UTF-8</charset> </encoder> </appender> <appender name="SYS_ERROR" class="ch.qos.logback.core.rolling.RollingFileAppender"> <File>${catalina.base}/program/error.log</File> <append>true</append> <!--過濾器,只打ERROR級別的日誌--> <filter class="ch.qos.logback.classic.filter.LevelFilter"> <level>ERROR</level> <onMatch>ACCEPT</onMatch> <onMismatch>DENY</onMismatch> </filter> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <fileNamePattern>${catalina.base}/program/error.log.%d</fileNamePattern> <maxHistory>12</maxHistory> </rollingPolicy> <encoder charset="UTF-8"> <pattern>[%d{yyyy-MM-dd HH:mm:ss.SSS}] %level [%thread] %file:%line - %msg%n</pattern> <charset>UTF-8</charset> </encoder> </appender> <!--不同業務邏輯的日誌列印到不同檔案--> <appender name="baitiaoUserAppender" class="ch.qos.logback.core.rolling.RollingFileAppender"> <File>${catalina.base}/program/bt.log</File> <append>true</append> <filter class="ch.qos.logback.classic.filter.LevelFilter"> <level>INFO</level> <onMatch>ACCEPT</onMatch> <onMismatch>DENY</onMismatch> </filter> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <fileNamePattern>${catalina.base}/program/bt.log.%d</fileNamePattern> <maxHistory>12</maxHistory> </rollingPolicy> <encoder charset="UTF-8"> <pattern>[%d{yyyy-MM-dd HH:mm:ss.SSS}] %level [%thread] %file:%line - %msg%n</pattern> <charset>UTF-8</charset> </encoder> </appender> <appender name="xjkUserAppender" class="ch.qos.logback.core.rolling.RollingFileAppender"> <File>${catalina.base}/program/xjk.log</File> <append>true</append> <filter class="ch.qos.logback.classic.filter.LevelFilter"> <level>INFO</level> <onMatch>ACCEPT</onMatch> <onMismatch>DENY</onMismatch> </filter> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <fileNamePattern>${catalina.base}/program/xjk.log.%d</fileNamePattern> <maxHistory>12</maxHistory> </rollingPolicy> <encoder charset="UTF-8"> <pattern>[%d{yyyy-MM-dd HH:mm:ss.SSS}] %level [%thread] %file:%line - %msg%n</pattern> <charset>UTF-8</charset> </encoder> </appender> <!-- 不同的業務邏輯日誌列印到指定資料夾--> <logger name="baitiaoUser" additivity="false" level="INFO"> <appender-ref ref="baitiaoUserAppender"/> </logger> <logger name="xjkUser" additivity="false" level="INFO"> <appender-ref ref="xjkUserAppender"/> </logger> <logger name="com.act.award" level="INFO"/> <logger name="com.act.award" level="DEBUG"/> <!--info和error分開列印--> <root level="INFO"> <appender-ref ref="CONSOLE"/> <appender-ref ref="SYS_INFO"/> <appender-ref ref="SYS_ERROR"/> </root> </configuration>

程式碼:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:/spring/application.xml")
public class MainTest {
    Logger XJK_USER_LOG = LoggerUtils.Logger(LogFileName.XJK_USER);
    Logger BAITIAO_USER_LOG = LoggerUtils.Logger(LogFileName.BAITIAO_USER);

    @Test
    public void testGetBusinessAccount() throws Exception {
        XJK_USER_LOG.info("小金庫使用者進來了...");
        BAITIAO_USER_LOG.info("白條使用者進來了...");
    }

}

public enum LogFileName {

    //配置到logback.xml中的logger name="vipUser"
    XJK_USER("xjkUser"),
    BAITIAO_USER("baitiaoUser");

    private String logFileName;

    LogFileName(String fileName) {
        this.logFileName = fileName;
    }

    public String getLogFileName() {
        return logFileName;
    }

    public void setLogFileName(String logFileName) {
        this.logFileName = logFileName;
    }

    public static LogFileName getAwardTypeEnum(String value) {
        LogFileName[] arr = values();
        for (LogFileName item : arr) {
            if (null != item && StringUtils.isNotBlank(item.logFileName)) {
                return item;
            }
        }
        return null;
    }
}

public class LoggerUtils {
    public static <T> Logger Logger(Class<T> clazz) {
        return LoggerFactory.getLogger(clazz);
    }

    /**
     * 列印到指定的檔案下
     *
     * @param desc 日誌檔名稱
     * @return
     */
    public static Logger Logger(LogFileName desc) {
        return LoggerFactory.getLogger(desc.getLogFileName());
    }
}

日誌檔案形式:

Paste_Image.png
bt.log內容:
INFO [main] MainTest.java:23 - 白條使用者進來了…
xjk.log內容:
INFO [main] MainTest.java:22 - 小金庫使用者進來了…