Log4j 2.0在開發中的進階使用詳解—讀取設定檔(六),log4j使用詳解

來源:互聯網
上載者:User

Log4j 2.0在開發中的進階使用詳解—讀取設定檔(六),log4j使用詳解
log4j中配置記錄檔存放的位置不一定在src下面,即根目錄下。這個時候我們需要解決如何載入設定檔的問題。在log4j1.x中解決的方法就比較多了。如:PropertyConfigurator.configure();和DOMConfigurator.configure ();這兩種方法讀取。而在log4j2.x當中,這兩個類都已經不存在了。這個時候我們該如何去載入設定檔呢?答案也很簡單,就是log4j2.x的版本給我提供了ConfigurationSource和Configurator這兩個類。我們可以使用它們進行手動的載入任意位置的設定檔資訊。我就主要介紹三種方法:log4j 2讀取設定檔的三種方法。log4j 2讀取的設定檔可以分為三類:src下的設定檔、絕對路徑的設定檔、相對路徑的設定檔。我們一一給例子。直接看代碼:

package com.herman.test;import java.io.File;import java.io.FileInputStream;import java.net.URL;import org.apache.logging.log4j.LogManager;import org.apache.logging.log4j.Logger;import org.apache.logging.log4j.core.config.ConfigurationSource;import org.apache.logging.log4j.core.config.Configurator;public class ConfigTest {private static Logger logger = LogManager.getLogger(ConfigTest.class);/** * log4j 2讀取設定檔 * log4j 2讀取的設定檔可以分為三類:src下的設定檔、絕對路徑的設定檔、相對路徑的設定檔 *///第一類  載入src下的設定檔public static void test0(){//src下的設定檔會預設的被log4j的架構載入,我們就不顯示的載入了//直接測試logger.info("我列印了.......");//輸出內容//2014-09-01 15:49:30,229 INFO  [main] test.ConfigTest (ConfigTest.java:18) - 我列印了.......}//第二類  絕對路徑的設定檔public static void test1(){//我們將log4j2.xml放在D盤下//這是需要手動的載入//絕對路徑設定檔ConfigurationSource source;try {//方法1  使用  public ConfigurationSource(InputStream stream) throws IOException 建構函式source = new ConfigurationSource(new FileInputStream("D:\\log4j2.xml"));//方法2 使用 public ConfigurationSource(InputStream stream, File file)建構函式File config=new File("D:\\log4j2.xml");source = new ConfigurationSource(new FileInputStream(config),config);//方法3 使用 public ConfigurationSource(InputStream stream, URL url) 建構函式String path="D:\\log4j2.xml";source = new ConfigurationSource(new FileInputStream(path),new File(path).toURL());//source.setFile(new File("D:\log4j2.xml"));//source.setInputStream(new FileInputStream("D:\log4j2.xml"));Configurator.initialize(null, source);Logger logger = LogManager.getLogger(ConfigTest.class.getName()); logger.trace("trace...");logger.debug("debug...");logger.info("info...");logger.warn("warn...");logger.error("error...");logger.fatal("fatal...");//一下是運行效果/*2014-09-01 16:03:07,331 DEBUG [main] test.ConfigTest (ConfigTest.java:42) - debug...2014-09-01 16:03:07,331 INFO  [main] test.ConfigTest (ConfigTest.java:43) - info...2014-09-01 16:03:07,331 WARN  [main] test.ConfigTest (ConfigTest.java:44) - warn...2014-09-01 16:03:07,331 ERROR [main] test.ConfigTest (ConfigTest.java:45) - error...2014-09-01 16:03:07,331 FATAL [main] test.ConfigTest (ConfigTest.java:46) - fatal...*/} catch (Exception e) {e.printStackTrace();}}//第三類  相對路徑的設定檔載入public static void test2(){//這裡需要注意路徑中不要出現中文和空格,如果存在中文,請使用url轉碼ConfigurationSource source;try {//方法1  使用getResource()String path="/com/herman/config/log4j2.xml";URL url=ConfigTest.class.getResource(path);source = new ConfigurationSource(new FileInputStream(new File(url.getPath())),url);Configurator.initialize(null, source);//方法2 使用System.getPropertyString config=System.getProperty("user.dir");source = new ConfigurationSource(new FileInputStream(config+"\\src\\com\\herman\\config\\log4j2.xml"));Configurator.initialize(null, source);//輸出內容/*2014-09-01 16:32:19,746 DEBUG [main] test.ConfigTest (ConfigTest.java:53) - debug...2014-09-01 16:32:19,746 INFO  [main] test.ConfigTest (ConfigTest.java:54) - info...2014-09-01 16:32:19,746 WARN  [main] test.ConfigTest (ConfigTest.java:55) - warn...2014-09-01 16:32:19,746 ERROR [main] test.ConfigTest (ConfigTest.java:56) - error...2014-09-01 16:32:19,746 FATAL [main] test.ConfigTest (ConfigTest.java:57) - fatal...*/} catch (Exception e) {e.printStackTrace();}}public static void main(String[] args) {//test0();//test1();test2();}}
很簡單吧!歡迎大家關注我的個人部落格!!!!如有不懂,疑問或者欠妥的地方,請加QQ群:135430763   進行反饋,共同學習!
怎自訂擷取log4j的設定檔

??1.???????????????? 設定檔的載入是在第一次使用,因此在系統工程第一次使用log4j的時候就要指明載入的設定檔的路徑??2.???????????????? 在linux伺服器上使用的是相對路徑,可以指明設定檔的方法??第一種方式://指明設定檔的載入目錄載入.properties檔案PropertyConfigurator.configure("E://log4j.properties");??第二種方式:/**?????? ??*初始化log4j配置資訊?????? ??*@throwsException?????? ??*/?????? publicstaticvoid initLog4j() throws Exception?????? {???????????? FileInputStream istream = null;???????????? try???????????? {???????????????????? Properties props = new Properties();//指明了擷取設定檔的路徑???????????????????? istream = new FileInputStream("conf/log4j.properties");???????????????????? props.load(istream);//從輸入資料流中讀取屬性列表???????????????????? PropertyConfigurator.configure(props);???????????? } catch (Exception ex)???????????? {???????????????????? log.error("initLog4j.error:", ex);???????????????????? thrownew Exception(ex);???????????? } finally???????????? {???????????????????? istream.close();???????????? }?????? }??使用上面的方法可以實現動態添加配置資訊如何?動態指定Log存放的路徑,即不用寫死?
 
log4jproperties設定檔是怎讀取的?

問錯地方了,這個應該到JAVA的板塊提問
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.