slf4j+logback多个日志输出配置实例

logback.xmlp配置:

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

    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder charset="UTF-8">
            <pattern>%d{HH:mm:ss.SSS} [%thread][%-5level] %msg -%logger%n</pattern>
        </encoder>
    </appender>

    <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <Prudent>true</Prudent>
        <encoder>
            <pattern>%-10(%d{yyyy-MM-dd HH:mm:ss.SSS}) [%-10thread][%-5level] %msg -%logger{36}%n</pattern>
            <charset>UTF-8</charset>
        </encoder>

        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>log/%d{yyyy-MM}/%d{yyyy_MM_dd}_${com.test}.%i.log</fileNamePattern>
            <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                <maxFileSize>10MB</maxFileSize>
            </timeBasedFileNamingAndTriggeringPolicy>
        </rollingPolicy>
    </appender>

    <appender name="TOP_LOG" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <Prudent>true</Prudent>
        <encoder>
            <pattern>%-10(%d{yyyy-MM-dd HH:mm:ss.SSS}) [%-10thread][%-5level] %msg -%logger{36}%n</pattern>
            <charset>UTF-8</charset>
        </encoder>

        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>log/%d{yyyy-MM}/%d{yyyy_MM_dd}_top.%i.log</fileNamePattern>
            <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                <maxFileSize>10MB</maxFileSize>
            </timeBasedFileNamingAndTriggeringPolicy>
        </rollingPolicy>
    </appender>

    <logger name="com.google">
        <level value="trace"/>
        <appender-ref ref="FILE"/>
        <appender-ref ref="STDOUT"/>
    </logger>
    <logger name="top_log">
        <level value="trace"/>
        <appender-ref ref="TOP_LOG"/>
    </logger>
</configuration>

 

在java代码中,这样来输出到不同的log文件:

 

static final Logger LOG = LoggerFactory.getLogger(TopServer.class);

static final Logger LOG_TOP = LoggerFactory.getLogger("top_log");

 

 

LOG.error("aaaa");
LOG_TOP.trace("bbbb");

 

其中<logger name="com.google">表示包名以com.google开头的代码都使用"FILE"配置的输出文件。

详细使用说明:http://czj4451.iteye.com/blog/1984259

你可能感兴趣的:(logback)