zoukankan      html  css  js  c++  java
  • spring配置日志

    原文:http://blog.csdn.net/xiejx618/article/details/41698913

    参考:http://spring.io/blog/2009/12/04/logging-dependencies-in-spring

    spring默认使用了commons-logging,在集成其它日志框架,slf4j使用了编译时绑定来代替运行时发现.它注定是更高效的(当然slf4j还可以使用占位符绑定等功能).下文介绍spring三种常用方案记录日志,推荐第三种
    方案一:commons-logging+Log4J

    1.依赖:

    <dependency>  
        <groupId>org.springframework</groupId>  
        <artifactId>spring-context</artifactId>  
        <version>${spring.version}</version>  
    </dependency>  
    <dependency>  
        <groupId>log4j</groupId>  
        <artifactId>log4j</artifactId>  
        <version>1.2.17</version>  
    </dependency>  

    2.在classpath根目录添加log4j.properties或log4j.xml,关于开发的日志级别配置,调得太高,会漏到一些信息看不见,调得太低,输出得太多,看得眼花缭乱,在产品阶段甚至影响应用性能.我觉得开发时,默认(一般依赖也是用默认appender)给WARN,项目本身编写的代码可以给DEBUG,其它另外特别指定,以下是log4j.properties的一个例子:

    log4j.rootCategory=WARN, stdout  
      
    log4j.appender.stdout=org.apache.log4j.ConsoleAppender  
    log4j.appender.stdout.layout=org.apache.log4j.PatternLayout  
    log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %t %c{2}:%L - %m%n  
      
    log4j.category.org.exam=DEBUG  
    log4j.category.org.springframework.beans.factory=INFO  

    根据log4j API(http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/Category.html),Category已经过时,应使用Logger代替.而Logger是Category的子类,在Category的所有操作都可以在Logger上执行.以上的properties文件内容应改为

    log4j.rootLogger=WARN, stdout  
      
    log4j.appender.stdout=org.apache.log4j.ConsoleAppender  
    log4j.appender.stdout.layout=org.apache.log4j.PatternLayout  
    log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %t %c{2}:%L - %m%n  
      
    log4j.logger.org.exam=DEBUG  
    log4j.logger.org.springframework.beans.factory=INFO  

    3.代码用法:

    import org.apache.log4j.Logger;  
    public class UserService {  
        final Logger logger = Logger.getLogger(UserService.class);  
        public boolean save(){  
            logger.error("虽然A喜欢我,但是我喜欢B");  
            return true;  
        }  
    }  

    方案二:slf4j+Log4J
    1.先在spring排除commons-logging.再引入4个包:桥接包jcl-over-slf4j,SLF4J API包slf4j-api,绑定到Log4J的包slf4j-log4j12,Log4J实现包log4j12.

    <dependency>  
        <groupId>org.slf4j</groupId>  
        <artifactId>jcl-over-slf4j</artifactId>  
        <version>1.7.7</version>  
    </dependency>  
    <dependency>  
        <groupId>org.slf4j</groupId>  
        <artifactId>slf4j-log4j12</artifactId>  
        <version>1.7.7</version>  
    </dependency>  
    <dependency>  
        <groupId>org.springframework</groupId>  
        <artifactId>spring-context</artifactId>  
        <version>${spring.version}</version>  
        <exclusions>  
            <exclusion>  
                <groupId>commons-logging</groupId>  
                <artifactId>commons-logging</artifactId>  
            </exclusion>  
        </exclusions>  
    </dependency>  

    2.log4j配置同上.
    3.代码用法:

    import org.slf4j.Logger;  
    import org.slf4j.LoggerFactory;  
    public class UserService {  
        final Logger logger = LoggerFactory.getLogger(UserService.class);  
        public boolean save(){  
            logger.error("虽然{}喜欢我,但是我喜欢{}","A","B");  
            return true;  
        }  
    }  

    方案三:slf4j+logback.其中,logback是slf4j原生的实现.

    1.先引入logback-classic依赖,它会将其它依赖引入,再在spring排除commons-logging.

    <dependency>  
        <groupId>ch.qos.logback</groupId>  
        <artifactId>logback-classic</artifactId>  
        <version>1.1.3</version>  
    </dependency>  
    <dependency>  
        <groupId>org.springframework</groupId>  
        <artifactId>spring-webmvc</artifactId>  
        <version>${spring.version}</version>  
        <exclusions>  
            <exclusion>  
                <groupId>commons-logging</groupId>  
                <artifactId>commons-logging</artifactId>  
            </exclusion>  
        </exclusions>  
    </dependency>  

    2.logback配置:查找顺序logback.groovy-->logback-test.xml-->logback.xml,详细配置还是参考文档http://logback.qos.ch/documentation.html.下面是logback.xml例子

    <?xml version="1.0" encoding="UTF-8"?>  
    <configuration>  
        <appender name="console" class="ch.qos.logback.core.ConsoleAppender">  
            <encoder>  
                <pattern>%d{yy.MM.dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n</pattern>  
            </encoder>  
        </appender>  
        <logger name="org.exam" level="debug" />  
        <logger name="org.springframework.beans.factory" level="info" />  
        <root level="warn">  
            <appender-ref ref="console"/>  
        </root>  
    </configuration>  
  • 相关阅读:
    7月自动化测试公开课通知
    招高级自动化测试工程师
    招聘软件开发工程师/开发实习生
    Selenium关键字驱动测试框架Demo(Java版)
    Eclipse+Selenium自动化测试脚本设计V1.0
    WebDriver基本API使用(基于Java)V1.0
    测试体系建设 免费咨询服务
    UFTQTP 12 新特性
    招聘自动化测试工程师
    AppScan学习笔记
  • 原文地址:https://www.cnblogs.com/But-you/p/9722382.html
Copyright © 2011-2022 走看看