Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

logback doesn't find maven's property project.build.directory

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?

like image 262
mark951131b Avatar asked Dec 16 '15 13:12

mark951131b


2 Answers

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

  1. Define a property in the pom to hold the location of your logfile:

    enter image description here

  2. Use the property in the logback.xml appender:

enter image description here

  1. Enable maven resource filtering so that any maven properties defined in the the resources being scanned are replaced with their corresponding values

enter image description here

like image 114
Ashutosh Jindal Avatar answered Sep 20 '22 11:09

Ashutosh Jindal


${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.

like image 42
luboskrnac Avatar answered Sep 17 '22 11:09

luboskrnac