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. Flex中的FusionCharts 2D面积图

    Flex中的FusionCharts 2D面积图 1.源码 <?xml version="1.0" encoding="utf-8"?> <s ...

  2. strstr()函数实现

    /* 函数要求:写一个函数模拟strstr()函数,设计中不得使用其他库函数. 函数原型:const char *strstr(const char *str1,const char *str2); ...

  3. C#中各种计时器 Stopwatch、TimeSpan

    1.使用 Stopwatch 类 (System.Diagnostics.Stopwatch)Stopwatch 实例可以测量一个时间间隔的运行时间,也可以测量多个时间间隔的总运行时间.在典型的 St ...

  4. freemarker之list遍历(八)

    1.设置数据源 /** * * @Title:student * @Description: * @param:@param name * @return: void * @throws */ pri ...

  5. 一个简单的freemark输入输出的案例(一)

    一. 创建FreeMarker模板文件user.ftl <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN&qu ...

  6. freemark标签中输出boolean值

    private boolean showHeader=true; public boolean getShowHeader(){ return this.showHeader; } public bo ...

  7. Luogu Dynamic Ranking (带修改的主席树)

    题目大意: 网址:https://www.luogu.org/problemnew/show/2617 给定一个序列a[1].a[2].....a[N],完成M个操作,操作有两种: [1]Q i j ...

  8. 【NOIP2016】【CJOJ2257】2257 愤怒的小鸟

    题目 Description https://www.luogu.org/problem/show?pid=2831 Kiana最近沉迷于一款神奇的游戏无法自拔. 简单来说,这款游戏是在一个平面上进行 ...

  9. [POJ2774]Long Long Message

    vjudge 一句话题意 给两个串,求最长公共子串. sol 把两个串接在一起求后缀数组.其实中间最好用一个没有出现过的字符连接起来. 判断如果\(SA[i]\)和\(SA[i-1]\)不属于同一个串 ...

  10. 【BZOJ3262】陌上花开(CDQ分治)

    [BZOJ3262]陌上花开(CDQ分治) 题解 原来放过这道题目,题面在这里 树套树的做法也请点上面 这回用CDQ分治做的 其实也很简单, 对于第一维排序之后 显然只有前面的对后面的才会产生贡献 那 ...