Logback:针对不同日志级别的不同不同文件

时间:2015-09-21 19:50:42

标签: java logging logback

我知道这类问题已经得到解答,但就我而言,我已尝试过每一个配置,但仍然无法正常工作。我需要对我的配置有一个全新的视图(我确信我错过了一些东西)。两个appender记录所有级别

我想将所有包的信息> =记录到控制台,而错误只记录到日志文件的错误。在我的情况下,他们都记录信息

这是配置。

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<!-- CONSOLE -->
<property name="LOG_PATTERN" value="%d [%thread] %-5level %logger{36} - %msg%n" />
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
    <encoder>
        <charset>UTF-8</charset>
        <pattern>${LOG_PATTERN}</pattern>
    </encoder>
</appender>

<!-- FILE FOR ERROR ONLY -->

<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>/Users/dev/Desktop/JAC/logs/frontend/errors.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">

    <fileNamePattern>/Users/dev/Desktop/JAC/logs/frontend/errors_%d{yyyy-MM-dd}.%i.log</fileNamePattern>
    <filter class="ch.qos.logback.classic.filter.LevelFilter">
        <level>ERROR</level>
        <onMatch>ACCEPT</onMatch>
        <onMismatch>DENY</onMismatch>
    </filter>
    <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
        <maxFileSize>5MB</maxFileSize>
    </timeBasedFileNamingAndTriggeringPolicy>
    <maxHistory>30</maxHistory>
</rollingPolicy>
<encoder>
    <charset>UTF-8</charset>
    <pattern>${LOG_PATTERN}</pattern>
</encoder>
</appender>


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

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

</configuration>

提前致谢

1 个答案:

答案 0 :(得分:2)

可能是,这不是更美丽的解决方案。但是,在这种情况下,会在控制台中记录日志级别INFO,并在文件中记录错误。调试级别不会打印。 INFO和ERROR只在一个出口处。 我的应用程序包是fr.myuser。

<?xml version="1.0" encoding="UTF-8"?>
<configuration>

    <property name="DEV_HOME" value="/home/myUser" />

    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>INFO</level>
            <onMatch>ACCEPT</onMatch>
            <onMismatch>DENY</onMismatch>
        </filter>
        <layout class="ch.qos.logback.classic.PatternLayout">
            <Pattern>
                %d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n
            </Pattern>
        </layout>
    </appender>

    <appender name="FILE-ERROR"
        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>${DEV_HOME}/error.log</file>
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <Pattern>
                %d{yyyy-MM-dd HH:mm:ss} - %msg%n
            </Pattern>
        </encoder>

        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <!-- rollover daily -->
            <fileNamePattern>${DEV_HOME}/error.%d{yyyy-MM-dd}.%i.log
                        </fileNamePattern>
            <timeBasedFileNamingAndTriggeringPolicy
                class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                <maxFileSize>10MB</maxFileSize>
            </timeBasedFileNamingAndTriggeringPolicy>
        </rollingPolicy>

    </appender>

    <!-- Send logs to console -->
    <logger name="fr.myuser" level="INFO">
        <appender-ref ref="STDOUT" />
    </logger>

    <root level="ERROR">
        <appender-ref ref="FILE-ERROR" />
    </root>

</configuration>