I am new to Logback, I am trying to add file path dynamically, with a property file, for both windows and Linux.

Here is the code sinppet I have, how can I get the value to ${MY_HOME}

${MY_HOME}/server.log

true

%d [%thread] %-5level %logger{35} - %msg%n

解决方案

Typically this is a system property, there are some answers that touch on this but only provide one part of the answer. These are:

But the manual on configuration shows that the mechanism is quite flexible

As in many scripting languages, logback configuration files support definition and substitution of variables. Variables can be defined within the configuration file itself, in an external file, in an external resource or even computed and defined on the fly.

In summary you have a number of options for defining the value of MY_HOME:

In the file

You are able to define the value in the file itself with:

In the system properties

You can arrange for it to be set as a system property, most likely when you start the JVM.

java -DMY_HOME="/home/myhome" ...

From a property file on your system

You can arrange for logback to read a property file:

From the classpath

You can write a properties file into a resources directory or into a jar and read it out as a resource, using the classpath.

Using the property definer

You can arrange to call into your code, by using a property definer. For example:

app

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;

}

}

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