Home > Mobile >  How to set path to logback configuration file in spring bootRun task in gradle
How to set path to logback configuration file in spring bootRun task in gradle

Time:12-16

I am currently trying to restructure my Spring Boot project so that all the configuration files are no longer bundled in the jar. However, I still want the bootRun task to make use of them.

For the application.properties, I was able to do that quite easily by pulling it out of the src/resources folder and onto the surface of the project (for now) and adding this to the build.gradle:

tasks.named("bootRun") {
    systemProperty "spring.config.location", "file:$projectDir/"
    mainClass = "my.project.MyMainClass"
}

For the logback configuration, I was trying the same thing, so:

tasks.named("bootRun") {
    systemProperty "spring.config.location", "file:$projectDir/"
    systemProperty "logback.configurationFile", "file:$projectDir/"
    mainClass = "my.project.MyMainClass"
}

...however, that did not work at all. Now instead of the logback configuration that I configured I just get the default configuration.

What am I doing wrong here? Based on what I've managed to find, this should work. My logback-spring.xml is located directly next to the application.properties.

Variations which I've also tried to the same end:

    systemProperty "logback.configurationFile", "file:$projectDir/logback-spring.xml"
    systemProperty "logback.configurationFile", "$projectDir/logback-spring.xml"
    systemProperty "logback.configurationFile", "C:/projects/myProject/logback-spring.xml"
    systemProperty "logback.configurationFile", "file:C:/projects/myProject/logback-spring.xml"

Update:

I have now debugged it a bit and arrived at this, which I feel even more should work, but doesn't:

tasks.named("bootRun") {
    println "Hi, I'm BootRun!"
    System.setProperty("spring.config.location", "file:$projectDir/")
    println "System Property spring.config.location is: "   System.getProperty('spring.config.location')
    System.setProperty("Logback.configurationFile", "$projectDir\\logback-spring.xml")
    println "System Property Logback.configurationFile is: "   System.getProperty('Logback.configurationFile')
    mainClass = "myProject.MyMainClass"
}

This gives me this output at the beginning of each build, way before Spring starts up:

> Configure project :
$0.1.0-SNAPSHOT$
Hi, I'm BootRun!
System Property spring.config.location is: file:C:\projects\myProject/
System Property Logback.configurationFile is: C:\projects\myProject\logback-spring.xml

> Task :createVersionFile
createVersionFile called. Path C:\projects\myProject\src\main\java\myProject

> Task :compileJava
> Task :processResources NO-SOURCE
> Task :classes

> Task :bootRun

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v2.7.4)
[...}

It feels like this should work to me, because the path to the logback-spring.xml. But could it maybe be an issue because it's run as part of a different task? That would feel weird because those are system variables...

CodePudding user response:

I think you should use logging.config instead of logback.configurationFile.

https://docs.spring.io/spring-boot/docs/2.7.6/reference/html/features.html#features.logging.custom-log-configuration

This one got me in trouble too once, but I was lucky to receive an error message: https://github.com/spring-projects/spring-boot/issues/4969

  • Related