spring Boot 集成log4j2

1. 表结构:

CREATE TABLE `log_table` (
  `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '主键id',
  `log_date` datetime DEFAULT NULL COMMENT '日志日期时间',
  `log_level` varchar(32) DEFAULT NULL COMMENT '日志级别',
  `log_class` varchar(255) DEFAULT NULL COMMENT '日志所在的类',
  `line_number` int(255) DEFAULT NULL COMMENT '在类中的行数',
  `log_detail` longtext COMMENT '日志详情',
  `log_module` varchar(255) DEFAULT NULL COMMENT '标识,多个项目的时候,区分一下',
  PRIMARY KEY (`id`)
) 

2. 引入依赖的jar

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

            log4j
            log4j
            1.2.17
        
        
            mysql
            mysql-connector-java
            5.1.39
        

3. 数据源:

package com.cn.log;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;
import org.apache.log4j.jdbc.JDBCAppender;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

public class Log2ConnectionFactory extends JDBCAppender {

    //Druid数据源
    private DruidDataSource dataSource1;

    private static Log2ConnectionFactory logConnectionFactory;

    public Log2ConnectionFactory() {
        super();
    }

    @Override
    protected void closeConnection(Connection con) {
        if (con != null) {
            try {
                con.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    protected Connection getConnection() throws SQLException {
        if (dataSource1 == null) {
            Properties result = new Properties();
            result.put("driverClassName", "com.mysql.jdbc.Driver");
            result.put("url", "jdbc:mysql://192.168.0.109:3309/test?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&useSSL=false&allowMultiQueries=true");
            result.put("username", "root");
            result.put("password", "root");
            try {
                dataSource1 = (DruidDataSource) DruidDataSourceFactory.createDataSource(result);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return dataSource1.getConnection();
    }

    public static Connection getDataSourceConnection() throws SQLException {
        if (logConnectionFactory == null) {
            logConnectionFactory = new Log2ConnectionFactory();
        }
        return logConnectionFactory.getConnection();
    }

}

 

4. log4j2.xml文件配置:



    
        
        /log
        
        log_table
    

    
        
        
            
        

        
        
            
            
        

        
        
            
            
            
                
                
            
        

        
        
            
            
            
            
            
            
            
            
            
            
            
            
            
        
    


    
        
            
            
            
        
        
        
            
        
    

5. 测试:

package com.cn.log;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping(value = "/game-api/demo")
public class DemoController {

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

    @GetMapping(value = "/test")
    public void test() {
        logger.error("hi,cn_yang");
    }

}

6.效果:

spring Boot 集成log4j2_第1张图片

你可能感兴趣的:(java)