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

2

11

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?

Portal answered 8/6, 2017 at 13:1 Comment(4)
Please wrap your path argument with doule quote ("). It should looks like -Dlog.dir="foo/logs"Mouseear
@eg04lt3r, Thanks, but this is not the problem.Portal
I think that you can only override logback file with right property. Please check this link #32659135.Mouseear
This involves having a different logback.xml file per environment, which is also not what I'd like to do. From what I understand, Logback supports properties, which can be used inside the logback.xml file. I would like to be able to have a default value defined for a property inside the logback.xml for the cases where this is not being overriden from the console via -Dproperty1=.....Portal
G
16

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"

Gabriellegabrielli answered 26/1, 2018 at 4:56 Comment(0)
P
2

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).

Prelude answered 8/6, 2017 at 13:15 Comment(3)
Thanks, but what you're talking about is Maven resource filtering and I'm not looking to do resource filtering.Portal
You are welcome but the way where you want to use to value your property placeholder : <file>${log.dir}/logs/my.log</file> looks like file filtering feature.Prelude
It's documented in the Logback docs for properties/variables and it's a better solution than Maven filtering as the values is not replaced in the actual resources file (which is what will happen with the Maven filtering).Portal

© 2022 - 2024 — McMap. All rights reserved.