springboot log4j的坑

litblank發表於2018-08-14

參考連線

blog.csdn.net/huangyaa729…

1、坑,pom.xml

哪種啟動方式

web啟動這種方式,不需要去掉引用,

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-api</artifactId>
    <version>2.8.2</version>
</dependency>
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <version>1.7.25</version>
</dependency>

複製程式碼

2、坑,版本衝突,版本不支援

1.3.8以後的spring boot版本對log4j不支援

3、log4j.xml不載入

可以在application.properties檔案中指定: logging.config=classpath:log4j2.xml ;

log4j.xml

<configuration>
    <!-- %m輸出的資訊,%p日誌級別,%t執行緒名,%d日期,%c類的全名,,,, -->
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%d %p (%file:%line\)- %m%n</pattern>
            <charset>UTF-8</charset>
        </encoder>
    </appender>
    <appender name="baselog"
              class="ch.qos.logback.core.rolling.RollingFileAppender">
        <File>log/base.log</File>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>./log/base.log.%d.%i</fileNamePattern>
            <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                <!-- or whenever the file size reaches 32 MB -->
                <maxFileSize>32 MB</maxFileSize>
            </timeBasedFileNamingAndTriggeringPolicy>
        </rollingPolicy>
        <encoder>
            <pattern>
                %d %p (%file:%line\)- %m%n
            </pattern>
            <charset>UTF-8</charset> <!-- 此處設定字符集 -->
        </encoder>
    </appender>
    <root level="info">
        <appender-ref ref="STDOUT"/>
    </root>
    <logger name="com.**" level="DEBUG">   <!-- 監控的包名,自己的包結構,在最後我會貼出我的包結構-->
        <appender-ref ref="baselog"/>
    </logger>
</configuration>
複製程式碼

相關文章