Reputation: 7647
I would like to externalize the log level in logback-spring.xml
as below,
<root level="${logback.loglevel}">
<appender-ref ref="CONSOLE"/>
</root>
In my application.yml
file I have:
logback:
loglevel: WARN
But this value is not picked from Logback XML and runs with DEBUG
level instead. If I hard coded the value there as WARN
only it runs as WARN
level.
Upvotes: 4
Views: 2814
Reputation: 1074
If what you want is just changing log level, then should follow the approach in jccampanero's answer. That is provided by Spring boot out of the box to make things simple for developers.
However, if you want to do more customization on the logging, and you want to have the flexibility to pass config values from application.properties
or application.yml
to your logback-spring.xml
. It is also very simple.
For example in application.properties
you have this config my.logging.level=WARN
In your logback-spring.xml
you can simply retrieve it by this
<springProperty scope="context" name="logLevel" source="my.logging.level" defaultValue="INFO"/>
And now you can use logLevel
(as defined in the springProperty tag above) in your logback-spring.xml
file like this:
<root level="${loglevel}">
<appender-ref ref="CONSOLE"/>
</root>
Hope you find this helpful.
Upvotes: 1
Reputation: 53461
It seems that the type of configuration externalization you are trying to achieve is based on the Spring application.yml
configuration files.
If that is the case, Spring Boot provides out of the box different configuration properties you can use to define the appropriate logging level, no matter the underlying logging library implementation you are using.
You can provide the appropriate configuration in your different application.properties
or application.yaml
, something like:
logging:
level:
root: "warn"
org.springframework.web: "debug"
org.hibernate: "error"
Be aware that using a custom logback-spring.xml
file is unnecessary.
Please, consider review the aforementioned documentation if you need more information.
Upvotes: 2
Reputation: 2154
Unable to use Spring Property Placeholders in logback.xml
Good to know about the difference between Spring EL and Logback's variables...I had assumed it was Spring that was in charge of parsing those variables for me. I did have the element, but that got me to thinking.
My
application.properties
file was outside of the jar, so Logback had no idea where to find it. By keeping my Spring-related properties in my externalapplication.properties
file, moving the logging-related properties into anapplication-internal.properties
file (located inside the jar), and pointing Logback to that file () got everything working as expected!
Upvotes: 0
Reputation: 1267
As an alternative you can also externalize the logback log level like this: https://logback.qos.ch/manual/configuration.html#definingPropsOnTheFly
You may define properties dynamically using the element. The define element takes two mandatory attributes: name and class. The name attribute designates the name of the property to set whereas the class attribute designates any class implementing the PropertyDefiner interface.
For example with a configuration file like this:
<configuration>
<define name="rootLevel" class="a.class.implementing.PropertyDefiner"/>
<root level="${rootLevel}"/>
</configuration>
And your a.class.implementing.PropertyDefiner
could look like this:
package a.class.implementing;
import ch.qos.logback.core.PropertyDefinerBase;
public class PropertyDefiner extends PropertyDefinerBase {
@Override
public String getPropertyValue() {
// todo: retrieve the property from your application context
return "WARN";
}
}
With this setup you can have any system wide available property at runtime define the loglevel.
Upvotes: 0
Reputation: 664
You can use springProfiles for that. An example logback-spring.xml
:
<configuration>
<include resource="org/springframework/boot/logging/logback/defaults.xml"/>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
</pattern>
</encoder>
</appender>
<springProfile name="production">
<root level="warn">
<appender-ref ref="STDOUT"/>
</root>
</springProfile>
<springProfile name="default,local,develop,qa">
<root level="INFO">
<appender-ref ref="STDOUT"/>
</root>
</springProfile>
</configuration>
The resources folder:
Whether you start spring
in production
or either (default, local, develop, qa
) it will use the right log level.
Starting the application with the arguments java -jar -Dspring.profiles.active=production
will set the log-level to warn
.
Upvotes: 1