Java Programming Feed

June 21, 2011

Configuring Tomcat Logging

If you looked at my recent posts I was playing with Java and Tomcat a lot, and trying to run those on Windows Azure. One of the things I wanted to achieve is to store Tomcat log files if folder different than the default Tomcat location. Surprisingly for me configuring Tomcat logging turned out to be not so intuitive. Let’s start with the basics…


Where are Tomcat Logs Stored?

By default Tomcat stores the log files under

Where CATALINA_BASE is the folder where Tomcat is installed. If you open that folder you will see something like this:


06/21/2011  02:49 PM  7,534 catalina.2011-06-21.log
06/21/2011  01:37 PM      0 host-manager.2011-06-21.log
06/21/2011  02:49 PM  1,872 localhost.2011-06-21.log
06/21/2011  02:49 PM      0 localhost_access_log.2011-06-21.txt
06/21/2011  01:37 PM      0 manager.2011-06-21.log


For more information what each file is about you can read the Tomcat Logging page.

My goal was to move those log files to a folder different from



How to Configure Tomcat Logging (Really How)?

If you search Google (or Apache’s web site) you will find out that in order to configure Tomcat logging you will need to either:

  • edit the file in $CATALINA_BASE\logs
  • or create new and set the java.util.logging.config.file System property to point to it

The easiest way to use the second approach is to set the Environment Variable



As you may expect the default file is located in $CATALINA_BASE\conf.

Now, the hard part with this is that you CANNOT use Environment Variables in Java properties file. And of course this was what I really wanted to do. In general what I wanted to do is to use the %ROLEROOT% Environment Variable in the location path for all the log files (see What Environment Variables Can You Use in Windows Azure). The workaround to this problem is to set Java System property to use the Environment Variable (ie. the –D option for java.exe). Tomcat startup scripts use Environment Variable JAVA_OPTS for exactly this purpose:




For Windows Azure specifically you can use the Variable tag in CSDEF:


<Variable name="JAVA_OPTS" value="-D[my_property_name]=%ROLEROOT%\[some_folder]" />


Next, in order to use the System property in the Java properties file you need to specify it in the following format:




Here is what I actually did. In CSDEF you set the Environment Variables as follows:


    <Variable name="TomcatLocalResourcePath"
              value="%ROLEROOT%\Approot\temp" />
    <Variable name="JAVA_OPTS" value="-DTomcatLocalResourcePath=%
              TomcatLocalResourcePath%" />


and in the you use the Java System property as follows: = ${TomcatLocalResourcePath} = ${TomcatLocalResourcePath} = ${TomcatLocalResourcePath} = ${TomcatLocalResourcePath}


This is all good, however it takes care only of the following log files:




What about localhost_access_log.2011-06-21.txt? The access log file in Tomcat is not configured via file but in server.xml file. You can read more about the Access Log Valve (which controls the access log) on Apache’s web site. The simple thing that you need to do is to set the directory attribute on the Valve tag as follows:


<Valve className="org.apache.catalina.valves.AccessLogValve"
               prefix="localhost_access_log." suffix=".txt"
               pattern="%h %l %u %t &quot;%r&quot; %s %b"


UNIX vs. Windows vs. Java Property Files

As a final note some clarification on when to use dollar sign $, percent % and dollar sign with curly braces ${} as I think it may be confusing for some people:

  • As you know dollar sign $ is used to evaluate Environment Variables in UNIX. For example if you define the following Environment Variable in UNIX:

    setenv MYTEMPPATH /usr/temp

    you can use it later on as follows:

  • In contrast Windows uses percent % to evaluate Environment Variables. Here is the same example for Windows:
    set MYTEMPPATH=C:\Temp

    and you can use it as follows:


  • Property files in Java use the UNIX type of format but with curly braces to evaluate System properties. For example if you define the System property as follows:


    you can use it in Java properties files as follows:${MyTempPath}