【Spring】logback-spring.xml 日志配置

 

写项目没关注过这个东西,项目工程文件也是同事从别的项目上搬过来的

测试跑环境发现不打印日志了,叫我看看怎么个事情

我们找到 logback-spring.xml 文件

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

    <!-- start:控制台 appender****************************************************************************** -->
    <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
        <encoder charset="UTF-8">
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{80} -
                %msg%n
            </pattern>
        </encoder>
    </appender>
    <!-- end:控制台 appender****************************************************************************** -->

    <!-- start:INFO appender****************************************************************************** -->
    <appender name="info" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <File>../logs/amerp-server/info/amerp-server-info.log</File>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <!--日志文件输出的文件名 -->
            <fileNamePattern>../logs/amerp-server/info/amerp-server-info-%d{yyyy-MM-dd}.log.%i
            </fileNamePattern>
            <!--日志文件保留天数-->
            <MaxHistory>3</MaxHistory>
            <timeBasedFileNamingAndTriggeringPolicy
                    class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                <!-- or whenever the file size reaches 10MB -->
                <maxFileSize>10MB</maxFileSize>
            </timeBasedFileNamingAndTriggeringPolicy>
        </rollingPolicy>
        <encoder>
            <Pattern>%-15d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level
                %logger{80}[%class:%line] - %msg%n
            </Pattern>
            <charset>UTF-8</charset>
            <immediateFlush>true</immediateFlush>
        </encoder>
        <filter class="ch.qos.logback.classic.filter.LevelFilter"><!-- 只打印INFO日志 -->
            <level>INFO</level>
            <onMatch>ACCEPT</onMatch>
            <onMismatch>DENY</onMismatch>
        </filter>
    </appender>
    <appender name="ASYNC-INFO" class="ch.qos.logback.classic.AsyncAppender">
        <!-- 不丢失日志.默认的,如果队列的80%已满,则会丢弃TRACT、DEBUG、INFO级别的日志 -->
        <discardingThreshold>0</discardingThreshold>
        <!-- 更改默认的队列的深度,该值会影响性能.默认值为256 -->
        <queueSize>1024</queueSize>
        <includeCallerData>true</includeCallerData>
        <!-- 添加附加的appender,最多只能添加一个 -->
        <appender-ref ref="info"/>
    </appender>
    <!-- end:INFO appender****************************************************************************** -->

    <!-- start:ERROR appender****************************************************************************** -->
    <appender name="error" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <File>../logs/amerp-server/error/amerp-server-error.log</File>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <!--日志文件输出的文件名 -->
            <fileNamePattern>../logs/amerp-server/error/amerp-server-error-%d{yyyy-MM-dd}.log.%i
            </fileNamePattern>
            <!--日志文件保留天数-->
            <MaxHistory>3</MaxHistory>
            <timeBasedFileNamingAndTriggeringPolicy
                    class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                <!-- or whenever the file size reaches 10MB -->
                <maxFileSize>10MB</maxFileSize>
            </timeBasedFileNamingAndTriggeringPolicy>
        </rollingPolicy>
        <encoder>
            <Pattern>%-15d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level
                %logger{80}[%class:%line] - %msg%n
            </Pattern>
            <charset>UTF-8</charset>
            <immediateFlush>true</immediateFlush>
        </encoder>
        <filter class="ch.qos.logback.classic.filter.LevelFilter"><!-- 只打印ERROR日志 -->
            <level>ERROR</level>
            <onMatch>ACCEPT</onMatch>
            <onMismatch>DENY</onMismatch>
        </filter>
    </appender>
    <appender name="ASYNC-ERROR" class="ch.qos.logback.classic.AsyncAppender">
        <!-- 不丢失日志.默认的,如果队列的80%已满,则会丢弃TRACT、DEBUG、INFO级别的日志 -->
        <discardingThreshold>0</discardingThreshold>
        <!-- 更改默认的队列的深度,该值会影响性能.默认值为256 -->
        <queueSize>1024</queueSize>
        <includeCallerData>true</includeCallerData>
        <!-- 添加附加的appender,最多只能添加一个 -->
        <appender-ref ref="error"/>
    </appender>
    <!-- end:ERROR appender****************************************************************************** -->

    <!-- start:WARN appender****************************************************************************** -->
    <appender name="warn" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <File>../logs/amerp-server/warn/amerp-server-warn.log</File>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <!--日志文件输出的文件名 -->
            <fileNamePattern>../logs/amerp-server/warn/amerp-server-warn-%d{yyyy-MM-dd}.log.%i
            </fileNamePattern>
            <!--日志文件保留天数-->
            <MaxHistory>3</MaxHistory>
            <timeBasedFileNamingAndTriggeringPolicy
                    class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                <!-- or whenever the file size reaches 10MB -->
                <maxFileSize>10MB</maxFileSize>
            </timeBasedFileNamingAndTriggeringPolicy>
        </rollingPolicy>
        <encoder>
            <Pattern>%-15d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level
                %logger{80}[%class:%line] - %msg%n
            </Pattern>
            <charset>UTF-8</charset>
            <immediateFlush>true</immediateFlush>
        </encoder>
        <filter class="ch.qos.logback.classic.filter.LevelFilter"><!-- 只打印WARN日志 -->
            <level>WARN</level>
            <onMatch>ACCEPT</onMatch>
            <onMismatch>DENY</onMismatch>
        </filter>
    </appender>
    <appender name="ASYNC-WARN" class="ch.qos.logback.classic.AsyncAppender">
        <!-- 不丢失日志.默认的,如果队列的80%已满,则会丢弃TRACT、DEBUG、INFO级别的日志 -->
        <discardingThreshold>0</discardingThreshold>
        <!-- 更改默认的队列的深度,该值会影响性能.默认值为256 -->
        <queueSize>1024</queueSize>
        <includeCallerData>true</includeCallerData>
        <!-- 添加附加的appender,最多只能添加一个 -->
        <appender-ref ref="warn"/>
    </appender>
    <!-- end:ERROR appender****************************************************************************** -->

    <!-- start:project default level -->
    <logger name="org.springframework" level="INFO"/>
    <logger name="org.apache" level="INFO"/>
    <logger name="org.apache.cxf" level="INFO"/>
    <logger name="springfox" level="INFO"/>
    <logger name="org.activiti" level="INFO"/>
    <!-- end:project default level****************************************************************************** -->

    <!-- star:dev环境配置文件****************************************************************************** -->
    <springProfile name="dev">
        <logger name="cn.hyite.amerp" level="DEBUG"/>
        <!--<logger name="org.apache.mina" level="ERROR"/>-->
        <root level="DEBUG">
            <appender-ref ref="CONSOLE"/>
        </root>
    </springProfile>
    <!-- end:dev环境配置文件****************************************************************************** -->


    <!-- star:test环境配置文件****************************************************************************** -->
    <springProfile name="test">
        <!-- 下面配置一些第三方包的日志过滤级别,用于避免刷屏 -->
        <logger name="cn.hyite.amerp" level="INFO"/>
        <logger name="org.springframework" level="ERROR"/>
        <logger name="org.apache.cxf" level="ERROR"/>
        <logger name="org.apache.mina.filter" level="ERROR"/>
        <!-- root -->
        <root level="info">
            <appender-ref ref="ASYNC-INFO"/>
            <appender-ref ref="ASYNC-ERROR"/>
            <appender-ref ref="ASYNC-WARN"/>
        </root>
    </springProfile>
    <!-- end:test环境配置文件****************************************************************************** -->

    <!-- star:prod环境配置文件****************************************************************************** -->
    <springProfile name="prod">
        <!-- 下面配置一些第三方包的日志过滤级别,用于避免刷屏 -->
        <logger name="cn.hyite.amerp" level="INFO"/>
        <logger name="org.springframework" level="ERROR"/>
        <logger name="org.apache.cxf" level="ERROR"/>
        <logger name="org.apache.mina.filter" level="ERROR"/>
        <!-- root -->
        <root level="INFO">
            <appender-ref ref="ASYNC-INFO"/>
            <appender-ref ref="ASYNC-ERROR"/>
            <appender-ref ref="ASYNC-WARN"/>
        </root>
    </springProfile>
    <!-- end:prod环境配置文件****************************************************************************** -->

