问题描述
我是Logback的新手,我试图为Windows和Linux动态添加带有属性文件的文件路径.
I am new to Logback, I am trying to add file path dynamically, with a property file, for both windows and Linux.
这是我拥有的代码sinppet,如何将值获取到$ {MY_HOME}
Here is the code sinppet I have, how can I get the value to ${MY_HOME}
<appender name="SERVER_FILE" class="ch.qos.logback.core.FileAppender">
<file>${MY_HOME}/server.log</file>
<append>true</append>
<encoder>
<pattern>%d [%thread] %-5level %logger{35} - %msg%n</pattern>
</encoder>
</appender>
推荐答案
通常,这是系统属性,有一些与此相关的答案,但仅提供了一部分答案.这些是:
Typically this is a system property, there are some answers that touch on this but only provide one part of the answer. These are:
- Logback.xml configuration
- logback how to set destination folder for log files
但是配置上的手册显示该机制非常灵活
But the manual on configuration shows that the mechanism is quite flexible
总而言之,您可以使用许多选项来定义MY_HOME的值:
In summary you have a number of options for defining the value of MY_HOME:
在文件中
您可以使用以下命令在文件本身中定义值:
You are able to define the value in the file itself with:
<property name="MY_HOME" value="/home/myhome"/>
在系统属性中
您可以安排将其设置为系统属性,最有可能在启动JVM时使用.
You can arrange for it to be set as a system property, most likely when you start the JVM.
java -DMY_HOME="/home/myhome" ...
从系统上的属性文件中
您可以安排登录以读取属性文件:
You can arrange for logback to read a property file:
<property file="/opt/example/instance_1/properties/system.properties" />
从类路径
您可以使用类路径将属性文件写入资源目录或jar中,并将其作为资源读出.
You can write a properties file into a resources directory or into a jar and read it out as a resource, using the classpath.
<property resource="prod.properties" />
使用属性定义器
您可以使用属性定义器来安排调用您的代码.例如:
You can arrange to call into your code, by using a property definer. For example:
<define name="MY_HOME" class="biz.nowhere.HomePropertyDefiner">
<application>app</application>
</define>
该类类似(例如)的地方:
Where that class is something like (as an example):
public class HomePropertyDefiner extends PropertyDefinerBase {
private String application;
@Override
public String getPropertyValue() {
return String.format("/opt/%s/%s", application, MyInstanceManager.instancePath());
}
public void setApplication(String application) {
this.application = application;
}
}
这篇关于如何在logback.xml中动态指定文件路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!