2、springboot 集成 log4j2

1、Spring boot默认是使用logback来进行日志记录,而大多数框架依赖log4j,即将logback替换为log4j2

2、springboot 集成 log4j2_第1张图片

 

2、引入log4j2 的maven依赖

 

2、springboot 集成 log4j2_第2张图片

 

3、如果引入springboot 其他组件的自动启动包,也要注意是否默认启用了logback ,例如aop启动包也需要去除logback依赖

2、springboot 集成 log4j2_第3张图片

4、配置log4j2的启动参数

2、springboot 集成 log4j2_第4张图片

配置文件放在src/main/java/application 下,命名为log4j2.xml ,项目启动时,log4j2会自动到这个目录加载配置文件
也可以在application.properties 中配置路径:logging.config=classpath:log4j2.xml

***配置文件中加了详细的配置说明,具体参数不再重复解释;

      log4j2 配置文件(xml)

xml version="1.0" encoding="UTF-8"?>

<configuration status="debug">

    

    <appenders>

 

        <Console name="Console" target="SYSTEM_OUT">

            

            <ThresholdFilter level="info" onMatch="ACCEPT" onMismatch="DENY"/>

            <PatternLayout pattern="[%d{HH:mm:ss.SSS}] %-5level %class{36} %L %M - %msg%xEx%n"/>

        Console>

 

        

        

        <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>

                

                <TimeBasedTriggeringPolicy interval="1" modulate="true"/>

                

                <SizeBasedTriggeringPolicy size="200 MB"/>

 

            Policies>

        RollingFile>

 

        

        <RollingFile name="RollingFileInfo" fileName="./logs/info.log"

                     filePattern="logs/$${date:yyyy-MM}/info-%d{yyyy-MM-dd}-%i.log.gz">

            <Filters>

                

                <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>

                

                <TimeBasedTriggeringPolicy interval="1" modulate="true"/>

                

                <SizeBasedTriggeringPolicy size="200 MB"/>

            Policies>

        RollingFile>

 

        

        <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>

                

                <TimeBasedTriggeringPolicy interval="1" modulate="true"/>

                

                <SizeBasedTriggeringPolicy size="200 MB"/>

            Policies>

        RollingFile>

 

        

        <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>

                

                <TimeBasedTriggeringPolicy interval="1" modulate="true"/>

                

                <SizeBasedTriggeringPolicy size="200 MB"/>

            Policies>

        RollingFile>

 

        

      

    appenders>

 

    <loggers>

        

        

        <root level="all">

            <appender-ref ref="Console" level="info"/>

            <appender-ref ref="RollingFileInfo" level="info"/>

            <appender-ref ref="RollingFileWarn" level="warn"/>

            <appender-ref ref="RollingFileError" level="error"/>

            <appender-ref ref="RollingFileDebug" level="debug"/>

        root>

 

        

     

 

 

        

        

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

        <Logger name="org.mybatis" level="debug"/>

        <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>

configuration>

 

你可能感兴趣的:(Spring,系列)