log4j2单独打印sql日志配置

前两天系统要求业务逻辑日志和数据库sql日志分开打印。
项目日志用的log4j2,架构为SSM架构,代码结构遵循MVC模式即每个业务模块都有单独的包结构,每个业务包下都有serivce、dao等包结构,要单独打印sql日志就要筛选出每个业务包结构下的dao包里的日志,但糟糕的是log4j2没有通配符,包路径不能com.**.dao.**这样写,所以需要用到自定义过滤器实现日志过滤,下面是我增加的log4j2配置。

话不多说,上代码:

1.log4j2.xml配置


                                                             注意加上下方包扫描路径为自定义filter所在包路径

	
        
        ${sys:catalina.home}/logs
        %d{yyyy/MM/dd HH:mm:ss.SSS} [%p] %t %c %m%n
    
    
        
                                 fileName="${LOG_HOME}/mybatis-sql.log"
                                 
                                 filePattern="${LOG_HOME}/mybatis-sql-%d{yyyy-MM-dd}.log"
                                 append="true">
		    
		    	
				
			
            
            
                
										 日志分割大小设置
                
            
        
    
    
    
          
            
          
    

2.自定义过滤器ClassPathFilter

import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.Marker;
import org.apache.logging.log4j.core.Filter;
import org.apache.logging.log4j.core.LogEvent;
import org.apache.logging.log4j.core.Logger;
import org.apache.logging.log4j.core.config.Node;
import org.apache.logging.log4j.core.config.plugins.Plugin;
import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
import org.apache.logging.log4j.core.config.plugins.PluginFactory;
import org.apache.logging.log4j.core.filter.AbstractFilter;
import org.apache.logging.log4j.message.Message;
//自定义标签插件注解,分别为标签名,节点类型,元素类型,是否打印
@Plugin(name = "ClassPathFilter", category = Node.CATEGORY,elementType = Filter.ELEMENT_TYPE,printObject = true)
public class ClassPathFilter extends AbstractFilter {
	
	private final String pathName;
	
	private ClassPathFilter(String pathName,Result onMatch, Result onMismatch){
		super(onMatch, onMismatch);
		this.pathName=pathName;
	}
    /**重写所有标签级别的过滤器,走自定义过滤规则,获取loggerName就是输出日志的对象的全名*/
    public Result filter(Logger logger, Level level, Marker marker, String msg, Object... params) {
    	return filter(logger.getName());
    }
 
    public Result filter(Logger logger, Level level, Marker marker, Object msg, Throwable t) {
    	return filter(logger.getName());
    }
 
    public Result filter(Logger logger, Level level, Marker marker, Message msg, Throwable t) {
    	return filter(logger.getName());
    }
 
    @Override
    public Result filter(LogEvent event) {
    	return filter(event.getLoggerName());
    }
    
    //@PluginAttribute注解可以获取到xml中配置的属性值
    @PluginFactory
    public static ClassPathFilter createFilter(@PluginAttribute("pathName") String pathName,
					@PluginAttribute("onMatch") final Result match,
					@PluginAttribute("onMismatch") final Result mismatch) {
        return new ClassPathFilter(pathName, match, mismatch);
    }
    //自定义过滤规则
	 public Result filter(String loggerName) {
		 if(loggerName!=null&&loggerName.indexOf(this.pathName)==-1){
			 return onMismatch;
		 }
		 return onMatch;
	 }
}

自此配置完毕,重启项目去tomcat/logs/目录下看生成的日志吧。

你可能感兴趣的:(零散知识点)