log4jdbc-log4j2-jdbc4.1仅使用笔记

简介
log4jdbc是一个JDBC驱动器,能够记录SQL日志和SQL执行时间等信息。log4jdbc使用SLF4J(Simple Logging Facade)作为日志系统。

特性:

  1. 支持JDBC3和JDBC4。
  2. 支持现有大部分JDBC驱动。
  3. 易于配置(在大部分情况下,只需要改变驱动类名并在jdbc url前加上jdbc:log4,设置好日志输出级别)。
  4. 能够自动把SQL变量值加到SQL输出日志中,改进易读性和方便调试。
  5. 能够快速标识出应用程序中执行比较慢的SQL语句。
  6. 能够生成SQL连接数信息帮助识别连接池/线程问题

Log4jdbc-log4j2版本选择

~~log4jdbc-log4j2-jdbc3.jar for JDBC 3 support in JDK 1.5
log4jdbc-log4j2-jdbc4.jar for JDBC 4 support in JDK 1.6~~ 
log4jdbc-log4j2-jdbc4.1.jar for JDBC 4.1 support in JDK 1.7~1.8

引入依赖

<dependencies>
	<dependency>
        <groupId>org.springframework.bootgroupId>
         <artifactId>spring-boot-starter-jdbcartifactId>
     dependency>
     <dependency>
         <groupId>org.bgee.log4jdbc-log4j2groupId>
         <artifactId>log4jdbc-log4j2-jdbc4.1artifactId>
         <version>1.16version>
     dependency>
     
  	 <dependency>
         <groupId>org.slf4jgroupId>
          <artifactId>slf4j-apiartifactId>
          <version>1.7.25version>
      dependency>
     <dependency>
         <groupId>ch.qos.logbackgroupId>
         <artifactId>logback-classicartifactId>
         <version>1.1.11version>
     dependency>
dependencies>

数据库连接驱动

# 数据库驱动:
spring.datasource.driver-class-name=net.sf.log4jdbc.sql.jdbcapi.DriverSpy
# 数据源名称
#spring.datasource.name=defaultDataSource
# 数据库连接地址
spring.datasource.url=jdbc:log4jdbc:mysql://localhost:3306/databaseName?useSSL=false&serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true
# 数据库用户名&密码:
spring.datasource.username=root
spring.datasource.password=root

添加配置
log4jdbc-log4j2-jdbc4.1仅使用笔记_第1张图片

添加log4jdbc.log4j2.properties配置文档

# If you use SLF4J. First, you need to tell log4jdbc-log4j2 that you want to use the SLF4J logger
# mapper根路径
log4jdbc.debug.stack.prefix=com.project.system.api.mapper
log4jdbc.spylogdelegator.name=net.sf.log4jdbc.log.slf4j.Slf4jSpyLogDelegator
log4jdbc.auto.load.popular.drivers=false
log4jdbc.drivers=com.mysql.cj.jdbc.Driver
log4jdbc.dump.booleanastruefalse=true
log4jdbc.dump.sql.addsemicolon=true

修改logback.xml


<configuration scan="true" scanPeriod="30 seconds" debug="false">
    
    <contextName>plojectNamecontextName>
    <property name="LOG_HOME" value="logs/"/>
    <property name="log.charset" value="utf-8"/>
    <property name="log.pattern"
              value="%black(%contextName-) %red(%d{yyyy-MM-dd HH:mm:ss}) %green([%thread]) %highlight(%-5level) %boldMagenta(%logger{36}) - %gray(%msg%n)"/>
    
    <appender name="console" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>${log.pattern}pattern>
            <charset>${log.charset}charset>
        encoder>
    appender>

    
    <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <FileNamePattern>${LOG_HOME}/server.log.%d{yyyy-MM-dd-HH}FileNamePattern>
            <MaxHistory>60MaxHistory>
        rollingPolicy>
        <encoder>
            <pattern>[%level] %d{yyyy-MM-dd HH:mm:ss.SSS} %X{logId} %c.java - %msg%npattern>
            <charset>utf-8charset>
        encoder>
    appender>


    
    <root level="info">
        
        <appender-ref ref="console"/>
        
        <appender-ref ref="FILE"/>
    root>

    
    
    <logger name="jdbc.resultsettable" level="INFO" additivity="false">
        <appender-ref ref="console"/>
    logger>

    
    <logger name="jdbc.sqltiming" level="INFO" additivity="false">
        <appender-ref ref="console"/>
    logger>

    
    <logger name="jdbc.sqlonly" level="OFF" additivity="false">
        <appender-ref ref="console"/>
    logger>

    
    <logger name="jdbc.resultset" level="ERROR" additivity="false">
        <appender-ref ref="console"/>
    logger>

    
    <logger name="jdbc.connection" level="OFF" additivity="false">
        <appender-ref ref="console"/>
    logger>

    
    <logger name="jdbc.audit" level="OFF" additivity="false">
        <appender-ref ref="console"/>
    logger>

configuration>

你可能感兴趣的:(Java,日志,java,log4j)