SringBoot啓動讀取不到logback-spring.xml配置

環境

IDEA Ultimate 2018.3
MAVEN 3.5.4
SpringBoot 2.0.2.RELEASE

背景

啓動springboot工程發現沒有打印日誌

分析

1、我們是根據不同環境進行的不同日誌配置,所以第一步需要在application.properties文件中添加

[email protected]@

然後在pom.xml文件中配置profile信息

 <profiles>
        <profile>
            <!-- development environment -->
            <id>dev</id>
            <properties>
                <profiles.active>dev</profiles.active>
            </properties>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>

        <!-- test environment -->
        <profile>
            <id>test</id>
            <properties>
                <profiles.active>test</profiles.active>
            </properties>
        </profile>

        <!-- Preproduction environment -->
        <profile>
            <id>pre</id>
            <properties>
                <profiles.active>pre</profiles.active>
            </properties>
        </profile>

        <!-- production environment -->
        <profile>
            <id>prod</id>
            <properties>
                <profiles.active>prod</profiles.active>
            </properties>
        </profile>
    </profiles>

2、配置logback-spring.xml
Spring Boot官方推薦優先使用帶有-spring的文件名作爲你的日誌配置(如使用logback-spring.xml,而不是logback.xml),命名爲logback-spring.xml的日誌配置文件,spring boot可以爲它添加一些spring boot特有的配置項,默認的命名規則,並且放在 src/main/resources 下面即可。

<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true" scanPeriod="60 seconds" debug="false">

    <contextName>xxx</contextName>

    <!-- test文件路徑 -->
    <property name="logback.appname" value="xxx"/>
    <property name="logback.logdir" value="/usr/logs/"/>

    <!-- pro文件路徑 -->
    <!--<property name="logback.logdir" value="/opt/logs" />-->

    <!--輸出到控制檯 ConsoleAppender-->
    <appender name="consoleLog" class="ch.qos.logback.core.ConsoleAppender">
        <!--展示格式 layout-->
        <layout class="ch.qos.logback.classic.PatternLayout">
            <pattern>%d{yyyy/MM/dd HH:mm:ss,SSS} [%p] [%t] %C.%M\(%F:%L\) - %msg%n</pattern>
                <!--<pattern>%d{HH:mm:ss.SSS} %contextName [%thread] %-5level %logger{36} - %msg%n</pattern>-->
            <!--</pattern>-->
        </layout>
    </appender>

    <!-- 輸出到文件 -->
    <appender name="fileInfoLog" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <!--如果只是想要 Info 級別的日誌,只是過濾 info 還是會輸出 Error 日誌,因爲 Error 的級別高,
        所以我們使用下面的策略,可以避免輸出 Error 的日誌-->
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <!--過濾 Error-->
            <level>ERROR</level>
            <!--匹配到就禁止-->
            <onMatch>DENY</onMatch>
            <!--沒有匹配到就允許-->
            <onMismatch>ACCEPT</onMismatch>
        </filter>

        <!--日誌名稱,如果沒有File 屬性,那麼只會使用FileNamePattern的文件路徑規則
            如果同時有<File>和<FileNamePattern>,那麼當天日誌是<File>,明天會自動把今天
            的日誌改名爲今天的日期。即,<File> 的日誌都是當天的。
        -->
        <File>${logback.logdir}/info.${logback.appname}.log</File>
        <!--滾動策略,按照時間滾動 TimeBasedRollingPolicy-->
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <!--文件路徑,定義了日誌的切分方式——把每一天的日誌歸檔到一個文件中,以防止日誌填滿整個磁盤空間-->
            <FileNamePattern>${logback.logdir}/info.${logback.appname}.%d{yyyy-MM-dd}.log</FileNamePattern>
            <!--只保留最近90天的日誌-->
            <maxHistory>15</maxHistory>
            <!--用來指定日誌文件的上限大小,那麼到了這個值,就會刪除舊的日誌-->
            <!--<totalSizeCap>1GB</totalSizeCap>-->
        </rollingPolicy>
        <!--日誌輸出編碼格式化-->
        <encoder>
            <charset>UTF-8</charset>
            <!--<pattern>%d [%thread] %-5level %logger{36} %line - %msg%n</pattern>-->
            <pattern>%d{yyyy/MM/dd HH:mm:ss,SSS} [%p] [%t] %C.%M\(%F:%L\) - %msg%n</pattern>
        </encoder>
    </appender>

    <appender name="fileErrorLog" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <!--如果只是想要 Error 級別的日誌,那麼需要過濾一下,默認是 info 級別的,ThresholdFilter-->
        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
            <level>Error</level>
        </filter>
        <!--日誌名稱,如果沒有File 屬性,那麼只會使用FileNamePattern的文件路徑規則
            如果同時有<File>和<FileNamePattern>,那麼當天日誌是<File>,明天會自動把今天
            的日誌改名爲今天的日期。即,<File> 的日誌都是當天的。
        -->
        <File>${logback.logdir}/error.${logback.appname}.log</File>
        <!--滾動策略,按照時間滾動 TimeBasedRollingPolicy-->
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <!--文件路徑,定義了日誌的切分方式——把每一天的日誌歸檔到一個文件中,以防止日誌填滿整個磁盤空間-->
            <FileNamePattern>${logback.logdir}/error.${logback.appname}.%d{yyyy-MM-dd}.log</FileNamePattern>
            <!--只保留最近90天的日誌-->
            <maxHistory>15</maxHistory>
            <!--用來指定日誌文件的上限大小,那麼到了這個值,就會刪除舊的日誌-->
            <!--<totalSizeCap>1GB</totalSizeCap>-->
        </rollingPolicy>
        <!--日誌輸出編碼格式化-->
        <encoder>
            <charset>UTF-8</charset>
            <!--<pattern>%d [%thread] %-5level %logger{36} %line - %msg%n</pattern>-->
            <pattern>%d{yyyy/MM/dd HH:mm:ss,SSS} [%p] [%t] %C.%M\(%F:%L\) - %msg%n</pattern>
        </encoder>
    </appender>

    <logger name="org.apache.zookeeper" level="OFF"/>

    <!--指定最基礎的日誌輸出級別-->
    <!--<root level="INFO">
        &lt;!&ndash;appender將會添加到這個loger&ndash;&gt;
        <appender-ref ref="consoleLog"/>
        <appender-ref ref="fileInfoLog"/>
        <appender-ref ref="fileErrorLog"/>
    </root>-->

    <!-- 開發環境 -->
    <springProfile name="dev">
        <root level="info">
            <appender-ref ref="consoleLog"/>
            <appender-ref ref="fileInfoLog"/>
            <appender-ref ref="fileErrorLog"/>
        </root>
    </springProfile>

    <!-- 測試環境 -->
    <springProfile name="test">
        <root level="info">
            <appender-ref ref="consoleLog"/>
            <appender-ref ref="fileInfoLog"/>
            <appender-ref ref="fileErrorLog"/>
        </root>
    </springProfile>

    <!-- 預生產環境 -->
    <springProfile name="pre">
        <root level="info">
            <appender-ref ref="consoleLog"/>
            <appender-ref ref="fileInfoLog"/>
            <appender-ref ref="fileErrorLog"/>
        </root>
    </springProfile>

    <!-- 線上環境 -->
    <springProfile name="prod">
        <root level="info">
            <appender-ref ref="consoleLog"/>
            <appender-ref ref="fileInfoLog"/>
            <appender-ref ref="fileErrorLog"/>
        </root>
    </springProfile>

