在logback.xml中有没有办法通过classpath:指定文件日志目的地,没有绝对路径? [英] Is there a way in logback.xml to specify file log destination through classpath:, without absolute path?

查看:364
本文介绍了在logback.xml中有没有办法通过classpath:指定文件日志目的地,没有绝对路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的logback.xml配置文件中这个appender:

I've in my logback.xml configuration file this appender:

<appender name="FILE"
            class="ch.qos.logback.core.rolling.RollingFileAppender">
    <file>classpath:addressbookLog.log</file>
    <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
      <Pattern>%d{dd MMM yyyy;HH:mm:ss} %-5level %logger{36} - %msg%n
      </Pattern>
    </encoder>
    <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
      <FileNamePattern>classpath:addressbookLog.%i.log.zip</FileNamePattern>
      <MinIndex>1</MinIndex>
      <MaxIndex>10</MaxIndex>
    </rollingPolicy>

    <triggeringPolicy
      class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
      <MaxFileSize>2MB</MaxFileSize>
    </triggeringPolicy>
  </appender>

因此我指定文件的路径,以相对的方式通过类路径打印日志,但它不起作用,没有创建和写入文件addressbookLog.log。
它仅适用于/home/andrea/.../resources/addressbookLog.log等绝对路径
您对如何使用classpath有任何想法吗?

so that I specify path to file in which to print logs in a relative way through classpath, but it doesn't work, no file addressbookLog.log is created and written. It only works with absolute paths like /home/andrea/.../resources/addressbookLog.log Have you any ideas on how to make it work with classpath?

推荐答案

第3章:Logback配置:变量替换告诉我们引用外部定义的变量的各种方法,例如系统属性 classpath

The Chapter 3: Logback configuration: Variable substitution told us the various ways to refer to the variable defined outside, e.g. system properties and classpath.

重要配置是创建一个包含所有变量的单独文件。我们可以在类路径上引用资源而不是文件。例如

The significant configuration is creating a separate file that will contain all the variables. We can make a reference to a resource on the class path instead of a file as well. e.g.

<configuration>

  <property resource="resource1.properties" />

  <appender name="FILE" class="ch.qos.logback.core.FileAppender">
     <!-- Here we can refer to the variable 
      defined at the resource1.properties -->
     <file>${USER_HOME}/myApp.log</file>
     <encoder>
       <pattern>%msg%n</pattern>
     </encoder>
   </appender>

   <root level="debug">
     <appender-ref ref="FILE" />
   </root>
</configuration>



外部属性文件(resource1.properties)



The external properties file (resource1.properties)

USER_HOME=/path/to/somewhere

请注意, resource1.properties 是一个资源,可在类路径中找到。

Please note that the resource1.properties is a resource which available at the classpath.

您可以参考第3章:Logback配置中的完整版本:变量替换。我希望这可能有所帮助。

You may refer to the full version at Chapter 3: Logback configuration: Variable substitution. I hope this may help.

这篇关于在logback.xml中有没有办法通过classpath:指定文件日志目的地,没有绝对路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