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 version="1.0" encoding="UTF-8" ?>
- <configuration>
- <contextName>JTheque</contextName>
- <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
- <file>logs/jtheque.log</file>
- <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
- <FileNamePattern>logs/jtheque.%i.log.zip</FileNamePattern>
- <MinIndex>1</MinIndex>
- <MaxIndex>5</MaxIndex>
- </rollingPolicy>
- <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
- <MaxFileSize>5MB</MaxFileSize>
- </triggeringPolicy>
- <layout class="ch.qos.logback.classic.PatternLayout">
- <Pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</Pattern>
- </layout>
- </appender>
- <root level="DEBUG">
- <appender-ref ref="FILE"/>
- </root>
- </configuration>
使用該配置,不會生成任何日志文件,這可能是 LogBack 的 bug,解決的辦法就是使用絕對路徑,你可以用一些系統變量來代替,例如:
- ...
- <file>${user.dir}/logs/jtheque.log</file>
- <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
- <FileNamePattern>${user.dir}/logs/jtheque.%i.log.zip</FileNamePattern>
- <MinIndex>1</MinIndex>
- <MaxIndex>5</MaxIndex>
- </rollingPolicy>
- ...
現在就好了,希望對某些使用 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