Spring Boot logback-spring.xml

时间:2018-08-13 08:52:35

标签: xml spring-boot logging spring-logback

有人可以帮助我为这种情况创建一个logback-spring.xml文件:

启动日志和应用程序日志应记录到2个单独的文件中,并不断更新。文件附加器在logback-spring.xml中的外观如何?

2 个答案:

答案 0 :(得分:1)

我设法通过这种方法实现了

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="APP_FILE" class="ch.qos.logback.core.FileAppender">
    <filter class="ch.qos.logback.core.filter.EvaluatorFilter">
        <evaluator> <!-- defaults to type ch.qos.logback.classic.boolex.JaninoEventEvaluator -->
            <expression>return logger.equals("<YOUR FULL CLASS NAME FOR APPLICATION CLASS>");</expression>
        </evaluator>
        <OnMismatch>ACCEPT</OnMismatch>
        <OnMatch>DENY</OnMatch>
    </filter>

    <file>app.log</file>
    <append>true</append>
    <!-- set immediateFlush to false for much higher logging throughput -->
    <immediateFlush>true</immediateFlush>
    <encoder>
        <pattern>%d{dd-MM-yyyy HH:mm:ss.SSS} %magenta([%thread]) %highlight(%-5level) %logger{36}.%M - %msg%n</pattern>
    </encoder>
</appender>

<appender name="STARTUP_FILE" class="ch.qos.logback.core.FileAppender">
    <filter class="ch.qos.logback.core.filter.EvaluatorFilter">
        <evaluator> <!-- defaults to type ch.qos.logback.classic.boolex.JaninoEventEvaluator -->
            <expression>return logger.equals("<YOUR FULL CLASS NAME FOR APPLICATION CLASS>");</expression>
        </evaluator>
        <OnMismatch>DENY</OnMismatch>
        <OnMatch>ACCEPT</OnMatch>
    </filter>
    <file>startup.log</file>
    <append>true</append>
    <!-- set immediateFlush to false for much higher logging throughput -->
    <immediateFlush>true</immediateFlush>
    <encoder>
        <pattern>%d{dd-MM-yyyy HH:mm:ss.SSS} %magenta([%thread]) %highlight(%-5level) %logger{36}.%M - %msg%n</pattern>
    </encoder>
</appender>

<logger name="<YOUR PACKAGE HERE>" level="debug">
    <appender-ref ref="APP_FILE" />
</logger>

<root level="info">
    <appender-ref ref="STARTUP_FILE" />
</root>

只需替换:

<YOUR FULL CLASS NAME FOR APPLICATION CLASS>
<YOUR PACKAGE HERE>

示例:

com.bhrother.exampleapp.Application.class
com.bhrother.exampleapp

在这里,我们正在按loggerName过滤日志,以了解每个日志应存放的位置。

答案 1 :(得分:0)

    <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xml>
<configuration>
    <include resource="org/springframework/boot/logging/logback/defaults.xml"/>
    <include resource="org/springframework/boot/logging/logback/console-appender.xml"/>

    <appender name="ROLLING-FILE"
              class="ch.qos.logback.core.rolling.RollingFileAppender">
        <encoder>
            <pattern>
              some pattern
            </pattern>
        </encoder>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
        <!--daily rollover -->
            <fileNamePattern>some pattern</fileNamePattern>
        </rollingPolicy>
    </appender>
    <appender name="STARTUP"
              class="ch.qos.logback.core.rolling.RollingFileAppender">
        <encoder>
            <pattern>
                ${CONSOLE_LOG_PATTERN}
            </pattern>
        </encoder>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>some pattern</fileNamePattern>
        </rollingPolicy>
    </appender>
    <logger name="SOME PACKAGE" level="INFO" additivity="false">
        <appender-ref ref="CONSOLE"/>
        <appender-ref ref="ROLLING-FILE"/>
    </logger>
    <root level="INFO">
        <appender-ref ref="CONSOLE"/>
        <appender-ref ref="STARTUP"/>
    </root>
</configuration>