zoukankan      html  css  js  c++  java
  • 【log4j2 加载配置文件】 加载配置文件的三种方法

    log4j 2读取的配置文件可以分为三类:src下的配置文件、绝对路径的配置文件、相对路径的配置文件。
      1 package com.herman.test;
      2 
      3 import java.io.File;
      4 import java.io.FileInputStream;
      5 import java.net.URL;
      6 
      7 import org.apache.logging.log4j.LogManager;
      8 import org.apache.logging.log4j.Logger;
      9 import org.apache.logging.log4j.core.config.ConfigurationSource;
     10 import org.apache.logging.log4j.core.config.Configurator;
     11 
     12 public class ConfigTest {
     13   
     14   private static Logger logger = LogManager.getLogger(ConfigTest.class);
     15   /**
     16    * log4j 2读取配置文件
     17    * log4j 2读取的配置文件可以分为三类:src下的配置文件、绝对路径的配置文件、相对路径的配置文件
     18    */
     19   
     20   //第一类  加载src下的配置文件
     21   public static void test0(){
     22     //src下的配置文件会默认的被log4j的框架加载,我们就不显示的加载了
     23     //直接测试
     24     logger.info("我打印了.......");
     25     //输出内容
     26     //2014-09-01 15:49:30,229 INFO  [main] test.ConfigTest (ConfigTest.java:18) - 我打印了.......
     27   }
     28   
     29   //第二类  绝对路径的配置文件
     30   public static void test1(){
     31     //我们将log4j2.xml放在D盘下
     32     //这是需要手动的加载
     33     //绝对路径配置文件        
     34     ConfigurationSource source;
     35     try {
     36       //方法1  使用  public ConfigurationSource(InputStream stream) throws IOException 构造函数
     37       source = new ConfigurationSource(new FileInputStream("D:\log4j2.xml"));
     38       
     39       //方法2 使用 public ConfigurationSource(InputStream stream, File file)构造函数
     40       File config=new File("D:\log4j2.xml");
     41       source = new ConfigurationSource(new FileInputStream(config),config);
     42       
     43       //方法3 使用 public ConfigurationSource(InputStream stream, URL url) 构造函数
     44       String path="D:\log4j2.xml";
     45       source = new ConfigurationSource(new FileInputStream(path),new File(path).toURL());
     46       
     47       //source.setFile(new File("D:log4j2.xml"));        
     48       //source.setInputStream(new FileInputStream("D:log4j2.xml"));        
     49       Configurator.initialize(null, source);                
     50       Logger logger = LogManager.getLogger(ConfigTest.class.getName());         
     51       logger.trace("trace...");        
     52       logger.debug("debug...");        
     53       logger.info("info...");        
     54       logger.warn("warn...");        
     55       logger.error("error...");        
     56       logger.fatal("fatal...");
     57       //一下是运行效果
     58       /*2014-09-01 16:03:07,331 DEBUG [main] test.ConfigTest (ConfigTest.java:42) - debug...
     59       2014-09-01 16:03:07,331 INFO  [main] test.ConfigTest (ConfigTest.java:43) - info...
     60       2014-09-01 16:03:07,331 WARN  [main] test.ConfigTest (ConfigTest.java:44) - warn...
     61       2014-09-01 16:03:07,331 ERROR [main] test.ConfigTest (ConfigTest.java:45) - error...
     62       2014-09-01 16:03:07,331 FATAL [main] test.ConfigTest (ConfigTest.java:46) - fatal...*/
     63     } catch (Exception e) {
     64       e.printStackTrace();
     65     }        
     66   }
     67   
     68   //第三类  相对路径的配置文件加载
     69   public static void test2(){
     70     //这里需要注意路径中不要出现中文和空格,如果存在中文,请使用url转码
     71     ConfigurationSource source;
     72     try {
     73       //方法1  使用getResource()
     74       String path="/com/herman/config/log4j2.xml";
     75       URL url=ConfigTest.class.getResource(path);
     76       source = new ConfigurationSource(new FileInputStream(new File(url.getPath())),url);
     77       Configurator.initialize(null, source);    
     78       
     79       //方法2 使用System.getProperty
     80       String config=System.getProperty("user.dir");
     81       source = new ConfigurationSource(new FileInputStream(config+"\src\com\herman\config\log4j2.xml"));
     82       Configurator.initialize(null, source);
     83       
     84       //输出内容
     85       /*2014-09-01 16:32:19,746 DEBUG [main] test.ConfigTest (ConfigTest.java:53) - debug...
     86       2014-09-01 16:32:19,746 INFO  [main] test.ConfigTest (ConfigTest.java:54) - info...
     87       2014-09-01 16:32:19,746 WARN  [main] test.ConfigTest (ConfigTest.java:55) - warn...
     88       2014-09-01 16:32:19,746 ERROR [main] test.ConfigTest (ConfigTest.java:56) - error...
     89       2014-09-01 16:32:19,746 FATAL [main] test.ConfigTest (ConfigTest.java:57) - fatal...*/
     90     } catch (Exception e) {
     91       e.printStackTrace();
     92     }
     93   }
     94   
     95   public static void main(String[] args) {
     96     //test0();
     97     //test1();
     98     test2();
     99   }
    100 }
    View Code
  • 相关阅读:
    如何计算两个日期之间相差天数
    解决并发问题的小技巧
    Linq实现下拉框绑定
    No DataType in DataTemplate in Windows Phone(二)
    使用TOAD操作oracle初步
    使用log4net记录server Log
    尘世一场烟火
    No DataType in DataTemplate in Windows Phone(—)
    MVC设置初始页时发生的无法找到资源的简单错误
    oracle 使用in的灵异事件
  • 原文地址:https://www.cnblogs.com/sxdcgaq8080/p/5646233.html
Copyright © 2011-2022 走看看