Search code examples
mavenspring-mvclogbackslf4j

Externalize logback.xml in Spring MVC


I use SL4J for logging in my Java Spring MVC Web Application. I have the logback.xml file under /src/main/resources. In my pom.xml file, I have the following related to logging:

<properties>
<org.slf4j-version>1.6.4</org.slf4j-version>
<logback.version>1.0.1</logback.version>
</properties>

And the following dependencies:

<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>${org.springframework-version}</version>
            <exclusions>
                <!-- Exclude Commons Logging in favor of SLF4j -->
                <exclusion>
                <groupId>commons-logging</groupId>
                <artifactId>commons-logging</artifactId>
                </exclusion>
            </exclusions>
    </dependency>
    <!-- Logging -->
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>${org.slf4j-version}</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>jcl-over-slf4j</artifactId>
        <version>${org.slf4j-version}</version>
        <scope>runtime</scope>
    </dependency>
    <!-- logback -->
     <dependency>
      <groupId>ch.qos.logback</groupId>
      <artifactId>logback-classic</artifactId>
      <version>${logback.version}</version>
     </dependency>

     <dependency>
      <groupId>ch.qos.logback</groupId>
      <artifactId>logback-core</artifactId>
      <version>${logback.version}</version>
     </dependency>
</dependencies>

Now logging is working fine for me. But now I am trying to move the logback.xml file outside the war file so that I do not have to change the war file to change the logging level. I was trying to move the file to ${catalina.base}/conf/. I was unable to find a way to specify the path of the file which would be ${catalina.base}/conf/logback.xml. I have found a couple of similar questions, but I couldint find a solution that I could use: Externalized the location of logback.xml in Spring Boot using application.properties and logback externalization. Is there any way I can move the file under the conf folder under tomcat and specify its path so that I can keep this logback.xml ouside the war file


Solution

  • It is not good idea to put logging properties in tomcat config folder. Better to put it inside application folder/war.

    But there is a way to locate logback.xml from external directory by setting a property in your spring application properties file.

    For example in application.properties add logging.config=file:/home/user/tomcat/conf/logback.xml