springboot+logback不同设备id的日志打印到不同设备id文件

logback-spring.xml







    
    
    
    


    
        deviceId
        unknown
    
    
        
            
                UTF-8
                %msg%n
            
            
                ${logDir}\%d{yyyyMMdd}\${deviceId}_error.log
                ${maxHistory}
            
            
                ERROR
                ACCEPT
                DENY
            
        
    






    
        deviceId
        unknown
    
    
        
            true
            
                %msg%n
                UTF-8
            
            
                ${logDir}\%d{yyyyMMdd}\${deviceId}_info.log
                ${maxHistory}
            
            
                INFO
                ACCEPT
                DENY
            
        
    




    
    0
    
    2048
    true
    
    



    
    0
    
    2048
    true
    
    



    
    
    



java 代码

import cn.xxx.mq.entity.Log;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;

import java.util.Date;

public class LogUtil{

    private static Logger log = LoggerFactory.getLogger(LogUtil.class);

    public static void errorLog(String devId, String title, String cmd, JSONObject msg){
        Log logEntity = new Log(devId,DateTimeUtil.dateToStr(new Date(),DateTimeUtil.STANDARD_FORMAT),
                title,cmd,msg);
        MDC.put("deviceId", devId);
        log.error(JSONObject.toJSONString(logEntity));
    }

    public static void infoLog(String devId,String title,String cmd,JSONObject msg){
        Log logEntity = new Log(devId,DateTimeUtil.dateToStr(new Date(),DateTimeUtil.STANDARD_FORMAT),
                title,cmd,msg);
        MDC.put("deviceId", devId);
        log.info(JSONObject.toJSONString(logEntity));
    }
}

你可能感兴趣的:(springboot,logback,java)