logback不同业务的日志打印到不同文件

在业务逻辑较为多的系统中,为了能快速的排查线上的问题和清楚的查询各个业务的日志信息,往往需要对不同业务线的日志进行分开记录:
比如现在系统中有对小金库用户发放奖励和白条用户进行发放奖励。

logback.xml配置文件如下:




    
        
            [%d{yyyy-MM-dd HH:mm:ss.SSS}] %level [%thread] %file:%line - %msg%n
            UTF-8
        
    

    
        ${catalina.base}/program/info.log
        true
        
        
            INFO
            ACCEPT
            DENY
        
        
            ${catalina.base}/program/info.log.%d
            30
        

        
            [%d{yyyy-MM-dd HH:mm:ss.SSS}] %level [%thread] %file:%line - %msg%n
            UTF-8
        
    

    
        ${catalina.base}/program/error.log
        true
        
        
            ERROR
            ACCEPT
            DENY
        
        
            ${catalina.base}/program/error.log.%d
            12
        

        
            [%d{yyyy-MM-dd HH:mm:ss.SSS}] %level [%thread] %file:%line - %msg%n
            UTF-8
        
    

    
    
        ${catalina.base}/program/bt.log
        true
        
            INFO
            ACCEPT
            DENY
        
        
            ${catalina.base}/program/bt.log.%d
            12
        
        
            [%d{yyyy-MM-dd HH:mm:ss.SSS}] %level [%thread] %file:%line - %msg%n
            UTF-8
        
    

    
        ${catalina.base}/program/xjk.log
        true
        
            INFO
            ACCEPT
            DENY
        
        
            ${catalina.base}/program/xjk.log.%d
            12
        
        
            [%d{yyyy-MM-dd HH:mm:ss.SSS}] %level [%thread] %file:%line - %msg%n
            UTF-8
        
    

    
    
        
    
    
        
    

    
    

    
    
        
        
        
    


代码:

@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  Logger Logger(Class clazz) {
        return LoggerFactory.getLogger(clazz);
    }

    /**
     * 打印到指定的文件下
     *
     * @param desc 日志文件名称
     * @return
     */
    public static Logger Logger(LogFileName desc) {
        return LoggerFactory.getLogger(desc.getLogFileName());
    }
}

日志文件形式:

logback不同业务的日志打印到不同文件_第1张图片
Paste_Image.png

bt.log内容:
INFO [main] MainTest.java:23 - 白条用户进来了...
xjk.log内容:
INFO [main] MainTest.java:22 - 小金库用户进来了...

你可能感兴趣的:(logback不同业务的日志打印到不同文件)