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}
<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>
You may specify the location of the default configuration file with a system property named "logback. configurationFile". The value of this property can be a URL, a resource on the class path or a path to a file external to the application.
In a Logback. xml file, all the configuration options are enclosed within the <configuration> root element. In the root element, you can set the debug=true attribute to inspect Logback's internal status. You can also configure auto scanning of the configuration file by setting the scan=true attribute.
xml , you can use <springProperty> to access properties from Spring's environment including those configured in application. properties . This is described in the documentation: The tag allows you to surface properties from the Spring Environment for use within Logback.
Appenders place log messages in their final destinations. A Logger can have more than one Appender. We generally think of Appenders as being attached to text files, but Logback is much more potent than that. Layout prepares messages for outputting.
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:
<property name="MY_HOME" value="/home/myhome"/>
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:
<property file="/opt/example/instance_1/properties/system.properties" />
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.
<property resource="prod.properties" />
Using the property definer
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;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With