log4j2示例:将sql相关日志记录到指定文件

log4j2.xml配置文件






<configuration status="WARN" monitorInterval="30">

    <properties>
        <property name="logPath">/opt/applog/property>
    properties>

    
    <Appenders>
        
        <Console name="Console" target="SYSTEM_OUT">
            
            <PatternLayout pattern="[%d{yyyy-MM-dd HH:mm:ss}] [%p] - %l - %m%n"/>
        Console>

        
        <RollingFile name="root" fileName="${sys:user.home}/logs/root/root.log"
                     filePattern="${sys:user.home}/logs/root/root-%d{yyyy-MM-dd}-%i.log">
            <PatternLayout pattern="[%d{yyyy-MM-dd HH:mm:ss}] [%p] - %l%n%m%n"/>
            <Policies>
                
                <TimeBasedTriggeringPolicy modulate="true" interval="1"/>
                
                
            Policies>
            
            <DefaultRolloverStrategy max="20"/>
        RollingFile>

        
        <RollingFile name="root-error" fileName="${sys:user.home}/logs/root/root-error.log"
                     filePattern="${sys:user.home}/logs/root/root-error-%d{yyyy-MM-dd}-%i.log">
            
            <ThresholdFilter level="error" onMatch="ACCEPT" onMismatch="DENY"/>
            <PatternLayout pattern="[%d{yyyy-MM-dd HH:mm:ss}] [%p] - %l%n%m%n"/>
            <Policies>
                <TimeBasedTriggeringPolicy modulate="true" interval="1"/>
            Policies>
            <DefaultRolloverStrategy max="20"/>
        RollingFile>

        
        <RollingFile name="sql" fileName="${sys:user.home}/logs/sql/sql.log"
                     filePattern="${sys:user.home}/logs/sql/sql-%d{yyyy-MM-dd}-%i.log">
            <PatternLayout pattern="[%d{yyyy-MM-dd HH:mm:ss}] [%p] - %l%n%m%n"/>
            <Policies>
                <TimeBasedTriggeringPolicy modulate="true" interval="1"/>
            Policies>
            <DefaultRolloverStrategy max="20"/>
        RollingFile>

    Appenders>

    <Loggers>
        
        
        <Root level="ALL">
            <AppenderRef ref="Console"/>
            <AppenderRef ref="root"/>
            <AppenderRef ref="root-error"/>
        Root>
        
        


        
        <Logger name="org.springframework" level="INFO"/>
        <Logger name="org.mybatis" level="INFO"/>


        
        <Logger name="java.sql.Connection" level="DEBUG" additivity="true">
            <AppenderRef ref="sql" />
        Logger>
        <Logger name="java.sql.PreparedStatement" level="DEBUG" additivity="true">
            <AppenderRef ref="sql" />
        Logger>
        <Logger name="java.sql.ResultSet" level="INFO" additivity="true">
            <AppenderRef ref="sql" />
        Logger>
        <Logger name="java.sql.Statement" level="INFO" additivity="true">
            <AppenderRef ref="sql" />
        Logger>
        <Logger name="org.apache.ibatis" level="TRACE" additivity="true">
            <AppenderRef ref="sql" />
        Logger>
        <Logger name="org.mybatis.spring" level="TRACE" additivity="true">
            <AppenderRef ref="sql" />
        Logger>
        <Logger name="org.springframework.jdbc.datasource.DataSourceTransactionManager" level="INFO" additivity="true">
            <AppenderRef ref="sql" />
        Logger>
        <Logger name="org.springframework.transaction.jta.WebSphereUowTransactionManager" level="INFO" additivity="true">
            <AppenderRef ref="sql" />
        Logger>
        <Logger name="org.springframework.boot.logging" level="DEBUG" additivity="true">
            <AppenderRef ref="sql" />
        Logger>
        <Logger name="me.jtzen9.demostudy.dao" level="TRACE" additivity="true">
            <AppenderRef ref="sql" />
        Logger>

    Loggers>

configuration>

相关代码

---------------------------------------------------------------
@RestController
@RequestMapping("/test")
public class TestController {

    @Autowired
    private TestService service;

    @GetMapping("/getSysConstants")
    public List<SysConstant> getSysConstants(){
        return service.getSysConstants();
    }
}
---------------------------------------------------------------
@Service
public class TestService {

    @Autowired
    private ITestDao dao;

    public List<SysConstant> getSysConstants(){
        return dao.getSysConstants();
    }
}
---------------------------------------------------------------
public interface ITestDao {
    public List<SysConstant> getSysConstants();
}
---------------------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="me.jtzen9.demostudy.dao.ITestDao">

    <select id="getSysConstants" resultType="me.jtzen9.demostudy.entity.SysConstant">
        select * from tbl_sysconstant
    </select>
</mapper>
---------------------------------------------------------------
@SpringBootApplication
@MapperScan("me.jtzen9.demostudy.dao")
public class DemoStudyApplication {
    public static void main(String[] args) {
        SpringApplication.run(DemoStudyApplication.class, args);
    }
}

application.properties


#spring.main.show-banner=false
spring.main.banner-mode=off

# 开发环境 配置
server.port=9085

# jdbc
spring.datasource.driver-class-name=oracle.jdbc.OracleDriver
spring.datasource.url=jdbc:oracle:thin:@XXXX:1521:orcl
spring.datasource.username=XXX
spring.datasource.password=XXX

# XML文件位置 指定DAO与SQL语句关联
mybatis.mapperLocations=classpath:mapper/*.xml

pom文件


<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0modelVersion>
    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>1.5.19.RELEASEversion>
        <relativePath/> 
    parent>
    <groupId>me.jtzen9groupId>
    <artifactId>demo-studyartifactId>
    <version>0.0.1-SNAPSHOTversion>
    <packaging>jarpackaging>

    <name>demo-studyname>

    <properties>
        <java.version>1.8java.version>
        <oracle.version>11.2.0.4oracle.version>
        <mybatis-spring-boot-starter.version>1.3.1mybatis-spring-boot-starter.version>
    properties>

    <dependencies>
        
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starterartifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.bootgroupId>
                    <artifactId>spring-boot-starter-loggingartifactId>
                exclusion>
            exclusions>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-log4j2artifactId>
        dependency>

        
        <dependency>
            <groupId>org.mybatis.spring.bootgroupId>
            <artifactId>mybatis-spring-boot-starterartifactId>
            <version>${mybatis-spring-boot-starter.version}version>
        dependency>
        <dependency>
            <groupId>com.oraclegroupId>
            <artifactId>ojdbc6artifactId>
            <version>${oracle.version}version>
        dependency>

        
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>


    dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
            plugin>
        plugins>
    build>

project>

你可能感兴趣的:(Java开源组件库)