项目中用到的logback列子

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <appender name="ACTIVITY"
        class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>log/activity.log</file>
        <encoder charset="UTF-8">
            <pattern>%date|%msg%n</pattern>
        </encoder>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>log/activity.log-%d{yyyy-MM-dd}</fileNamePattern>
            <!-- 保留的天数/小时数 -->
            <maxHistory>30</maxHistory>
        </rollingPolicy>
    </appender>
    <appender name="SYS"
        class="ch.qos.logback.core.rolling.RollingFileAppender">
        <!-- 日志输出类型 -->
        <file>log/sys.log</file>
        <encoder charset="UTF-8">
            <!-- 输出格式化 -->
            <pattern>%date|%thread|%level|%F->%M|%msg%n</pattern>
        </encoder>
        <!-- 临界值过滤器,过滤掉级别更低的日志 -->
        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
            <level>INFO</level>
        </filter>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <!-- 日志迁移方式(滚动策略) -->
            <fileNamePattern>log/sys.log-%d{yyyy-MM-dd-HH}</fileNamePattern>
        </rollingPolicy>
    </appender>
    <appender name="stdout" class="ch.qos.logback.core.ConsoleAppender">
        <encoder charset="UTF-8">
            <pattern>%date|%thread|%level|%F->%M|%msg%n</pattern>
        </encoder>
        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
            <level>INFO</level>
        </filter>
    </appender>
    <!-- additivity="false"表示不传递 -->
    <logger name="ACTIVITY" level="DEBUG" additivity="false">
        <appender-ref ref="ACTIVITY" />
    </logger>
    <!-- root是默认的logger,日志的输出先由logger的level控制,再由appender的filter控制 -->
    <root level="DEBUG">
        <!-- 默认日志采用两种输出方式 -->
        <appender-ref ref="stdout" />
        <appender-ref ref="SYS" />
    </root>
</configuration>

 

引用jar包:

logback-classic-1.1.1.jar    logback-core-1.1.1.jar     slf4j-api-1.6.6.jar

 

java代码:

package log_test;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class Test {
    
    private static final Logger log = LoggerFactory.getLogger(Test.class);
    private static Logger log2 = LoggerFactory.getLogger("ACTIVITY");
    
    public static void main(String[] args) {
        
        log.info(System.currentTimeMillis()+"");
        log.error(System.currentTimeMillis()+"");
        log.debug(System.currentTimeMillis()+"");
        
        log2.info(System.currentTimeMillis()+" ACTIVITY");
        log2.error(System.currentTimeMillis()+" ACTIVITY");
        log2.debug(System.currentTimeMillis()+" ACTIVITY");
        
    }
}

 

指定配置文件路径:

private static void readLogPath() {
        LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
        JoranConfigurator configurator = new JoranConfigurator();
        configurator.setContext(lc);
        lc.reset();
        try {
            configurator.doConfigure("res/logback.xml");
        } catch (JoranException e) {
            e.printStackTrace();
        }
        StatusPrinter.printInCaseOfErrorsOrWarnings(lc);
    }

 

你可能感兴趣的:(logback)