</configuration>

 

先看 springProfile一栏,这里对应每一个profile

一共配置了三种 dev - test - prod

开发 - 测试 - 生产

    <!-- star:dev环境配置文件****************************************************************************** -->
    <springProfile name="dev">
        <logger name="cn.hyite.amerp" level="DEBUG"/>
        <!--<logger name="org.apache.mina" level="ERROR"/>-->
        <root level="DEBUG">
            <appender-ref ref="CONSOLE"/>
        </root>
    </springProfile>
    <!-- end:dev环境配置文件****************************************************************************** -->


    <!-- star:test环境配置文件****************************************************************************** -->
    <springProfile name="test">
        <!-- 下面配置一些第三方包的日志过滤级别,用于避免刷屏 -->
        <logger name="cn.hyite.amerp" level="INFO"/>
        <logger name="org.springframework" level="ERROR"/>
        <logger name="org.apache.cxf" level="ERROR"/>
        <logger name="org.apache.mina.filter" level="ERROR"/>
        <!-- root -->
        <root level="info">
            <appender-ref ref="ASYNC-INFO"/>
            <appender-ref ref="ASYNC-ERROR"/>
            <appender-ref ref="ASYNC-WARN"/>
        </root>
    </springProfile>
    <!-- end:test环境配置文件****************************************************************************** -->

    <!-- star:prod环境配置文件****************************************************************************** -->
    <springProfile name="prod">
        <!-- 下面配置一些第三方包的日志过滤级别,用于避免刷屏 -->
        <logger name="cn.hyite.amerp" level="INFO"/>
        <logger name="org.springframework" level="ERROR"/>
        <logger name="org.apache.cxf" level="ERROR"/>
        <logger name="org.apache.mina.filter" level="ERROR"/>
        <!-- root -->
        <root level="INFO">
            <appender-ref ref="ASYNC-INFO"/>
            <appender-ref ref="ASYNC-ERROR"/>
            <appender-ref ref="ASYNC-WARN"/>
        </root>
    </springProfile>
    <!-- end:prod环境配置文件****************************************************************************** -->

 

但是没找到决定到底使用哪一个profile,我猜想是在application.yml设置的

  profiles:
    active: dev

 

改成 test 就能正常输出了,晕倒

  

 

 

 

 

 

 

  

 

posted @ 2024-12-06 11:00  emdzz  阅读(138)  评论(0编辑  收藏  举报