【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

 

时间: 2024-09-17 03:51:57

【log4j2 加载配置文件】 加载配置文件的三种方法的相关文章

jquery动态加载js三种方法

 <!-- 这里为你提供了三种动态加载js的jquery实例代码哦,由于jquery是为用户提供方便的,所以利用jquery动态加载文件只要一句话$.getscript("test.js");就ok了. <!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd&qu

jquery动态加载js三种方法实例

这里为你提供了三种动态加载js的jquery实例代码哦,由于jquery是为用户提供方便的,所以利用jquery动态加载文件只要一句话$.getScript("test.js");就OK了.   复制代码 代码如下: <!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dt

jquery动态加载js三种方法实例_jquery

复制代码 代码如下: <!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="

jquery 动态加载js三种方法

<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="content-

android实现ViewPager懒加载的三种方法

在项目中ViewPager和Fragment接口框架已经是处处可见,但是在使用中,我们肯定不希望用户在当前页面时就在前后页面的数据,加入数据量很大,而用户又不愿意左右滑动浏览,那么这时候ViewPager中本来充满善意的预加载就有点令人不爽了.我们能做的就是屏蔽掉ViewPager的预加载机制.虽然ViewPager中提供的有setOffscreenPageLimit()来控制其预加载的数目,但是当设置为0后我们发现其根本没效果,这个的最小值就是1,也就是你只能最少前后各预加载一页.那么,这时候

Yii中单独为module加载Bootstrap或其他组件的4种方法

这里有4中方法来实现这个: 1.在应用的配置文件中添加如下内容 (protected/config/main.php): PHP  代码如下 复制代码     'modules'=>array(         'admin'=>array(             'preload'=>array('<span class='wp_keywordlink_affiliate'><a href="http://lxy.me/tag/bootstrap"

JavaScript动态加载CSS的三种方法

css|javascript|动态|加载 如果你有很多关联的CSS文件要一起加载,或者想动态的加载不同的CSS文件,那么下面的方法你一定对你有帮助. 第一种:一般用在外部CSS文件中加载必须的文件 程序代码@import url(style.css);/*只能用在CSS文件中或者style标签中*/ 第二种:简单的在页面中加载一个外部CSS文件  程序代码document.createStyleSheet(cssFile); 第三种:用createElement方法创建CSS的Link标签  程

动态加载JS文件的三种方法_javascript技巧

直接看实例.例1 重新加载js文件 复制代码 代码如下: function loadJs(file) {            var head = $("head").remove("script[role='reload']");            $("<scri" + "pt>" + "</scr" + "ipt>").attr({ role: 're

动态加载js文件的三种方法

<script language="网页特效"> function importfn(){  var head = document.getelementsbytagname("head")[0];  var script = document.createelement('script');  script.id = 'sid';  script.type = 'text/javascript';  script.src = '../js/alertt