I have a small project with Spring Boot and maven, and now I'm trying to configure logback to write to a file. I want it to write to a file given by ${project.build.directory}/${log.folder}/logfile.log
, so a subfolder of the build directory, being ${log.folder}
a property that I specify in an application.properties
file, places under my /resources
folder.
This is my logback.xml
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<include resource="org/springframework/boot/logging/logback/base.xml"/>
<logger name="org.springframework.boot" level="INFO"/>
<logger name="org.springframework.security" level="ERROR"/>
<logger name="org.glassfish.jersey" level="DEBUG"/>
<property resource="application.properties"/>
<appender name="DUMMY_APPENDER" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${project.build.directory}/${log.folder}/logfile.log</file>
<encoder>
<pattern>%d{"yyyy-MM-dd HH:mm:ss,SSS zzz"}, [%thread] %-5level %logger{5} - %msg%n
</pattern>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${log.folder}/spring.log.%d</fileNamePattern>
</rollingPolicy>
</appender>
<logger name="xxxxxx" level="INFO" additivity="false">
<appender-ref ref="DUMMY_APPENDER"/>
</logger>
<root level="INFO">
<appender-ref ref="DUMMY_APPENDER"/>
</root>
</configuration>
It writes the logs, but my problem is that when I run the application, it creates a folder project.build.directory_IS_UNDEFINED, then places my log.folder under it. It says in the documentation, that
As its build tool, logback relies on Maven, a widely-used open-source build tool.
And when, in the logback.xml, I start typing ${pro... then my IDE displays a set of available maven implicit properties.
So it should work, but it doesn't. Any idea why?
It doesn't work due to what @luboskmac said.
Here is how to make it work. I have reproduced the problem and pushed up a solution here : https://github.com/ajorpheus/logback-maven/releases
Here is a summary of the fix : https://github.com/ajorpheus/logback-maven/commit/f245e5a6f4c13f9ba8e161c5452d296b653977d0
Define a property in the pom to hold the location of your logfile:
Use the property in the logback.xml
appender:
${project.build.directory}
is Maven's property, which is available only during the maven build. On the other hand, Logback is being used on runtime of the application. So obviously ${project.build.directory}
is not defined on runtime.
You need to pick location which will be tailored to runtime environment and your deployment topology.
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