Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to specify file path Dynamically in logback.xml

Tags:

logback

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> 
like image 305
user2108383 Avatar asked Sep 15 '15 21:09

user2108383


People also ask

How do I specify the path for Logback xml?

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.

How do I setup a Logback xml file?

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.

How do I use application properties in Logback xml?

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.

What is Appender in 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.


1 Answers

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

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;
  }

}
like image 169
andygavin Avatar answered Sep 23 '22 17:09

andygavin