SpringBoot系列之切換log4j日誌框架

SpringBoot系列之使用切換log4j日誌框架html

ok,在pom文件右鍵->Diagrams->show Dependencies....,如圖,找到spring-boot-starter-logging,能夠看到SpringBoot的日誌實現默認依賴與logback,ok,若是你對這些知識不是很理解的,建議先看我Springboot專欄的日誌系列博客:https://smilenicky.blog.csdn.net/category_9195353.htmlweb

本博客要實現的是切換默認日誌框架爲log4j,固然是不建議這樣作的,由於log4j有性能問題,因此其做者纔開發了logback,不過做爲學習的話,仍是能夠學一下怎麼切換Springboot默認的日誌框架spring

先去slf4j官網拿一張圖:圖示,切換日誌框架,爲了不衝突,通常都是先排除日誌框架的實現jar,而後再將以前博客提到的偷樑換柱jar,好比log4j-to-slf4j.jar等等先排除,而後再引入對應的日誌實現jar,如圖所示的slf4j-log4j12.jar,由於本博客並不是入門教程,因此學習以前請先參考我以前Springboot日誌方面的博客,再來學習
在這裏插入圖片描述
ok,基於slf4j官方提供的知識,咱們就能夠實踐了,首先選中logback-classic.jar(logback實現jar)、log4j-to-slf4j.jar(將log4j API強制切換回slf4j的偷樑換柱jar),而後右鍵,選擇exclusion
在這裏插入圖片描述apache

ok,再次打開pom文件,能夠看到idea自動幫咱們exclusion一些jar了springboot

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <exclusion>
                    <artifactId>logback-classic</artifactId>
                    <groupId>ch.qos.logback</groupId>
                </exclusion>
                <exclusion>
                        <artifactId>log4j-to-slf4j</artifactId>
                    <groupId>org.apache.logging.log4j</groupId>
                </exclusion>
            </exclusions>
        </dependency>

ok,避免日誌衝突,exclusion了logback的實現jar和偷樑換柱的log4j-to-slf4j以後,咱們還須要引入log4j的實現jarapp

<dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
        </dependency>

ok,這是slf4j官網的說法,可是我發如今一些舊的版本SpringBoot是有提供spring-boot-starter-log4j這個場景啓動器的,因此咱們能夠更簡便的作log4j引入框架

直接exclusion spring-boot-starter-logging:ide

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

而後直接引入log4j的場景啓動器,建議加上版本,由於有些版本並無提供log4j配置,本博客是換回1.5.7才支持的,2.2.1的版本仲裁都沒提供對應版本的spring-boot

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-log4j</artifactId>
            <version>1.3.8.RELEASE</version>
        </dependency>

ok,而後在resources直接丟log4j.properties性能

# LOG4J rootCategory config
log4j.rootCategory=INFO, stdout, file
# LOG4J console config
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %5p %c{1}:%L - %m%n

# root日誌輸出
log4j.appender.file=org.apache.log4j.DailyRollingFileAppender
log4j.appender.file.file=logs/springboot.log
log4j.appender.file.DatePattern='.'yyyy-MM-dd
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %5p %c{1}:%L - %m%n

啓動SpringBoot日誌:
在這裏插入圖片描述

相關文章
相關標籤/搜索