Rotating catalina.out log file on Linux / Unix
search cancel

Rotating catalina.out log file on Linux / Unix

book

Article ID: 343473

calendar_today

Updated On:

Products

VMware Support Only for Apache Tomcat

Issue/Introduction

This article provides information on rotating the logs/catalina.out file.

Symptoms:
When running on Linux or Unix, vFabric tc Server and Apache Tomcat redirect the output from STDOUT and STDERR directly to the logs/catalina.out file. Because the output is sent to this file with a shell redirection, it does not go through the standard logging mechanisms and, by default, it is not rotated.

Note: When running on Windows, this is not needed as STDOUT and STDERR are redirected and can be rotated by the Java Service Wrapper.

Environment

VMware vFabric tc Server 2.6
Apache Tomcat 6
VMware vFabric tc Server 2.7
Apache Tomcat 7
VMware vFabric tc Server 2.8
Apache Tomcat 5.5

Resolution

Selecting a log rotation tool

By default, Apache Tomcat and vFabric tc Server redirect the output from STDOUT and STDERR directly to a file. To rotate this log file, the output from STDOUT and STDERR must instead be piped to an intermediate tool that is capable of rotating the log file. There are a few existing tools that can be used:

Note: VMware does not endorse or recommend any particular third-party utility, nor is the list below meant to be exhaustive.
  • rotatelogs

    If you already have Apache HTTP Server, vFabric ERS HTTPD, or vFabric Web Server installed, you can use the rotatelogs tool that is packaged with this software. This is the same tool that these software packages use to rotate their log files. For more information, see the rotatelogs documentation.

  • cronolog

    This is a third-party, Open Source log rotation tool that is very similar to rotatelogs. It can be installed directly from http://cronolog.org/ or possibly through your operating system's package manager.

  • If you do not want to use a third-party tool, you can write your own script. The script would simply need to receive log data via STDIN and handle writing it to some log location.

Once you have rotatelogs, cronolog, or another similar tool installed, you must then modify the catalina.sh script so that it will pipe to the tool rather than redirect directly to a file.

Modifications for vFabric tc Server

  1. Open the $TCSERVER_INSTALL/tomcat-x.x.x.RELEASE/bin/catalina.sh file in a text editor.
  2. Search for and delete this line:

    touch "$CATALINA_OUT"

  3. Search for this line, which occurs twice in the file:

    >> "$CATALINA_OUT" 2>&1 &

  4. For daily rotation, replace both occurrences of the line from the previous step with one of these lines. Choose the line that matches the log rotation tool that is installed on your system.

    • rotatelogs

      2>&1 |/usr/bin/rotatelogs "$CATALINA_BASE/logs/catalina.out" 86400 &

    • cronolog

      2>&1 |/usr/bin/cronolog "$CATALINA_BASE/logs/catalina-%Y-%m-%d.out" &

    • Custom script

      2>&1 |/usr/bin/myapp arg1 arg2 ... argX &

    Note: The commands above assume the tool is installed in the /usr/bin directory. Adjust the path to the executable as required for your system.

  5. To use a different log rotation scheme, such as rotate by size, simply adjust the command listed in the previous step.
  6. Restart the vFabric tc Server instance to activate the changes.

Modifications for Apache Tomcat

  1. Open the $CATALINA_HOME/bin/catalina.sh file in a text editor.
  2. Search for and delete this line:

    touch "$CATALINA_BASE"/logs/catalina.out

  3. Search for this line, which occurs twice in the file:

    $CATALINA_BASE"/logs/catalina.out 2>&1 &

  4. For daily rotation, replace both occurrences of the line from the previous step with one of these lines. Choose the line that matches the log rotation tool that is installed on your system.

    • rotatelogs

      2>&1 |/usr/bin/rotatelogs "$CATALINA_BASE/logs/catalina.out" 86400 &

    • cronolog

      2>&1 |/usr/bin/cronolog "$CATALINA_BASE/logs/catalina-%Y-%m-%d.out" &

    • Custom script

      2>&1 |/usr/bin/myapp arg1 arg2 ... argX &

    Note: The commands above assume the tool is installed in the /usr/bin directory. Adjust the path to the executable as required for your system.

  5. To use a different log rotation scheme, such as rotate by size, simply adjust the command listed in the previous step.
  6. Restart Apache Tomcat to activate the changes.


Additional Information

Instead of rotating the catalina.out file, it is possible to greatly limit the amount of information that is written to this file so that it may not be necessary to rotate the file.

These tips can be applied to prevent excessive logging to catalina.out:
  • When using Apache Tomcat, the default logging configuration writes to both catalina-YYYY-MM-DD.log and catalina.out.

    To prevent Apache Tomcat from logging information to catalina.out:

    1. Open the conf/logging.properties file in a text editor.
    2. Locate this line:

      .handlers = 1catalina.org.apache.juli.FileHandler, java.util.logging.ConsoleHandler

    3. Remove ", java.util.logging.ConsoleHandler" from the end of that line.
    4. Save the file and restart Apache Tomcat.

  • If you have a web application that is writing directly to System.out or System.err, you can use the swallowOutput setting of Apache Tomcat and vFabric tc Server to instruct the server to capture this output and send it through the logging system. For more information on enabling this feature, see the Apache Tomcat documentation:


  • If you have a web application that is using a third-party logging system like Log4j, SLF4J, or Logback, ensure that the logging configuration for that application is not configured to log to the console. Instead, configure your logging system to log directly to a file or some other log capturing mechanism.
Pivotal Links

Pivotal tc Server Knowledge Base:



Note: The links in this article were correct as of January 18, 2013. If you find a link is broken, provide feedback and a VMware employee will update the link.

Impact/Risks:
Making the adjustments suggested in this article will result in the incorrect process ID being written to the process ID file, specified by $CATALINA_PID. The process ID that will actually be written to the file is the process ID of the log rotation application, not Apache Tomcat or vFabric tc Server.

In most cases this should not affect the operation of Apache Tomcat or vFabric tc Server. However, if you encounter issues with Apache Tomcat or vFabric tc Server failing to shut down properly, you may want to revert the changes suggested by this article and see if that resolves the issue.