I would like to define a property/variable in Logback (1.2.1) that:
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?
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 .
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.
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.
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"
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).
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