SpringBoot基础实战-第八篇-日志-切换日志框架

1 slf4j+logback切换到slf4j+log4j

打开pom.xml查看架构树

SpringBoot基础实战-第八篇-日志-切换日志框架_第1张图片

Shift+Delete把logback-classic排除:

SpringBoot基础实战-第八篇-日志-切换日志框架_第2张图片

 

在pom.xml中添加依赖


  org.slf4j
  slf4j-log4j12

此时的架构图:

SpringBoot基础实战-第八篇-日志-切换日志框架_第3张图片

此时完整pom.xml



	4.0.0
	
		org.springframework.boot
		spring-boot-starter-parent
		2.1.3.RELEASE
		 
	
	com.wsc
	springboot-logging
	0.0.1-SNAPSHOT
	springboot-logging
	Demo project for Spring Boot

	
		1.8
	

	
		
			org.springframework.boot
			spring-boot-starter-web
			
				
					logback-classic
					ch.qos.logback
				
			
		

		
			org.slf4j
			slf4j-log4j12
		
		
			org.springframework.boot
			spring-boot-starter-test
			test
		
	

	
		
			
				org.springframework.boot
				spring-boot-maven-plugin
			
		
	


在resources下创建log4j.properties,内容如下:

### set log levels ###
log4j.rootLogger = debug ,  stdout ,  D ,  E

### 输出到控制台 ###
log4j.appender.stdout = org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target = System.out
log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern =  %d{ABSOLUTE} ==wsc=== %5p %c{ 1 }:%L - %m%n

#### 输出到日志文件 ###
#log4j.appender.D = org.apache.log4j.DailyRollingFileAppender
#log4j.appender.D.File = logs/log.log
#log4j.appender.D.Append = true
#log4j.appender.D.Threshold = DEBUG ## 输出DEBUG级别以上的日志
#log4j.appender.D.layout = org.apache.log4j.PatternLayout
#log4j.appender.D.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss}  [ %t:%r ] - [ %p ]  %m%n
#
#### 保存异常信息到单独文件 ###
#log4j.appender.D = org.apache.log4j.DailyRollingFileAppender
#log4j.appender.D.File = logs/error.log ## 异常日志文件名
#log4j.appender.D.Append = true
#log4j.appender.D.Threshold = ERROR ## 只输出ERROR级别以上的日志!!!
#log4j.appender.D.layout = org.apache.log4j.PatternLayout
#log4j.appender.D.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss}  [ %t:%r ] - [ %p ]  %m%n

运行测试方法contextLoads()

切换成功

 

2 slf4j+logback切换到slf4j+log4j2

pom.xml中的内容如下:



	4.0.0
	
		org.springframework.boot
		spring-boot-starter-parent
		2.1.3.RELEASE
		 
	
	com.wsc
	springboot-logging
	0.0.1-SNAPSHOT
	springboot-logging
	Demo project for Spring Boot

	
		1.8
	

	
		
			org.springframework.boot
			spring-boot-starter-web
			
		

		
			org.springframework.boot
			spring-boot-starter-test
			test
		
	

	
		
			
				org.springframework.boot
				spring-boot-maven-plugin
			
		
	


打开架构树,排除spring-boot-starter-logging

SpringBoot基础实战-第八篇-日志-切换日志框架_第4张图片

再在pom.xml中添加依赖、

  
	org.springframework.boot
	spring-boot-starter-log4j2
  

完整pom.xml



	4.0.0
	
		org.springframework.boot
		spring-boot-starter-parent
		2.1.3.RELEASE
		 
	
	com.wsc
	springboot-logging
	0.0.1-SNAPSHOT
	springboot-logging
	Demo project for Spring Boot

	
		1.8
	

	
		
			org.springframework.boot
			spring-boot-starter-web
			
				
					spring-boot-starter-logging
					org.springframework.boot
				
			

		

		
			org.springframework.boot
			spring-boot-starter-test
			test
		
		
			org.springframework.boot
			spring-boot-starter-log4j2
		
	

	
		
			
				org.springframework.boot
				spring-boot-maven-plugin
			
		
	


 

在resources下创建log4j2.xml






    
    
        
        
            
            
        
        
        
            
        
        
        
            
            
            
            
                
                
            
        
        
            
            
            
                
                
            
            
            
        
        
            
            
            
                
                
            
        
    
    
    
        
        
        
        
            
            
            
            
        
    

运行测试方法contextLoads()

 

 

 

你可能感兴趣的:(SpringBoot基础)