当前位置:首页 > 开发 > 开源软件 > 正文

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

发表于: 2014-11-17   作者:aigo   来源:转载   浏览:
摘要: logback.xmlp配置: <?xml version="1.0" encoding="UTF-8"?> <configuration> <jmxConfigurator/> <appender name="STDOUT" class="ch.qos.lo

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

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

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号