Java logback.xml 中有没有办法通过 classpath: 指定文件日志目的地,没有绝对路径?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16480052/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there a way in logback.xml to specify file log destination through classpath:, without absolute path?
提问by andPat
I've in my logback.xml configuration file this appender:
我在我的 logback.xml 配置文件中有这个 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>
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?
这样我就可以通过类路径指定以相对方式打印日志的文件路径,但它不起作用,没有创建和写入文件 addressbookLog.log。它只适用于绝对路径,如 /home/andrea/.../resources/addressbookLog.log 你有没有关于如何使它与类路径一起工作的想法?
采纳答案by Charlee Chitsuk
The Chapter 3: Logback configuration: Variable substitutiontold us the various ways to refer to the variable defined outside, e.g. system properties
and classpath
.
在第3章:配置的logback:变量替换告诉我们的各种方式来引用变量定义外,如system properties
和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.
重要的配置是创建一个包含所有变量的单独文件。我们也可以引用类路径上的资源而不是文件。例如
The logback.xml
logback.xml
<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>
The external properties file (resource1.properties)
外部属性文件 (resource1.properties)
USER_HOME=/path/to/somewhere
Please note that the resource1.properties
is a resource which available at the classpath
.
请注意,resource1.properties
是可在classpath
.
You may refer to the full version at Chapter 3: Logback configuration: Variable substitution. I hope this may help.
您可以参考第 3 章:Logback 配置:变量替换中的完整版本。我希望这可能会有所帮助。
回答by ratelgogo
<FileNamePattern>${user.dir}/logs/addressbookLog.%i.log.zip</FileNamePattern>
<FileNamePattern>${user.dir}/logs/addressbookLog.%i.log.zip</FileNamePattern>