码迷,mamicode.com
首页 > 其他好文 > 详细

SL4J+logback基本配置

时间:2015-06-13 11:12:52      阅读:201      评论:0      收藏:0      [点我收藏+]

标签:

SL4J简介

SLF4J,即简单日志门面(Simple Logging Facade for Java),不是具体的日志解决方案,它只服务于各种各样的日志系统,也就是说它只是一个抽象层(abstract layer),它允许你在后台使用任意一个日志类库。如果你开发的是类库或者嵌入式组件,那么就应该考虑采用SLF4J,因为不可能影响最终用户选择哪种日志系统。在另一方面,如果是一个简单或者独立的应用,确定只有一种日志系统,那么就没有使用SLF4J的必要。

Pom配置

  <properties>
         <slf4j-api.version>1.6.4</slf4j-api.version>
        <logback-classic.version>0.9.28</logback-classic.version>
        <jcl-over-slf4j.version>1.6.4</jcl-over-slf4j.version>
  </properties>
<!-- 日志开始 -->
        
        <!-- 日志框架API -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>${slf4j-api.version}</version>
        </dependency>
        <!-- 日志实现提供者 -->
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>${logback-classic.version}</version>
            <exclusions>
                <exclusion>
                    <groupId>commons-logging</groupId>
                    <artifactId>commons-logging</artifactId>
                </exclusion>
            </exclusions>
            <scope>runtime</scope>
        </dependency>
        <!-- 拦截 apache commons logging -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>jcl-over-slf4j</artifactId>
            <version>${jcl-over-slf4j.version}</version>
            <scope>runtime</scope>
        </dependency>
        
        <!-- 日志结束 -->

 

SL4J logback 基本配置

<?xml version="1.0" encoding="UTF-8"?>  
<configuration debug="true">  
    <!-- 应用名称 -->  
    <property name="APP_NAME" value="chinesefactored_beta" />  
    <!--日志文件的保存路径,首先查找系统属性-Dlog.dir,如果存在就使用其;否则,在当前目录下创建名为logs目录做日志存放的目录 -->  
    <property name="LOG_HOME" value="${log.dir:-logs}/${APP_NAME}" />  
    <!-- 日志输出格式 -->  
    <property name="ENCODER_PATTERN"  
        value="%d{yyyy-MM-dd  HH:mm:ss.SSS} [%thread] %-5level %logger{80} - %msg%n" />  
    <contextName>${APP_NAME}</contextName>  
  
    <!-- 控制台日志:输出全部日志到控制台 -->  
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">  
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">  
            <Pattern>${ENCODER_PATTERN}</Pattern>  
        </encoder>  
    </appender>  
  
    <!-- 文件日志:输出全部日志到文件 -->  
    <appender name="FILE"  
        class="ch.qos.logback.core.rolling.RollingFileAppender">  
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">  
            <fileNamePattern>${LOG_HOME}/output.%d{yyyy-MM-dd}.log</fileNamePattern>  
            <maxHistory>7</maxHistory>  
        </rollingPolicy>  
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">  
            <pattern>${ENCODER_PATTERN}</pattern>  
        </encoder>  
    </appender>  
  
    <!-- 错误日志:用于将错误日志输出到独立文件 -->  
    <appender name="ERROR_FILE"  
        class="ch.qos.logback.core.rolling.RollingFileAppender">  
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">  
            <fileNamePattern>${LOG_HOME}/error.%d{yyyy-MM-dd}.log</fileNamePattern>  
            <maxHistory>7</maxHistory>  
        </rollingPolicy>  
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">  
            <pattern>${ENCODER_PATTERN}</pattern>  
        </encoder>  
        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">  
            <level>WARN</level>  
        </filter>  
    </appender>  
      
    <!-- 独立输出的同步日志 -->  
    <appender name="STARDJ_FILE"  class="ch.qos.logback.core.rolling.RollingFileAppender">  
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">  
            <fileNamePattern>${LOG_HOME}/sync.%d{yyyy-MM-dd}.log</fileNamePattern>  
            <maxHistory>7</maxHistory>  
        </rollingPolicy>  
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">  
            <pattern>${ENCODER_PATTERN}</pattern>  
        </encoder>  
    </appender>  
      
    <logger name="log.stardj" level="DEBUG" addtivity="true">  
        <appender-ref ref="STARDJ_FILE" />  
    </logger>  
      
    <root>  
        <level value="DEBUG" />  
        <appender-ref ref="STDOUT" />  
        <appender-ref ref="FILE" />  
        <appender-ref ref="ERROR_FILE" />  
    </root>  
</configuration>  

 

测试代码

package com.stardj.qa.chinesefactored_beta;

import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class log_test {
//    private final static Logger LOG = LoggerFactory.getLogger(log_test.class);
    private final static Logger LOG = LoggerFactory.getLogger("log.stardj");
    @Test
    public void testLog(){
        String str = "This is a test";
        try {  
            LOG.error("this is an error test.");  
            LOG.info(str);
            System.out.println("OK.");  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
        
    }

}

 

SL4J+logback基本配置

标签:

原文地址:http://www.cnblogs.com/stardjyeah/p/4573040.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!