• spring-boot logback配置


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

    1.1 在pom.xml中添加

    <profiles>
            <profile>
                <!-- 开发环境 -->
                <id>dev</id>
                <activation>
                    <activeByDefault>true</activeByDefault>
                </activation>
                <properties>
                    <env>dev</env>
                    <jar-scope>compile</jar-scope><!--provided-->
                    <!--<log-home>D:/gs-spring-boot-log</log-home>-->
                </properties>
            </profile>
    </profiles>

    build下添加

    <resources>
                <resource>
                    <filtering>true</filtering>
                    <directory>src/main/resources</directory>
                    <includes>
                        <include>${env}/*.properties</include>
                        <include>**/*.properties</include>
                        <include>**/*.xml</include>
                    </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" />
    
            <!-- ch.qos.logback.core.ConsoleAppender 控制台输出 -->
            <appender name="console" class="ch.qos.logback.core.ConsoleAppender">
                <encoder>
                    <pattern>%d{HH:mm:ss.SSS} %-5level [%thread] %logger{36} - %msg%n</pattern>
                </encoder>
            </appender>
            <!-- service -->
            <appender name="serviceRolling" class="ch.qos.logback.core.rolling.RollingFileAppender">
                <file>${LOG_HOME_SERVICE}.txt</file>
                <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>100MB</MaxFileSize>
                    </TimeBasedFileNamingAndTriggeringPolicy>
                </rollingPolicy>
                <encoder>
                    <pattern>%d{HH:mm:ss.SSS} %-5level [%thread] %logger{36} - %msg%n</pattern>
                </encoder>
            </appender>
    
            <!-- common -->
            <appender name="commonRolling"
                      class="ch.qos.logback.core.rolling.RollingFileAppender">
                <file>${LOG_HOME_COMMON}.txt</file>
                <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>100MB</MaxFileSize>
                    </TimeBasedFileNamingAndTriggeringPolicy>
                </rollingPolicy>
                <encoder>
                    <pattern>%d{HH:mm:ss.SSS} %-5level [%thread] %logger{36} - %msg%n</pattern>
                </encoder>
            </appender>
            <!-- integration -->
            <appender name="errorRolling"
                      class="ch.qos.logback.core.rolling.RollingFileAppender">
                <filter class="ch.qos.logback.classic.filter.LevelFilter">
                    <level>ERROR</level>
                    <onMatch>ACCEPT</onMatch>
                    <onMismatch>DENY</onMismatch>
                </filter>
                <file>${LOG_HOME_ERROR}.txt</file>
                <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>100MB</MaxFileSize>
                    </TimeBasedFileNamingAndTriggeringPolicy>
                </rollingPolicy>
                <encoder>
                    <pattern>%d{HH:mm:ss.SSS} %-5level [%thread] %logger{36} - %msg%n</pattern>
                </encoder>
            </appender>
    
            <logger name="hello" level="INFO">
                <appender-ref ref="serviceRolling" />
            </logger>
    
            <!--<logger name="hello" level="INFO">
                <appender-ref ref="commonRolling" />
            </logger>
    
            <logger name="dao" level="DEBUG">
                <appender-ref ref="serviceRolling" />
            </logger>-->
            <!-- 日志级别 -->
            <root level="INFO">
                <!--<appender-ref ref="console" />-->
                <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了

  • 相关阅读:
    帧同步优化难点及解决方案
    四元数
    臭鼬管理法
    十分钟理解Gradle
    深入理解Android之Gradle
    走出体制的臭鼬工厂,臭鼬著名的“14条”管理原则
    Unity5-ABSystem(五):AssetBundle内存
    Unity5-ABSystem(四):AssetBundle依赖
    Unity5-ABSystem(三):AssetBundle加载
    ASP.NET Core搭建多层网站架构【2-公共基础库】
  • 原文地址:https://www.cnblogs.com/yhzh/p/6673879.html
Copyright © 2020-2023  润新知