spring-boot logback配置


接着上篇的代码,日志在不同环境下的配置也不一样,所以要分开配置,主要使用maven的profile

1.1 在pom.xml中添加

<profiles>
        <profile>
            
            <id>devid>
            <activation>
                <activeByDefault>trueactiveByDefault>
            activation>
            <properties>
                <env>devenv>
                <jar-scope>compilejar-scope>
                
            properties>
        profile>
profiles>

build下添加

<resources>
            <resource>
                <filtering>truefiltering>
                <directory>src/main/resourcesdirectory>
                <includes>
                    <include>${env}/*.propertiesinclude>
                    <include>**/*.propertiesinclude>
                    <include>**/*.xmlinclude>
                includes>
            resource>
        resources>

1.2 之后在src/main/resources下面添加日志文件logback-spring.xml、config.properties配置:

其中,logback-spring.xml内容:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <include resource="org/springframework/boot/logging/logback/base.xml"/>

        <property resource="@env@/config.properties" />
        <timestamp key="bySecond" datePattern="yyyyMMdd'T'HHmmss" />

        
        <substitutionProperty name="LOG_HOME" value="${log-home}/@env@" />
        
        <substitutionProperty name="PROJECT_NAME" value="${project-name}" />
        <substitutionProperty name="LOG_HOME_SERVICE" value="${LOG_HOME}/${PROJECT_NAME}/service" />
        <substitutionProperty name="LOG_HOME_ERROR" value="${LOG_HOME}/${PROJECT_NAME}/error" />
        <substitutionProperty name="LOG_HOME_COMMON" value="${LOG_HOME}/${PROJECT_NAME}/common" />

        
        <appender name="console" class="ch.qos.logback.core.ConsoleAppender">
            <encoder>
                <pattern>%d{HH:mm:ss.SSS} %-5level [%thread] %logger{36} - %msg%npattern>
            encoder>
        appender>
        
        <appender name="serviceRolling" class="ch.qos.logback.core.rolling.RollingFileAppender">
            <file>${LOG_HOME_SERVICE}.txtfile>
            <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
                <fileNamePattern>${LOG_HOME_SERVICE}-%d{yyyy-MM-dd}.%i.log
                fileNamePattern>

                <TimeBasedFileNamingAndTriggeringPolicy
                        class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                    <MaxFileSize>100MBMaxFileSize>
                TimeBasedFileNamingAndTriggeringPolicy>
            rollingPolicy>
            <encoder>
                <pattern>%d{HH:mm:ss.SSS} %-5level [%thread] %logger{36} - %msg%npattern>
            encoder>
        appender>

        
        <appender name="commonRolling"
                  class="ch.qos.logback.core.rolling.RollingFileAppender">
            <file>${LOG_HOME_COMMON}.txtfile>
            <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
                <fileNamePattern>${LOG_HOME_COMMON}-%d{yyyy-MM-dd}.%i.log
                fileNamePattern>

                <TimeBasedFileNamingAndTriggeringPolicy
                        class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                    <MaxFileSize>100MBMaxFileSize>
                TimeBasedFileNamingAndTriggeringPolicy>
            rollingPolicy>
            <encoder>
                <pattern>%d{HH:mm:ss.SSS} %-5level [%thread] %logger{36} - %msg%npattern>
            encoder>
        appender>
        
        <appender name="errorRolling"
                  class="ch.qos.logback.core.rolling.RollingFileAppender">
            <filter class="ch.qos.logback.classic.filter.LevelFilter">
                <level>ERRORlevel>
                <onMatch>ACCEPTonMatch>
                <onMismatch>DENYonMismatch>
            filter>
            <file>${LOG_HOME_ERROR}.txtfile>
            <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
                <fileNamePattern>${LOG_HOME_ERROR}-%d{yyyy-MM-dd}.%i.log
                fileNamePattern>

                <TimeBasedFileNamingAndTriggeringPolicy
                        class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                    <MaxFileSize>100MBMaxFileSize>
                TimeBasedFileNamingAndTriggeringPolicy>
            rollingPolicy>
            <encoder>
                <pattern>%d{HH:mm:ss.SSS} %-5level [%thread] %logger{36} - %msg%npattern>
            encoder>
        appender>

        <logger name="hello" level="INFO">
            <appender-ref ref="serviceRolling" />
        logger>

        
        
        <root level="INFO">
            
            <appender-ref ref="errorRolling" />
        root>

configuration>

config.properties内容:

project-name=gs-spring-boot
log-home=D:/gs-spring-boot-log

经过以上步骤,配置已经好了,如果直接运行main方法是不行的,logback-spring.xml中的@env@是找不到的,因此要先执行maven命令

clean package -P dev

这样做是不是很麻烦呢?嗯。

idea执行main方法,会自动生成一个类似MyApplication(main方法所在类)的运行配置,edit configurations...编辑这个配置

运行之前执行maven命令,之后就可以直接运行main了