springboot整合log4j2

github地址:https://github.com/JackrayWang/springbootshiro

项目目录如下:
springboot整合log4j2_第1张图片
pom特别注意,在由spring-boot的依赖中默认添加了日志,需要去掉之后log4j2才可以生效,否则包冲突。


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


    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.1.1.RELEASE
         
    
    com.jakcray
    springbootshiro
    0.0.1-SNAPSHOT
    springbootshiro
    Demo project for Spring Boot

    
        1.8
    

    
        
        
            org.springframework.boot
            spring-boot-starter-jdbc
            
                
                    org.springframework.boot
                    spring-boot-starter-logging
                
            
        
        
            org.springframework.boot
            spring-boot-starter-thymeleaf
            
                
                    org.springframework.boot
                    spring-boot-starter-logging
                
            
        
        
            org.springframework.boot
            spring-boot-starter-web
            
                
                    org.springframework.boot
                    spring-boot-starter-logging
                
            
        
        
            org.springframework.boot
            spring-boot-starter-websocket
            
                
                    org.springframework.boot
                    spring-boot-starter-logging
                
            
        
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            1.3.2
            
                
                    org.springframework.boot
                    spring-boot-starter-logging
                
            
        

        
            mysql
            mysql-connector-java
            runtime
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
            
                
                    org.springframework.boot
                    spring-boot-starter-logging
                
            
        

        
        
            com.alibaba
            druid
            1.1.12
        

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

    

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

            
                org.mybatis.generator
                mybatis-generator-maven-plugin
                1.3.2
                
                    ${basedir}/src/main/resources/generator/generatorConfig.xml
                    true
                    true
                
            

        
    

log4j2.xml



    
        [%d{yyyy-MM-dd'T'HH:mm:ss,SSSXXX}] %-5p [%t] %c{2} - %m%n]
        
        
        E:/logs
    


    
        
            
        
        
            
        

        

        
            
            
            
            
            
                
                

                
                
            
        

        
            
            
            
            
            
                
                

                
                
            
        

        
            
            
            
            
            
                
                

                
                
            
        

        
            
            
            
            
            
                
                

                
                
            
        

    

    
        
            
            
            
            
            
        
    


pom引入log4j2.xml

spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/xq?useUnicode=true&characterEncoding=UTF-8&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
spring.datasource.username=xq
spring.datasource.password=xq123@123
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.max-idle=10
spring.datasource.max-wait=10000
spring.datasource.min-idle=5
spring.datasource.initial-size=5

#mybatis.config-location=classpath:mybatis-config.xml
#mybatis mapper文件的位置
mybatis.mapper-locations=classpath:mapper/*Mapper.xml
#扫描pojo类的位置,在此处指明扫描实体类的包,在mapper中就可以不用写pojo类的全路径名了
mybatis.type-aliases-package=com.jackray.springbootshiro.mybatisutils.mapper

logging.config=classpath:log4j2/log4j2.xml

##jsp##
#spring.mvc.view.prefix=/jsp/
#spring.mvc.view.suffix=.html

controller

package com.jakcray.springbootshiro.controller;

import com.jakcray.springbootshiro.mybatisutils.dao.UserDAO;
import com.jakcray.springbootshiro.mybatisutils.mapper.UserDAOMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController {

    @Autowired
    UserDAOMapper userDAOMapper;


    private static final Logger logger = LoggerFactory.getLogger(TestController.class.getName());

    @RequestMapping(value = "/test",method = RequestMethod.GET)
    public String Test(){

        UserDAO userDAO = userDAOMapper.selectByPrimaryKey("12345");

        logger.info("success");


//        System.out.println(userDAO.getEmail());
//
//        System.out.println("success");

        return "big success";
    }

}

test

访问:http://localhost:8080/test

控制台打印日志
springboot整合log4j2_第2张图片

log日志文件目录
springboot整合log4j2_第3张图片
日志文件打印日志(写入文件)
springboot整合log4j2_第4张图片

你可能感兴趣的:(springboot)