0

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

启动日志和应用程序日志应记录到 2 个单独的文件中,这些文件会不断更新。文件附加程序将如何查看logback-spring.xml

4

2 回答 2

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 过滤日志以了解每个日志应该去哪里。

于 2018-08-13T09:54:32.063 回答
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>
于 2018-08-14T13:03:14.857 回答