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:gitdaotiedaotie	argetclasses  
        File f = new File(this.getClass().getResource("/").getPath());  
        System.out.println(f);  
  
        // 获取当前类的所在工程路径; 如果不加“/”  获取当前类的加载目录  D:gitdaotiedaotie	argetclassesmy  
        File f2 = new File(this.getClass().getResource("").getPath());  
        System.out.println(f2);  
  
        // 第二种:获取项目路径    D:gitdaotiedaotie  
        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:gitdaotiedaotie  
        System.out.println(System.getProperty("user.dir"));  
         /* 
          * 结果: C:Documents and SettingsAdministratorworkspaceprojectName 
          * 获取当前工程路径 
          */  
  
        // 第五种:  获取所有的类路径 包括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


  

原文地址:https://www.cnblogs.com/luffystory/p/7979442.html