Logback does not find maven 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 be written to the file specified by ${project.build.directory}/${log.folder}/logfile.log , so the subfolder of the assembly directory, which is ${log.folder} , is the property that I specify in file application.properties , is placed in 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 logs, but my problem is that when the application starts, it creates the project.build.directory_IS_UNDEFINED folder and then puts my log file under it. He says in the documentation that

As a build tool, logback is based on Maven, the widely used open-source build tool.

And when I start typing $ {pro ... in logback.xml, then my IDE displays the set of available implicit maven properties .

Therefore, it should work, but it is not. Any idea why?

+5
source share
3 answers

This does not work due to what @luboskmac said.

Here's how to do it. I reproduced the problem and clicked the 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 the property in pom to save the location of your log file:

    enter image description here

  • Use the property in app logback.xml :

enter image description here

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

enter image description here

+4
source

${project.build.directory} is a Maven property that is only available during maven build. Logback, on the other hand, is used at runtime. Thus, it is obvious that ${project.build.directory} not defined at runtime.

You need to choose a location that will be adapted to the runtime and your deployment topology.

+2
source

You can use the properties of the deployment container.

Like for tomcat

 ${catalina.base}/logs 
0
source

Source: https://habr.com/ru/post/1238386/


All Articles