zoukankan      html  css  js  c++  java
  • log4j 分类输出

    一个log4j的问题也是折磨了我两天了。

    终于算是实现了个符合需求的小demo。然而,我必须吧log4j搞定,这个乐塞。

    需求描述:

      用xml配置文件,将debug、info、warn、error分类且单独放各自相应的日志文件中。

    实现:

      xml文件:

    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
    
    <log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
    
      <appender name="console" class="org.apache.log4j.ConsoleAppender">
        <param name="Target" value="System.out"/>
        <layout class="org.apache.log4j.PatternLayout">
          <param name="ConversionPattern" value="%-5p %c{1} - %m%n"/>
        </layout>
      </appender>
      
      <appender name="my_error" class="org.apache.log4j.FileAppender">
        <param name="File" value="D://logs/log4j_error.log"/>   
        <layout class="org.apache.log4j.PatternLayout">
          <param name="ConversionPattern" value="%-5p %c{1} - %m%n"/>
        </layout>
        <filter class="org.apache.log4j.varia.LevelRangeFilter">
                            <param name="levelMin" value="ERROR" />
                            <param name="levelMax" value="ERROR" />
        </filter>
      </appender>
      
      <appender name="my_warn" class="org.apache.log4j.FileAppender">
        <param name="File" value="D://logs/log4j_warn.log"/>   
        <layout class="org.apache.log4j.PatternLayout">
          <param name="ConversionPattern" value="%-5p %c{1} - %m%n"/>
        </layout>
        <filter class="org.apache.log4j.varia.LevelRangeFilter">
                            <param name="levelMin" value="WARN" />
                            <param name="levelMax" value="WARN" />
        </filter>
      </appender>
      
      <appender name="my_info" class="org.apache.log4j.FileAppender">
        <param name="File" value="D://logs/log4j_info.log"/>   
        <layout class="org.apache.log4j.PatternLayout">
          <param name="ConversionPattern" value="%-5p %c{1} - %m%n"/>
        </layout>
        <filter class="org.apache.log4j.varia.LevelRangeFilter">
                            <param name="levelMin" value="INFO" />
                            <param name="levelMax" value="INFO" />
        </filter>
      </appender>
      
      <appender name="my_debug" class="org.apache.log4j.FileAppender">
        <param name="File" value="D://logs/log4j_debug.log"/>   
        <layout class="org.apache.log4j.PatternLayout">
          <param name="ConversionPattern" value="%-5p %c{1} - %m%n"/>
        </layout>
        <filter class="org.apache.log4j.varia.LevelRangeFilter">
                            <param name="levelMin" value="DEBUG" />
                            <param name="levelMax" value="DEBUG" />
        </filter>
      </appender>
      
         <logger name="Test.log4jTest" additivity="true">      
            <level value="DEBUG" />      
            <appender-ref ref="my_debug" />  
            <appender-ref ref="my_warn" /> 
            <appender-ref ref="my_info" /> 
            <appender-ref ref="my_error" />  
         </logger>
         
        <root>
          <priority value ="debug" />
          <appender-ref ref="console" />
        </root>
    
    </log4j:configuration>

    测试类 log4jTest:

    package Test;
    
    import org.apache.log4j.Appender;
    import org.apache.log4j.xml.DOMConfigurator;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    
    public class log4jTest {
        private static Logger logger = LoggerFactory.getLogger(log4jTest.class);
    
        static {
            DOMConfigurator.configure("log4j.xml");
        }
    
        public static void main(String[] args) {
            logger.debug("this is debug");
            logger.info("this is info");
            logger.error("this is error");
            logger.warn("this is warn");
        }
    
    }

    关键点:

      1.xml中,起单独分类作用的是标签 <filter>

      2.测试类中,加上一行代码,用于获取log4j.xml。(为了配置方便,我的log4j放在了项目根目录下,与src同级,故必须加上改行代码。)

        DOMConfigurator.configure("log4j.xml");

      其他point将在后续文章逐点列出,敬请关注。

  • 相关阅读:
    15个常用的Excel函数公式
    Excel常用公式大全
    Python+Selenium-select
    Python+Selenium--操作测试对象
    Python+Selenium--鼠标事件
    Python+Selenium--键盘事件
    Python+Selenium--设置等待时间
    Python+Selenium--定位iframe中的对象
    Python+Selenium--启动及关闭浏览器
    Tomcat安装详解
  • 原文地址:https://www.cnblogs.com/not-NULL/p/5147829.html
Copyright © 2011-2022 走看看