1

I use SLF4J with Logback in my desktop application.

Application can find configuration file (target/classes/logback.xml) and configure logger in a correct way. But when I change configuration file (<root level="debug">) and restart application nothing changes in my logger settings.

Here is my configuration:

<configuration debug="true" scan="true" scanPeriod="10 seconds">

    <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>${consoleLayoutPattern}</pattern>
        </encoder>
    </appender>

    <appender name="LOG_FILE"
        class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${fileName}</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>${filePattern}</fileNamePattern>
            <maxHistory>60</maxHistory>
            <totalSizeCap>3GB</totalSizeCap>
        </rollingPolicy>
        <encoder>
            <pattern>${rollingFilePatternLayoutPattern}</pattern>
        </encoder>
    </appender>

    <root level="error">
        <appender-ref ref="LOG_FILE" />
        <appender-ref ref="CONSOLE" />
    </root>

</configuration>

What did I do wrong? Is there any solution to trace jar inner xml configuration changes and refresh logging setting after application restart?

Oleksii Valuiskyi
  • 2,691
  • 1
  • 8
  • 22
  • 2
    It seems weird to me that the configuration is under `target`. If so you need to rebuild your project to see the changes in the log levels – A Le Dref Aug 20 '19 at 13:27
  • @A Le Dref I put it in the default configuration folder (src/main/resources) because desktop application have to have configuration in distributive – Oleksii Valuiskyi Aug 20 '19 at 13:38
  • Did you rebuild? –  Aug 20 '19 at 14:22
  • @jhell After rebuilding everything ok. But I need solution allow changing settings after application restart without rebuilding – Oleksii Valuiskyi Aug 20 '19 at 14:35
  • 1
    possible duplicate of https://stackoverflow.com/questions/13442967/how-to-dynamically-change-log-level-in-slf4j-or-log4j –  Aug 20 '19 at 14:39
  • Possible duplicate of [Configuring Log4j Loggers Programmatically](https://stackoverflow.com/questions/8965946/configuring-log4j-loggers-programmatically) –  Aug 20 '19 at 14:48

1 Answers1

2

In order not to re-build your project you have in fact to put your log configuration file outside of your project. I mean put a path to this file in your application properties.

If you're using Spring Boot you can configure the path to the log config file in the application.properties file like this :

logging.config=/home/path_to/logback.xml

Doing this when you will start your application it will use the one specified and you will not have to rebuild your project.

As this path can change depending on your environment, I strongly advise you for staging or production to externalize the log config file as previously demonstrated but also use external application.properties file.

You can simply do this when launching your java program:

java -Dspring.config.location=file:///home/.../application.properties

Good practices like this is further explained on the Twelve Factors website, configuration part is here.

Hope this helps

A Le Dref
  • 422
  • 3
  • 7