</configuration>

通過 上述配置,理論上啓動spingboot應該就可以打印日誌了,然而並沒有,通過查看maven依賴發現多處jar包都引入了commons-logging,將其從依賴 中排除,重啓成功。最終的pom.xml文件配置如下:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>xxx</groupId>
        <artifactId>xxx</artifactId>
        <version>xxx</version>
        <relativePath>..</relativePath>
    </parent>

    <artifactId>xxx</artifactId>
    <packaging>jar</packaging>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    
    <dependencies>
    
        <dependency>
            <groupId>io.dropwizard.metrics</groupId>
            <artifactId>metrics-core</artifactId>
        </dependency>

        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-configuration2</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>commons-logging</groupId>
                    <artifactId>commons-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>commons-beanutils</groupId>
            <artifactId>commons-beanutils</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>commons-logging</groupId>
                    <artifactId>commons-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>commons-logging</groupId>
                    <artifactId>commons-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>com.alibaba.middleware</groupId>
            <artifactId>metrics-core-api</artifactId>
        </dependency>

        <dependency>
            <groupId>com.alibaba.middleware</groupId>
            <artifactId>metrics-reporter</artifactId>
        </dependency>

        <dependency>
            <groupId>com.alibaba.middleware</groupId>
            <artifactId>metrics-integration</artifactId>
        </dependency>

        <dependency>
        <groupId>com.alibaba.middleware</groupId>
        <artifactId>metrics-core-impl</artifactId>
        </dependency>

        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
        </dependency>

        <dependency>
            <groupId>com.google.re2j</groupId>
            <artifactId>re2j</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
    </dependencies>

    <profiles>
        <profile>
            <!-- development environment -->
            <id>dev</id>
            <properties>
                <profiles.active>dev</profiles.active>
            </properties>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>

        <!-- test environment -->
        <profile>
            <id>test</id>
            <properties>
                <profiles.active>test</profiles.active>
            </properties>
        </profile>

        <!-- Preproduction environment -->
        <profile>
            <id>pre</id>
            <properties>
                <profiles.active>pre</profiles.active>
            </properties>
        </profile>

        <!-- production environment -->
        <profile>
            <id>prod</id>
            <properties>
                <profiles.active>prod</profiles.active>
            </properties>
        </profile>
    </profiles>

    <build>

        <plugins>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                    <archive>
                        <manifest>
                            <!-- 你也可以在這裏自己寫MainClass -->
                            <mainClass>xxx</mainClass>
                        </manifest>
                    </archive>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

    <repositories>
        <repository>
            <id>alimaven</id>
            <name>aliyun maven</name>
            <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
            <releases>
                <enabled>true</enabled>
            </releases>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>

</project>

參考鏈接

1、spring-boot+logback+log4j2+MDC
2、Apache Maven Assembly
3、使用maven-assembly-plugin插件來定製化打包

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章