logback讀取src/test/resource下的配置文件


import java.io.File;  
import java.net.URISyntaxException;  
import java.util.Map;  
import java.util.Properties;  
  
//java在gradle工程訪問src/test/resources或者src/main/resources目錄下的資源配置文件  
public class  TestMain  
{  
    public static  void main(String args[]) throws URISyntaxException {  
        System.out.println(new File(".").getAbsolutePath());  
        Properties properties=new Properties();  
        try {  
            // properties.load(new FileInputStream("config.properties"));  
            System.out.println(TestMain.class.getResource("/config.properties").toExternalForm());  
            System.out.println(Thread.currentThread().getContextClassLoader().getResource("config.properties"));  
            properties.load(TestMain.class.getResource("/config.properties").openStream());  
  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
        String version=properties.getProperty("version");  
        System.out.println(version);  
        for(Map.Entry<Object,Object>  entry:properties.entrySet())  
        {  
            Object key=entry.getKey();  
            Object value=entry.getValue();  
            System.out.println(key+"="+value);  
        }  
    }  
  
}  

import java.io.File;  
import java.io.IOException;  
import java.net.URL;  
  
public class MyUrlDemo {  
  
  
    public static void main(String[] args) {  
        MyUrlDemo muDemo = new MyUrlDemo();  
        try {  
            muDemo.showURL();  
        } catch (IOException e) {  
            // TODO Auto-generated catch block  
            e.printStackTrace();  
        }  
    }  
  
    public void showURL() throws IOException {  
  
        // 第一種:獲取類加載的根路徑   D:\git\daotie\daotie\target\classes  
        File f = new File(this.getClass().getResource("/").getPath());  
        System.out.println(f);  
  
        // 獲取當前類的所在工程路徑; 如果不加“/”  獲取當前類的加載目錄  D:\git\daotie\daotie\target\classes\my  
        File f2 = new File(this.getClass().getResource("").getPath());  
        System.out.println(f2);  
  
        // 第二種:獲取項目路徑    D:\git\daotie\daotie  
        File directory = new File("");// 參數為空  
        String courseFile = directory.getCanonicalPath();  
        System.out.println(courseFile);  
  
  
        // 第三種:  file:/D:/git/daotie/daotie/target/classes/  
        URL xmlpath = this.getClass().getClassLoader().getResource("");  
        System.out.println(xmlpath);  
  
  
        // 第四種: D:\git\daotie\daotie  
        System.out.println(System.getProperty("user.dir"));  
         /* 
          * 結果: C:\Documents and Settings\Administrator\workspace\projectName 
          * 獲取當前工程路徑 
          */  
  
        // 第五種:  獲取所有的類路徑 包括jar包的路徑  
        System.out.println(System.getProperty("java.class.path"));  
  
    }  
}  

  

請看下面這段配置,這是無法工作的:

 

[XML]  view plain  copy
 
  1. <?xml version="1.0" encoding="UTF-8" ?>  
  2. <configuration>  
  3.     <contextName>JTheque</contextName>  
  4.    
  5.     <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">  
  6.         <file>logs/jtheque.log</file>  
  7.    
  8.         <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">  
  9.             <FileNamePattern>logs/jtheque.%i.log.zip</FileNamePattern>  
  10.             <MinIndex>1</MinIndex>  
  11.             <MaxIndex>5</MaxIndex>  
  12.         </rollingPolicy>  
  13.    
  14.         <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">  
  15.             <MaxFileSize>5MB</MaxFileSize>  
  16.         </triggeringPolicy>  
  17.    
  18.         <layout class="ch.qos.logback.classic.PatternLayout">  
  19.             <Pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</Pattern>  
  20.         </layout>  
  21.     </appender>  
  22.    
  23.     <root level="DEBUG">  
  24.         <appender-ref ref="FILE"/>  
  25.     </root>  
  26. </configuration>  

使用該配置,不會生成任何日志文件,這可能是 LogBack 的 bug,解決的辦法就是使用絕對路徑,你可以用一些系統變量來代替,例如:

 

[XML]  view plain  copy
 
  1. ...  
  2. <file>${user.dir}/logs/jtheque.log</file>  
  3.   
  4. <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">  
  5.     <FileNamePattern>${user.dir}/logs/jtheque.%i.log.zip</FileNamePattern>  
  6.     <MinIndex>1</MinIndex>  
  7.     <MaxIndex>5</MaxIndex>  
  8. </rollingPolicy>  
  9. ...  

現在就好了,希望對某些使用 LogBack 的人有幫助。

 

其實使用相對路徑是能產生日志文件的,只是這個相對路徑是相對與Eclipse(我是使用eclipse開發的,在eclipse啟動的),我發現日志全部跑到eclipse安裝目錄里面去了

 

不過看樣子,logback是不推薦使用相對路徑來記錄日志文件,個人覺得確實使用一些環境變量來引用絕對路徑要更好控制一點

 

tomcat下可以用:${catalina.base}/logs/your_log.log

 

  一直使用相對路徑. 沒發現問題.  使用你這個配置也沒問題. 可能你用的版本比較老,用最新的時會有警告,
  http://logback.qos.ch/codes.html#layoutInsteadOfEncoder
   ps:  logback在當打包時目錄不存在時不會自動創建的目錄,  需要做小小的修改才行.

 

 

1, 把日志發送到郵件中

2, 把日志保存到數據庫中(有異步么?)

 

官方文檔有: http://logback.qos.ch/manual/appenders.html


  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM