Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to define a variable in Logback with a default value and override it?

I would like to define a property/variable in Logback (1.2.1) that:

  • Has a default value
  • Can be overriden via a Java command-line option

Basically, during development, I would like Maven to be invoking the maven-surefire-plugin with something like this:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <configuration>
        <systemPropertyVariables>
            <log.dir>${project.build.directory}/logs</log.dir>
        </systemPropertyVariables>
    </configuration>
</plugin>

(I am sure that the above is working fine, because I have other properties being passed in for the tests this way and those work as expected).

At the moment, I have the following logback.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration>
<configuration scan="true" scanPeriod="30 seconds" debug="false">

    <property name="log.dir" value="."/>

    <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${log.dir}/logs/my.log</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
            <fileNamePattern>strongbox_%d{yyyy-MM-dd}.%i.log</fileNamePattern>

            <fileNamePattern>strongbox-%d{yyyy-MM-dd}.%i.log</fileNamePattern>
            <maxFileSize>128MB</maxFileSize>
            <maxHistory>31</maxHistory>
            <totalSizeCap>1GB</totalSizeCap>

            <maxHistory>30</maxHistory>
        </rollingPolicy>

        <encoder>
            <charset>UTF-8</charset>
            <pattern>%d{HH:mm:ss.SSS dd-MM-yyyy} | %-5.5p | %-20.20t | %-50.50logger{50} | %m%n</pattern>
        </encoder>
    </appender>
    ...
</configuration>

The -Dlog.dir=foo/logs, it is simply being ignored and the log file is being produced in the current directory. What am I doing wrong here? Does it need a scope? Does it need an if condition to be set up?

like image 330
carlspring Avatar asked Jun 08 '17 13:06

carlspring


People also ask

How do you specify Logback XML in application properties?

If you name your configuration file logback-spring. xml , rather than logback. xml , you can use <springProperty> to access properties from Spring's environment including those configured in application. properties .

What is Appender in Logback?

The Logback architecture is comprised of three classes: Logger, Appender, and Layout. A Logger is a context for log messages. This is the class that applications interact with to create log messages. Appenders place log messages in their final destinations. A Logger can have more than one Appender.

What is root tag in Logback?

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.


2 Answers

If you need a variable, say called log.file.root, which should default to a value app-logs, define it as:

<property name="LOG_ROOT" value="${log.file.root:-app-logs}" />

Then use ${LOG_ROOT} wherever you need to.

This variable can be overridden via the command line by:

-Dlog.file.root=/home/user/logs

Reference: https://logback.qos.ch/manual/configuration.html -> "Default values for variables"

like image 183
Sujay Anjankar Avatar answered Sep 23 '22 02:09

Sujay Anjankar


You specify the property for the maven-surefire-plugin.
So the property will be bound only for this plugin execution :

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <configuration>
        <systemPropertyVariables>
            <log.dir>${project.build.directory}/logs</log.dir>
        </systemPropertyVariables>
    </configuration>
</plugin>

In your case, you want that the property be bound whatever the executed plugin. Not only for test executions.

So you should use properties declared in the <build> tag of the pom.

<build>
      ...
  <properties>
     <log.dir>${project.build.directory}/logs</log.dir>
  </properties>
      ...
</build>

In order that the Maven property used in the logback configuration be replaced by the actual value computed from the pom, you have to enable the Maven resources filtering :

  ...
  <resource>
    <directory>src/main/resources</directory>
    <filtering>true</filtering>
  </resource>
  ...

And the Logback configuration has to be located inside the src/main/resources folder of course (this folder or a child).

like image 20
davidxxx Avatar answered Sep 25 '22 02:09

davidxxx