4
votes

I am using Spring with Spring boot. Recently while trying my hands on EhCache, I tried enabling logging for EhCache. Setting log-level in application.properties with :

logging.level.org.springframework.cache: DEBUG

It had no effect. So I came across a method to enable logging using 'logback'. Now I need to put logging configuration into file logback.xml.

My question is how configuration are handled in Spring? Do logback.xml is given priority over application.properties? Is there a method to use only one configuration? Either application.properties or logback.xml? Whats the point of having two configurations?

Edit: Later I found out, to enable EhCache logging, I need to add this line in my application.properties:

logging.level.net.sf.ehcache: DEBUG
2
The logging properties is provided by Spring Boot: docs.spring.io/spring-boot/docs/current/reference/html/… So,if you have logback.xml (or native config if you use other logging backend), it will take precedence. And, if you have logback.xml, you don't need to have logging.level.net.sf.ehcache: DEBUG in application.properites. Simply define the logger in logback.xml - Adrian Shum

2 Answers

0
votes

Yes. You can do the logging part using only one configuration logback.xml And using <springProperty> tag you can use Spring Environment properties within your Logback file.

<springProperty scope="context" name="fluentHost" source="myapp.fluentd.host"
        defaultValue="localhost"/>
<appender name="FLUENT" class="ch.qos.logback.more.appenders.DataFluentAppender">
    <remoteHost>${fluentHost}</remoteHost>
    ...
</appender>

Ref

0
votes

Logback will look in the classpath for the files logback_test.xml, then logback.xml if it can't find the first.

Assuming you're using maven you do not need anything in your properties file. Usually logback.xml in src/main/resources and logback_test.xml in src/test/resources.

You will need an implementation of logback and a api, usually in your pom you need this:

<!-- API -->
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <version>1.7.22</version>
</dependency>
<!-- Implementation -->
<dependency>
    <groupId>ch.qos.logback</groupId>
    <artifactId>logback-classic</artifactId>
    <version>1.1.8</version>
</dependency>

Ehcache will use slf4j so your logback config will be picked up.

See http://logback.qos.ch/manual/configuration.html for logback config.