Configuring Tomcat 9 with Logback

 

Apache Tomcat is a popular open-source implementation of the Java Servlet, JavaServer Pages, and Java Expression Language technologies. Logging is a crucial part of any application, and Logback is one of the most widely used logging frameworks for Java applications. This guide will walk you through configuring Tomcat 9 with Logback for efficient logging. At the time when I wrote this blog, the tomcat version I worked on was Tomcat 9.0.69 and the Tomcat logback available was 1.3.0-alpha5

Configuring Tomcat 9 with Logback


Prerequisites

Before we begin, ensure you have the following:

  • Apache Tomcat 9.0.69 installed (or 9.0.+)

  • Java Development Kit (JDK. 8 or 9) installed

    • set JAVA_HOME

    • set JRE_HOME 

  • Basic knowledge of XML configuration files

Step 1: Download Logback Libraries

First, you need to download the Logback library files. You can download them from this link:

https://github.com/tomcat-slf4j-logback/tomcat-slf4j-logback/releases/download/tomcat9/tomcat9-slf4j-logback-9.0.36-9.0.36-slf4j-2.0.0-alpha1-logback-1.3.0-alpha5.zip

Step 2: Place Logback Jars in Tomcat's lib Directory

Copy the downloaded logback-access-1.3.0-alpha5.jar, logback-core-1.3.0-alpha5.jar files to the lib directory of your Tomcat installation. The lib directory is typically located at:

<Tomcat_Home>/lib


Step 3: Create Logback Configuration File

Next, create a Logback configuration file named logback.xml in the conf directory of your Tomcat installation. The conf directory is typically located at:


<Tomcat_Home>/conf


Here is a basic example of a logback.xml configuration file:

<configuration>


    <property name="max.retention.days" value="60" />

    <property name="tomcat.log.path" value="<path to log directory>" />


    <appender name="CONSOLE" class="org.apache.juli.logging.ch.qos.logback.core.ConsoleAppender">

        <encoder>

            <pattern>%d{HH:mm:ss.SSS} %-5level {%thread} [%logger{20}] : %msg%n</pattern>

        </encoder>

    </appender>


    <appender name="FILE-CATALINA" class="org.apache.juli.logging.ch.qos.logback.core.rolling.RollingFileAppender">

        <file>${tomcat.log.path}/logs/catalina.log</file>

        <append>true</append>

        <encoder>

            <charset>utf-8</charset>

            <pattern>%d{HH:mm:ss.SSS} %-5level {%thread} [%logger{40}] : %msg%n</pattern>

        </encoder>

        <rollingPolicy class="org.apache.juli.logging.ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">

            <fileNamePattern>${tomcat.log.path}/logs/archive/catalina-%d{yyyyMMdd}-%i.log.gz</fileNamePattern> 

            <maxHistory>${max.retention.days}</maxHistory>

            <cleanHistoryOnStart>true</cleanHistoryOnStart>

            <maxFileSize>200MB</maxFileSize>

        </rollingPolicy>

    </appender>


    <appender name="FILE-LOCALHOST" class="org.apache.juli.logging.ch.qos.logback.core.rolling.RollingFileAppender">

        <file>${tomcat.log.path}/logs/localhost.log</file>

        <append>true</append>

        <encoder>

            <charset>utf-8</charset>

            <pattern>%d{HH:mm:ss.SSS} %logger{0} {%thread} %level : %msg%n</pattern>

        </encoder>

        <rollingPolicy class="org.apache.juli.logging.ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">

            <fileNamePattern>${tomcat.log.path}/logs/archive/localhost-%d{yyyyMMdd}-%i.log.gz</fileNamePattern> 

            <maxHistory>${max.retention.days}</maxHistory>

            <maxFileSize>200MB</maxFileSize>

        </rollingPolicy>

    </appender>


    <appender name="FILE-MANAGER" class="org.apache.juli.logging.ch.qos.logback.core.rolling.RollingFileAppender">

        <file>${tomcat.log.path}/logs/manager.log</file>

        <append>true</append>

        <encoder>

            <charset>utf-8</charset>

            <pattern>%d{HH:mm:ss.SSS} %logger{0} {%thread} %level : %msg%n</pattern>

        </encoder>

        <rollingPolicy class="org.apache.juli.logging.ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">

            <fileNamePattern>${tomcat.log.path}/logs/archive/manager-%d{yyyyMMdd}-%i.log.gz</fileNamePattern> 

            <maxHistory>${max.retention.days}</maxHistory>

            <maxFileSize>200MB</maxFileSize>

        </rollingPolicy>

    </appender>


    <appender name="FILE-HOST-MANAGER" class="org.apache.juli.logging.ch.qos.logback.core.rolling.RollingFileAppender">

        <file>${tomcat.log.path}/logs/host-manager.log</file>

        <append>true</append>

        <encoder>

            <charset>utf-8</charset>

            <pattern>%d{HH:mm:ss.SSS} %logger{0} {%thread} %level : %msg%n</pattern>

        </encoder>

        <rollingPolicy class="org.apache.juli.logging.ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">

            <fileNamePattern>${tomcat.log.path}/logs/archive/host-manager-%d{yyyyMMdd}-%i.log.gz</fileNamePattern> 

            <maxHistory>${max.retention.days}</maxHistory>

            <maxFileSize>200MB</maxFileSize>

        </rollingPolicy>

    </appender>


    <appender name="STDOUT" class="org.apache.juli.logging.ch.qos.logback.core.rolling.RollingFileAppender">

        <file>${tomcat.log.path}/logs/systemout.log</file>

        <append>true</append>

        <encoder>

            <charset>utf-8</charset>

            <pattern>%d{HH:mm:ss.SSS} %-5level {%thread} [%logger{40}] : %msg%n</pattern>

        </encoder>

        <rollingPolicy class="org.apache.juli.logging.ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">

            <fileNamePattern>${tomcat.log.path}/logs/archive/systemout-%d{yyyyMMdd}-%i.log.gz</fileNamePattern> 

            <maxHistory>${max.retention.days}</maxHistory>

            <cleanHistoryOnStart>true</cleanHistoryOnStart>

            <maxFileSize>200MB</maxFileSize>

        </rollingPolicy>

    </appender>

    <logger name="org.apache.catalina" level="DEBUG" additivity="false">

        <appender-ref ref="FILE-CATALINA" />

    </logger>


    <logger name="org.apache.catalina.core.ContainerBase.[Catalina]" level="DEBUG" additivity="false">

        <appender-ref ref="FILE-LOCALHOST" />

    </logger>


    <logger name="org.apache.catalina.core.ContainerBase.[Catalina].[/manager]" level="DEBUG"

        additivity="false">

        <appender-ref ref="FILE-MANAGER" />

    </logger>


    <logger name="org.apache.catalina.core.ContainerBase.[Catalina].[/host-manager]" level="DEBUG"

        additivity="false">

        <appender-ref ref="FILE-HOST-MANAGER" />

    </logger>


    <root level="DEBUG">

        <appender-ref ref="STDOUT" />

    </root>


