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. HI3531的nand flash测试

    void NAND_Init() {    *(unsigned int *)(0x20030000 + 0xd0) = 7; delay_x(0X5000);    *(unsigned int * ...

  2. Android项目中的各个模块框架设计

    作为Android开发,现对项目开发中的各个模块搭建,梳理如下: Android UI框架,开发人员需要达到专家级 网络框架 浏览框架 图片加载框架 图片裁剪压缩工具类 客户端并发框架 线程池设计 ( ...

  3. select(Linux 编程)

    select系统调用时用来让我们的程序监视多个文件句柄的状态变化的.程序会停在select这里等待,直到被监视的文件句柄有一个或多个发生了状态改变. 关于文件句柄,其实就是一个整数,通过socket函 ...

  4. SCRIPT5007:无法获取属性“show”的值,对象为null或未定义

    1.错误描述 SCRIPT5007:无法获取属性"show"的值,对象为null或未定义            dojo.js,行15.字符11808 2.错误原因    requ ...

  5. JustMock .NET单元测试利器(三)用JustMock测试你的应用程序

    用JustMock测试你的应用程序 本主题将指导您通过几个简单的步骤来使用Telerik®JustMock轻松测试您的应用程序.您将理解一个简单的原理,称为Arrange / Act / Assert ...

  6. 觉得OpenStack的网络复杂?其实你家里就有同样一个网络

    当你想了解OpenStack的Neutron网络,打开下面这张图的时候,心里一定是崩溃的,看起来这些模块连在一起很复杂,但其实和你家里的网络很像,看不出来?看我来慢慢解析. 其实这个网络的样子更像是我 ...

  7. python与机器学实践-何宇健 源代码及过程中遇到的问题

    # -*- coding: utf-8 -*-"""Spyder EditorThis is a temporary script file.""&q ...

  8. createjs绘制扇形的方法

    扇形由三段线条组成,两条直线和一条弧线,直线可以用createjs中的lineTo函数画出,弧线用Graphics.arc函数来画. 一.关于createjs中的Graphics.Arc API Gr ...

  9. 简单探讨 javascript 闭包

    函数作为返回值 高阶函数除了可以接受函数作为参数外,还可以把函数作为结果值返回. 我们来实现一个对Array的求和.通常情况下,求和的函数是这样定义的: function sum(arr) { ret ...

  10. 【python学习笔记】6.抽象

    [python学习笔记]6.抽象 创建函数: 使用def语句定义函数,不用声明参数类型,和返回值类型 def function_name(param1, param2): 'this is docum ...