Druid中使用log4j2进行日志输出

  1. pom.xml中springboot版本依赖
org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-logging org.springframework.boot spring-boot-starter-log4j2 com.alibaba druid-spring-boot-starter 1.1.6
  1. log4j2.xml文件中的日志配置(完整,可直接拷贝使用)
    <Console name="Console" target="SYSTEM_OUT">
        <!--只接受程序中DEBUG级别的日志进行处理-->
        <ThresholdFilter level="DEBUG" onMatch="ACCEPT" onMismatch="DENY"/>
        <PatternLayout pattern="[%d{HH:mm:ss.SSS}] %-5level %class{36} %L %M - %msg%xEx%n"/>
    </Console>

    <!--处理DEBUG级别的日志,并把该日志放到logs/debug.log文件中-->
    <!--打印出DEBUG级别日志,每次大小超过size,则这size大小的日志会自动存入按年份-月份建立的文件夹下面并进行压缩,作为存档-->
    <RollingFile name="RollingFileDebug" fileName="./logs/debug.log"
                 filePattern="logs/$${date:yyyy-MM}/debug-%d{yyyy-MM-dd}-%i.log.gz">
        <Filters>
            <ThresholdFilter level="DEBUG"/>
            <ThresholdFilter level="INFO" onMatch="DENY" onMismatch="NEUTRAL"/>
        </Filters>
        <PatternLayout
                pattern="[%d{yyyy-MM-dd HH:mm:ss}] %-5level %class{36} %L %M - %msg%xEx%n"/>
        <Policies>
            <SizeBasedTriggeringPolicy size="500 MB"/>
            <TimeBasedTriggeringPolicy/>
        </Policies>
    </RollingFile>

    <!--处理INFO级别的日志,并把该日志放到logs/info.log文件中-->
    <RollingFile name="RollingFileInfo" fileName="./logs/info.log"
                 filePattern="logs/$${date:yyyy-MM}/info-%d{yyyy-MM-dd}-%i.log.gz">
        <Filters>
            <!--只接受INFO级别的日志,其余的全部拒绝处理-->
            <ThresholdFilter level="INFO"/>
            <ThresholdFilter level="WARN" onMatch="DENY" onMismatch="NEUTRAL"/>
        </Filters>
        <PatternLayout
                pattern="[%d{yyyy-MM-dd HH:mm:ss}] %-5level %class{36} %L %M - %msg%xEx%n"/>
        <Policies>
            <SizeBasedTriggeringPolicy size="500 MB"/>
            <TimeBasedTriggeringPolicy/>
        </Policies>
    </RollingFile>

    <!--处理WARN级别的日志,并把该日志放到logs/warn.log文件中-->
    <RollingFile name="RollingFileWarn" fileName="./logs/warn.log"
                 filePattern="logs/$${date:yyyy-MM}/warn-%d{yyyy-MM-dd}-%i.log.gz">
        <Filters>
            <ThresholdFilter level="WARN"/>
            <ThresholdFilter level="ERROR" onMatch="DENY" onMismatch="NEUTRAL"/>
        </Filters>
        <PatternLayout
                pattern="[%d{yyyy-MM-dd HH:mm:ss}] %-5level %class{36} %L %M - %msg%xEx%n"/>
        <Policies>
            <SizeBasedTriggeringPolicy size="500 MB"/>
            <TimeBasedTriggeringPolicy/>
        </Policies>
    </RollingFile>

    <!--处理error级别的日志,并把该日志放到logs/error.log文件中-->
    <RollingFile name="RollingFileError" fileName="./logs/error.log"
                 filePattern="logs/$${date:yyyy-MM}/error-%d{yyyy-MM-dd}-%i.log.gz">
        <ThresholdFilter level="ERROR"/>
        <PatternLayout
                pattern="[%d{yyyy-MM-dd HH:mm:ss}] %-5level %class{36} %L %M - %msg%xEx%n"/>
        <Policies>
            <SizeBasedTriggeringPolicy size="500 MB"/>
            <TimeBasedTriggeringPolicy/>
        </Policies>
    </RollingFile>

    <!--druid的日志记录追加器-->
    <RollingFile name="druidSqlRollingFile" fileName="./logs/druid-sql.log"
                 filePattern="logs/$${date:yyyy-MM}/api-%d{yyyy-MM-dd}-%i.log.gz">
        <PatternLayout pattern="[%d{yyyy-MM-dd HH:mm:ss}] %-5level %L %M - %msg%xEx%n"/>
        <Policies>
            <SizeBasedTriggeringPolicy size="500 MB"/>
            <TimeBasedTriggeringPolicy/>
        </Policies>
    </RollingFile>
</appenders>

<loggers>
    <root level="DEBUG">
        <appender-ref ref="Console"/>
        <appender-ref ref="RollingFileInfo"/>
        <appender-ref ref="RollingFileWarn"/>
        <appender-ref ref="RollingFileError"/>
        <appender-ref ref="RollingFileDebug"/>
    </root>

    <!--记录druid-sql的记录-->
    <logger name="druid.sql.Statement" level="debug" additivity="false">
        <appender-ref ref="druidSqlRollingFile"/>
    </logger>
    <logger name="druid.sql.Statement" level="debug" additivity="false">
        <appender-ref ref="druidSqlRollingFile"/>
    </logger>

    <!--log4j2 自带过滤日志-->
    <Logger name="org.apache.catalina.startup.DigesterFactory" level="error" />
    <Logger name="org.apache.catalina.util.LifecycleBase" level="error" />
    <Logger name="org.apache.coyote.http11.Http11NioProtocol" level="warn" />
    <logger name="org.apache.sshd.common.util.SecurityUtils" level="warn"/>
    <Logger name="org.apache.tomcat.util.net.NioSelectorPool" level="warn" />
    <Logger name="org.crsh.plugin" level="warn" />
    <logger name="org.crsh.ssh" level="warn"/>
    <Logger name="org.eclipse.jetty.util.component.AbstractLifeCycle" level="error" />
    <Logger name="org.hibernate.validator.internal.util.Version" level="warn" />
    <logger name="org.springframework.boot.actuate.autoconfigure.CrshAutoConfiguration" level="warn"/>
    <logger name="org.springframework.boot.actuate.endpoint.jmx" level="warn"/>
    <logger name="org.thymeleaf" level="warn"/>
</loggers>
3. 配置application.properties

配置日志输出

spring.datasource.druid.filter.slf4j.enabled=true
spring.datasource.druid.filter.slf4j.statement-create-after-log-enabled=false
spring.datasource.druid.filter.slf4j.statement-close-after-log-enabled=false
spring.datasource.druid.filter.slf4j.result-set-open-after-log-enabled=false
spring.datasource.druid.filter.slf4j.result-set-close-after-log-enabled=false

上一篇
评论
说点什么吧?

发表评论

取消回复