Springboot系列-集成Log4j2日志框架

在pom配置文件引入依赖

	
		
			
				org.springframework.boot
				spring-boot-dependencies
				2.1.6.RELEASE
				pom
				import
			
		
	
		
			org.springframework.boot
			spring-boot-starter-web
			
				
					org.springframework.boot
					spring-boot-starter-logging
				
			
		

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

		
			org.apache.logging.log4j
			log4j-web
			2.11.2
		

在application.yml配置文件中执行日志配置文件

logging:
  config: classpath:log4j2.xml

log4j详细配置





    
    
        %d{yyyy-MM-dd HH:mm:ss,SSS} %-5p %c{1}:%L -%m%n
        EurekaServer
    
    
        
            
                ${pattern}
            
        
        
        
            
            
            
                ${pattern}
            
        
        
        
            
                ${pattern}
            
            
            
        
    
    
        
        
        
        
            
            
            
        
    

使用

package com.springboot.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by Administrator on 2019/7/15 0015.
 */
@Api(description = "Demo控制层")
@RestController
public class DemoController {
    private static final Logger logger= LoggerFactory.getLogger(DemoController.class);
    @ApiOperation(value = "swagger demo",notes = "实例")
    @PostMapping(value="hello")
    public void hello(@ApiParam(value = "用户名",name = "name",required = true) @RequestParam(value = "name",required = true) String name,
                      @ApiParam(value = "密码",name = "pwd",required = true) @RequestParam(value = "pwd",required = true) String pwd){
        logger.info("姓名{},密码{}",name,pwd);
    }
}

示例代码下载

https://gitee.com/liyongfu/sprinhgboot-log4j2.git

你可能感兴趣的:(SpringBoot)