</configuration>


This configuration logs messages to both the console and a file, with daily rolling of log files and a retention period of 30 days.

and a logback-access.xml:


<configuration>


    <property name="max.retention.days" value="60" />

    <property name="tomcat.log.path" value="<path to log directory>" />

    <!-- always good activate OnConsoleStatusListener -->

    <statusListener class="ch.qos.logback.core.status.OnConsoleStatusListener" /> 


    <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">

        <encoder class="ch.qos.logback.access.PatternLayoutEncoder">

            <pattern>common</pattern>

        </encoder>

    </appender>


    <appender name="ACCESS-LOG" class="ch.qos.logback.core.rolling.RollingFileAppender">

        <file>${tomcat.log.path}/logs/localhost-access.log</file>

        <append>true</append>

        <encoder class="ch.qos.logback.access.PatternLayoutEncoder">

            <charset>utf-8</charset>

            <pattern>common</pattern>

        </encoder>

        <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">

            <fileNamePattern>${tomcat.log.path}/logs/archive/localhost-access-%d{yyyyMMdd}-%i.log.tar.gz</fileNamePattern> 

            <maxHistory>${max.retention.days}</maxHistory>

            <maxFileSize>200MB</maxFileSize>

        </rollingPolicy>

    </appender>


    <appender-ref ref="ACCESS-LOG" />

path to log directory could be D:/tomcat9 for windows and /home/tomcat/tomcat9 for linux

Step 4: Configure Tomcat to Use Logback

To make Tomcat use Logback, you need to tell Tomcat to use SLF4J (Simple Logging Facade for Java) as its logging framework. To do this, remove a file named logging.properties from conf directory of your Tomcat installation.

Change server.xml and replace this line:

<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"

               prefix="localhost_access_log" suffix=".txt"

               pattern="%h %l %u %t &quot;%r&quot; %s %b" />

with this one:

<Valve className="ch.qos.logback.access.tomcat.LogbackValve" quiet="true" />


Replace setenv.bat, setenv.sh, tomcat-juli.jar in the bin directory of your Tomcat installation. The bin directory is typically located at:


<Tomcat_Home>/bin


Add following option to tomcat startup command:


-Dlogback.configurationFile=<path to logback.xml file>\conf\logback.xml

-Djuli-logback.configurationFile=<path to logback.xml file>\conf\logback.xml


option can be added to startup file startup or to the tomcat console.

Restart Tomcat

Step 5: Restart Tomcat

Finally, restart your Tomcat server to apply the changes. You can do this by running the following command from the bin directory of your Tomcat installation:

sh

Copia codice

./shutdown.sh

./startup.sh


For Windows:

sh

Copia codice

shutdown.bat

startup.bat


Conclusion

You have successfully configured Tomcat 9 to use Logback for logging. You can now benefit from Logback's powerful and flexible logging capabilities. Remember to customize the logback.xml and logback-access.xml  files to suit your specific logging requirements.


Comments

Popular posts from this blog

Monitoring and Logging with Prometheus: A Practical Guide

Creating a Complete CRUD API with Spring Boot 3: Authentication, Authorization, and Testing

Logging in Spring Boot 3: Best Practices for Configuration and Implementation