Externalize log4j.properties

This article outlines the steps to externalize the log4j.properties file used by the OvalEdge application, deployed on Apache Tomcat. Externalizing the logging configuration file enables system administrators and DevOps engineers to modify logging behavior (e.g., log levels, file paths, appenders) without requiring repackaging or redeployment of the application.

Apache Log4j is a widely used logging framework in Java applications, and log4j.properties serves as its configuration file. Externalizing this file allows for improved maintainability, environment-specific logging behavior, and simplified troubleshooting.

Prerequisites

Ensure the following conditions are met before proceeding:

  • Access Permissions: SSH access to the OvalEdge application virtual machine (VM) with sufficient privileges to modify files and restart services

  • Tomcat Installed: Apache Tomcat v10.1.40 is installed and running the OvalEdge application

  • Java Runtime: Java is installed and configured correctly on the host system

  • Editor Availability: A terminal text editor, such as vi or nano, is available

Configuration Steps

  1. Log in to the Application VM

    • Access the OvalEdge VM using SSH or direct terminal access:

      ssh <user>@<host-ip>

      Replace <user> and <host-ip> with the appropriate login credentials and IP address.

  1. Copy the Existing log4j.properties File

    • Navigate to the Tomcat application classes directory and copy the default log4j.properties to an external configuration directory:

      cp /home/ovaledge/apache-tomcat-10.1.40/webapps/ovaledge/WEB-INF/classes/log4j.properties /home/ovaledge/extprop/

      Sample Reference Screenshot:

      Ensure the directory /home/ovaledge/extprop/ exists. Create it using:

      mkdir -p /home/ovaledge/extprop/

  1. Modify the External log4j.properties File

    • Edit the copied log4j.properties file to enable external configuration.

    • Open the file:

      vi /home/ovaledge/extprop/log4j.properties

      Sample Reference Screenshot:

    • Comment out the internal configuration reference and uncomment the external path if applicable, for example:

      #log4j.rootLogger=INFO, CONSOLE
      log4j.rootLogger=INFO, FILE
    • Adjust other properties as required (log levels, file locations, patterns, etc.).

  2. Edit the setenv.sh File to Point to External Configuration

    • Navigate to the Tomcat bin directory:

      cd /home/ovaledge/apache-tomcat-10.1.40/bin
    • Open or create the setenv.sh file:

      vi setenv.sh
    • Add the following line to specify the external configuration path:

      export CATALINA_OPTS="$CATALINA_OPTS -Dlog4j.configuration=file:/home/ovaledge/extprop/log4j.properties"

      Sample Reference Screenshot:

      Ensure the path is absolute and uses the file: protocol prefix.

  3. Restart Tomcat

    • Apply the configuration by restarting the Tomcat server:

      /home/ovaledge/apache-tomcat-10.1.40/bin/shutdown.sh
      /home/ovaledge/apache-tomcat-10.1.40/bin/startup.sh

      Wait a few moments and verify that Tomcat has started successfully.

Validation

To confirm that the external log4j.properties is in effect:

  • Review the Tomcat logs (catalina.out or application logs) to ensure expected log behavior

  • Introduce a test log message (e.g., set to DEBUG) and verify output in the configured log file

  • Confirm that no logging configuration errors appear during Tomcat startup


Copyright © 2025, OvalEdge LLC, Peachtree Corners, GA, USA.

Last updated

Was this helpful?