SpringBoot整合Log4j2,记录日志到MongoDB数据库

在googel上看了一些经验和配置,都不能解决问题,记录此篇文章和经验。

目的,

1.使用Log4j2作为springboot的日志组件

2.记录日志到MongoDB

3.使用Lombok注解,简化代码

配置

1. Maven主要的依赖库

   提示:对于Log4j2,spring-data-mongodb-log4j 已经无用

 
            org.springframework.boot
            spring-boot-starter
            
                
                    org.springframework.boot
                    spring-boot-starter-logging
                
            
        
        	
		
		
		    org.apache.logging.log4j
		    log4j-mongodb3
		    2.12.0
		
		
		 
		
			org.springframework.boot
			spring-boot-starter-log4j2
		
		
		
		
			  org.projectlombok
			  lombok
			  1.18.8
		

 2. 在springboot里面引用Log4j2配置文件,logging.config=classpath:log4j2.xml

Log4j2配置文件中关于使用MongoDB的配置

  
            
      
            
        

3.使用Lombok的Log4j2注解

@RunWith(SpringRunner.class)
@SpringBootTest
@Log4j2(topic="Inventory_Module")
public class InventoryManagerServiceTest {
	
	
	@Test
	public void test_reduceAmountOfProduct() {
		
		log.info("---------this is reduceAmountOfProduct----------");
		log.debug("---------debug test for reduceAmountOfProduct----------");
	}

}

什么,你不会安装Lombok,请自行google或者百度。

更多详情可以参考开源代码仓库github:log4j2-mongodb

你可能感兴趣的:(springboot,JAVA大系)