log4j2打印日志(包含指定logger特殊打印)

log4j2.xml



    
        
            
        
        
            
                [%d{MM-dd HH:mm:ss SSS} %-5level] [%t] %c{3} - %m%n%ex
            
            
                
            
        


        
            
                %m%n
            

            
                
            
        
    
    


        
            
            
        

        
        
            
        
        
        
        
    



java类SPLogUtils

package com.daojia.data.query.util;

import com.alibaba.fastjson.JSON;

import com.daojia.data.query.vo.SPLog;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/**
 * Created by liquanyu on 2018-05-28
 */
public class SPLogUtils {
    //这里是用字符串指定特殊logger对象,也可以用类 eg:

    private static final Logger spLogger = LoggerFactory.getLogger("spLogger");
//    private static final Logger spLogger = LoggerFactory.getLogger(SPLogUtils.class);

    public static void log(String sql, Integer dsId) {

        spLogger.info(JSON.toJSONString(new SPLog(sql, dsId)));
    }
}


SPLog

package com.daojia.data.query.vo;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * Created with IDEA
 * description:
 * author:liquanyu
 * Date:2018/5/28
 * Time:10:48
 */
public class SPLog {
    private String logTime;
    private String sql;
    private Integer dsId;

    public SPLog(String sql, Integer dsId) {
        this.logTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        this.dsId = dsId;
        this.sql = sql;
    }

    public String getLogTime() {
        return logTime;
    }

    public void setLogTime(String logTime) {
        this.logTime = logTime;
    }

    public String getSql() {
        return sql;
    }

    public void setSql(String sql) {
        this.sql = sql;
    }

    public Integer getDsId() {
        return dsId;
    }

    public void setDsId(Integer dsId) {
        this.dsId = dsId;
    }
}


你可能感兴趣的:(log4j2打印日志(包含指定logger特殊打印))