• logback配置


    好吧,项目中一直使用的是logback做日志记录。

    开始跑Demo的时候,一直会报Failed to load class org.slf4j.impl.StaticLogger的错误。后来google下,发现是这个原因:

    This error is reported when the org.slf4j.impl.StaticLoggerBinder class could not be loaded into memory. This happens when no appropriate SLF4J binding could be found on the class path. Placing one (and only one) of slf4j-nop.jar , slf4j-simple.jar , slf4j-log4j12.jar , slf4j-jdk14.jar or logback-classic.jar on the class path should solve the problem.

    原来我只导了slf4j-ai-1.7.2.jar,logback-core-1.0.7.jar,后来加上logback-classic-1.0.7.jar,就不报错了。

    具体的配置呢,参考这个blog就好了呢。http://aub.iteye.com/blog/1101260 根据blog去做配置的时候,又发现了一个问题。

    就是logback.xml里面的配置文件一直木有生效哈。

    先借用刚才那个blog里面的demo吧

    java code

     1 package demo;
     2 
     3 import org.slf4j.Logger;
     4 import org.slf4j.LoggerFactory;
     5 
     6 import ch.qos.logback.classic.LoggerContext;
     7 import ch.qos.logback.core.util.StatusPrinter;
     8 
     9 public class LogbackDemo {
    10     private static Logger log = LoggerFactory.getLogger(LogbackDemo.class);
    11 
    12     public static void main(String[] args) {
    13         log.trace("====trace");
    14         log.debug("====debug");
    15         log.info("====info");
    16         log.warn("====warn");
    17         log.error("====error");
    18 //        String URL = "logback.xml";
    19 //        System.out.println(ClassLoader.getSystemResource(URL));
    20 //        LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
    21 //        // print logback's internal status
    22 //        StatusPrinter.print(lc);
    23     }
    24 }
    View Code

    logback.xml

     1 <configuration>
     2   <appender name="FILE" class="ch.qos.logback.core.FileAppender">
     3     <file>./testFile.log</file>
     4     <append>true</append>
     5     <encoder>
     6       <pattern>%-4relative [%thread] %-5level %logger{35} -%msg%n</pattern>
     7     </encoder>
     8   </appender>
     9   <root level="INFO">
    10     <appender-ref ref="FILE" />
    11   </root>
    12 </configuration>
    View Code

    明明我的logback.xml的文件中配置的是将log打印到文件中,实际运行的时候,却一直在控制台打印下面的信息。

    output in console

    1 17:15:38.444 [main] DEBUG demo.LogbackDemo - ====debug
    2 17:15:38.448 [main] INFO  demo.LogbackDemo - ====info
    3 17:15:38.448 [main] WARN  demo.LogbackDemo - ====warn
    4 17:15:38.448 [main] ERROR demo.LogbackDemo - ====error
    View Code

    奇怪吧。之前是把logback.xml放在conf这个文件夹下面的,后来把logback.xml直接放在src下就可以了。知道原因了吧,logback的运行机制是默认去classpath下面去找配置文件,是怎样去查找的呢?

    1.Logback 尝试在classpath中(in the classpath)寻找一个名为 logback.groovy  的文件。
    2.如果没有找到该文件, logback 尝试在classpath中(in the classpath)寻找一个名为 logback-test.xml 的文件。
    3.如果没有找到该文件,尝试在classpath中(in the classpath)寻找一个名为 logback.xml 的文件。
    4.如果没有找到任何一个文件,logback 自己自动使用 BasicConfigurator 配置,它使 logging output 被定向到 console

    由于,demo中我并没有配置logback.groovy,logback-test.xml,而logback.xml文件又被我放到了conf文件夹下,所以,也没有找到,最后,logback只好将日志以默认的配置输出了。

    默认配置

     1 <configuration>
     2   <appender name="STDOUT"
     3   class="ch.qos.logback.core.ConsoleAppender">
     4     <!-- encoders are assigned the type
     5          ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
     6     <encoder>
     7       <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} -
     8       %msg%n</pattern>
     9     </encoder>
    10   </appender>
    11   <root level="debug">
    12     <appender-ref ref="STDOUT" />
    13   </root>
    14 </configuration>
    View Code

    所以,上面的output是不是可以理解了呢。

    好吧,把logback.xml放在了conf文件夹下后,怎么让logback找到它呢?刚才说了,logback的运行机制是默认去classpath下面去找配置文件,所以,把文件夹conf加入到classpath中就好了。在eclipse的java build path中,找到source,add folder就好了。再重新跑demo的话就一切OK了。

    有时,为了更好的维护代码,增加灵活性,需要把logback.xml的一些配置项的值写在另一个配置文件中,一般是properties文件哈。

    config.properties

    1 LOG_LEVEL=DEBUG
    2 APP_USER_HOME=./

    相应的logaback.xml

     1 <configuration scan="true" debug="true">
     2   <property resource="conf.properties" />
     3   <appender name="FILE" class="ch.qos.logback.core.FileAppender">
     4     <file>${APP_USER_HOME}/testFile.log</file>
     5     <append>true</append>
     6     <encoder>
     7       <pattern>%-4relative [%thread] %-5level %logger{35} -%msg%n</pattern>
     8     </encoder>
     9   </appender>
    10   <root level="${LOG_LEVEL}">
    11     <appender-ref ref="FILE" />
    12   </root>
    13 </configuration>
    View Code

    这样在src下面就会产生一个日志文件testFile.log;

    同时,将configuration的属性debug设置为"true"时,这样允许访问logback内部的运行状态,这时候将会在控制台打印下面的信息

     1 17:38:27,562 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback.groovy]
     2 17:38:27,562 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback-test.xml]
     3 17:38:27,562 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Found resource [logback.xml] at [file:/D:/workspace/LogbackDemo/bin/logback.xml]
     4 17:38:27,626 |-INFO in ReconfigureOnChangeFilter{invocationCounter=0} - Will scan for changes in [[D:workspaceLogbackDemoinlogback.xml]] every 60 seconds. 
     5 17:38:27,626 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - Adding ReconfigureOnChangeFilter as a turbo filter
     6 17:38:27,639 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type [ch.qos.logback.core.FileAppender]
     7 17:38:27,641 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as [FILE]
     8 17:38:27,650 |-INFO in ch.qos.logback.core.joran.action.NestedComplexPropertyIA - Assuming default type [ch.qos.logback.classic.encoder.PatternLayoutEncoder] for [encoder] property
     9 17:38:27,675 |-INFO in ch.qos.logback.core.FileAppender[FILE] - File property is set to [.//testFile.log]
    10 17:38:27,676 |-INFO in ch.qos.logback.classic.joran.action.RootLoggerAction - Setting level of ROOT logger to DEBUG
    11 17:38:27,676 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [FILE] to Logger[ROOT]
    12 17:38:27,677 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - End of configuration.
    13 17:38:27,678 |-INFO in ch.qos.logback.classic.joran.JoranConfigurator@13c468a - Registering current configuration as safe fallback point

    当然,logback中还有很多别的设置,最最详细的看下面这两篇blog最好了。

    一个是之前提到的那个blog做基础入门:http://aub.iteye.com/blog/1101260

    还有一篇blog做深入了解:http://www.yulezhandian.com/?p=324 

  • 相关阅读:
    高数基础
    java.io.FileNotFoundException: [WEB-INF/spring-servlet.xml] cannot be opened because it does not exist
    HBase1.0.0 实现数据增删查
    cm 安装cdh 后添加hive服务
    HBase启动错误提示别的机器60000已经存在
    CM集群管理
    CM 安装CDH 错误: 安装失败。 无法接收 Agent 发出的检测信号。
    Js运动框架
    单片机DA转换实现正弦波
    怎样让树莓派接移动硬盘
  • 原文地址:https://www.cnblogs.com/alvwood/p/3225688.html
Copyright © 2020-2023  润新知