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")); }
}

  

请看下面这段配置,这是无法工作的:

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

使用该配置,不会生成任何日志文件,这可能是 LogBack 的 bug,解决的办法就是使用绝对路径,你可以用一些系统变量来代替,例如:

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

现在就好了,希望对某些使用 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


  

logback读取src/test/resource下的配置文件的更多相关文章

  1. [转]Maven项目读取src.main.resources下的文件

    要取编译后的路径,而不是你看到的src/main/resources的路径.如下: URL url = MyTest.class.getClassLoader().getResource(" ...

  2. Java web 项目读取src或者tomcat下class文件夹下的xml文件或者properties文件

    //生成一个文件对象: File file = new File(getClass().getClassLoader().getResource("test.xml").getPa ...

  3. 读取用户家目录下的配置文件到properties

    String conf = System.getProperty("user.home") + File.separator + "a.properties"; ...

  4. Java读取Maven工程下的配置文件,工具类

    Java开发中,经常需要在maven工程中读取src/main/resources下的配置文件: 思路如下: Class.getClassLoader() 返回类加载器ClassLoader,进而可以 ...

  5. 分享知识-快乐自己:IDEA下maven编译打包Java项目成jar包但是resource下配置文件无法编译

    今天在写分布式项目的时候,一直无法编译 resource 下的配置文件:(在target文件夹下的 classes文件查看是否编译) 最后只能通过在POM文件中配置resources配置 得以解决: ...

  6. maven项目无法读取src/main/java目录下的配置文件解决方法

    我们在用Mybatis去操作底层数据库的时候,需要用到xml配置文件,一般我们是把配置文件和dao放置在同一层目录.但是在用idea操作maven项目的时候,我们可能会遇到无法读取到dao对应的map ...

  7. maven工程读取resource下配置文件

    maven工程读取resource下配置文件 在maven工程中,我们会将配置文件放到,src/main/resources   下面,例如 我们需要确认resource 下的文件 编译之后存放的位置 ...

  8. java读取resource目录下的配置文件

    java读取resource目录下的配置文件 1:配置resource目录 下的文件 host: 127.0.0.1 port: 9300 2:读取    / 代表resource目录 InputSt ...

  9. Maven项目中读取src/main/resources目录下的配置文件

    在Maven项目的开发中,当需要读取src/下的配置文件时,该怎么做? 我们假设Resources下有一个文件名为kafka.properties的配置文件(为什么用kafka.properties, ...

随机推荐

  1. zTree实现删除树节点

    zTree实现删除树节点 1.实现源码 <!DOCTYPE html> <html> <head> <title>zTree实现基本树</titl ...

  2. eclipse和android studio的爱恨情仇

    Eclipse,以下简称ES(自己起的,不喜勿喷):Android studio,以下简称AS(都这么叫的啦)! 2000年,IBM怀胎24个月,终于产生了Eclipse,当时ES的诞生只是为了解决I ...

  3. RHEL部署ipa红帽身份验证

    1.先下载必须包 yum install -y ipa-server bind bind-dyndb-ldap 2.初始化ipa基本配置 ipa-server-install * Configure ...

  4. meta的各种参数

    <!DOCTYPE html> <!-- 使用 HTML5 doctype,不区分大小写 --> <html lang="zh-cmn-Hans"&g ...

  5. C# 找出泛型集合中的满足一定条件的元素 List.Wher()

    在学习的过程中,发现泛型集合List<T>有一个Where函数可以筛选出满足一定条件的元素,结合Lambda表达式使用特别方便,写出来与大家分享. 1.关于Func<> Fun ...

  6. Query 插件为什么要return this.each()

    jQuery.fn.test2= function(){ this.css("background","#ff0");//这里面的this为jquery对象,而 ...

  7. 异常-----freemarker.template.TemplateException: Error executing macro: write

    freemarker自定义标签 1.错误描述 六月 05, 2014 11:31:35 下午 freemarker.log.JDK14LoggerFactory$JDK14Logger error 严 ...

  8. 简单使用Mysql-Cluster-7.5搭建数据库集群

    阅读目录 前言 mysql cluster中的几个概念解释 架构图及说明 下载mysql cluster 安装mysql cluster之前 安装配置管理节点 安装配置数据和mysql节点 测试 启动 ...

  9. Xcode 9.0 新增功能大全

    Xcode是用于为Apple TV,Apple Watch,iPad,iPhone和Mac创建应用程序的完整开发人员工具集.Xcode开发环境采用tvOS SDK,watchOS SDK,iOS SD ...

  10. 【SPOJ】Distinct Substrings(后缀自动机)

    [SPOJ]Distinct Substrings(后缀自动机) 题面 Vjudge 题意:求一个串的不同子串的数量 题解 对于这个串构建后缀自动机之后 我们知道每个串出现的次数就是\(right/e ...