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 $CATALINA_BASE\logs
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
$CATALINA_BASE\logs
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
logging.propertiesfile in$CATALINA_BASE\logs - or create new
logging.propertiesand set thejava.util.logging.config.fileSystem property to point to it
The easiest way to use the second approach is to set the Environment Variable
LOGGING_CONFIG=”-Djava.util.logging.config.file=[your_logging.properties_file_location]”
As you may expect the default logging.properties 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:
set JAVA_OPTS=-DMY_SYSTEM_PROPERTY=%MY_ENVIRONMENT_VARIABLE
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:
${[my_property_name]}
Here is what I actually did. In CSDEF you set the Environment Variables as follows:
<Environment>
<Variable name="TomcatLocalResourcePath"
value="%ROLEROOT%\Approot\temp" />
<Variable name="JAVA_OPTS" value="-DTomcatLocalResourcePath=%
TomcatLocalResourcePath%" />
</Environment>
and in the logging.properties you use the Java System property as follows:
1catalina.org.apache.juli.FileHandler.directory = ${TomcatLocalResourcePath}
2localhost.org.apache.juli.FileHandler.directory = ${TomcatLocalResourcePath}
3manager.org.apache.juli.FileHandler.directory = ${TomcatLocalResourcePath}4host-manager.org.apache.juli.FileHandler.directory = ${TomcatLocalResourcePath}
This is all good, however it takes care only of the following log files:
catalina.2011-06-21.log
host-manager.2011-06-21.log
localhost.2011-06-21.log
manager.2011-06-21.log
What about localhost_access_log.2011-06-21.txt? The access log file in Tomcat is not configured via logging.properties 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"
directory="${TomcatLocalResourcePath}"
prefix="localhost_access_log." suffix=".txt"
pattern="%h %l %u %t "%r" %s %b"
resolveHosts="false"/>
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:
setenv SOMEPATH $MYTEMPPATH/new - 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:
set SOMEPATH=%MYTEMPPATH%\new
- 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:
-DMyTempPath=C:\Temp
you can use it in Java properties files as follows:
some.property=${MyTempPath}