TomcatExpert

How to configure log rotation in Apache Tomcat 5.5 and 6.0 using the Juli logging package

posted by SpringSource on March 18, 2010 11:46 PM

How to enable file rotation to periodically purge older log files and clear space using the default juli internal logging for Apache Tomcat.

By default, Apache Tomcat dies its internal logging through juli. The quantity of information stored in the log files on any server can quickly grow to be quite large. For instance, the access log file typically grows 1 MB or more per 10,000 requests. Therefore, it may be necessary to periodically rotate the log files by moving the files or deleting the existing logs to free up space on the server and allow for more efficient searching through logs. This cannot be done while the server is running, because Apache Tomcat will continue writing to the old log file as long as it holds the file open.

 To achieve this, in the logging.properties, add: 

.handlers = 1catalina.java.util.logging.FileHandler 
.level=FINE 
 
1catalina.java.util.logging.FileHandler.level = FINE 
1catalina.java.util.logging.FileHandler.pattern = ${catalina.base}/logs/${catalina.appserver.instance}.%g.log 
1catalina.java.util.logging.FileHandler.limit = 2000000 
1catalina.java.util.logging.FileHandler.count = 5 
1catalina.java.util.logging.FileHandler.formatter = java.util.logging.SimpleFormatter

where 1catalina.java.util.logging.FileHandler would instantiate the FileHandler that comes with JDK.

Since java.util.logging.FileHandler does not have .prefix or .directory, .prefix and .directory needs to be set in the catalina package:

1catalina.java.util.logging.FileHandler.pattern = ${catalina.base}/logs/catalina.%g.log 

NOTE: The names have to be consistent everywhere, including in the handlers and .handlers references.

For further details, the JDK filehandler is documented at: http://java.sun.com/j2se/1.4.2/docs/api/java/util/logging/FileHandler.html

For more than 10 years, SpringSource employees have been supporting Apache technologies, with unparalleled experience and commitment to the Apache Software Foundation. More than 400 of the Fortune 500 count on SpringSource to support their mission-critical business applications. Leaders of the Apache Software Foundation, including Board Members, work at SpringSource and dedicate a significant amount of time further developing the Apache Tomcat open source project. Over the last 2 years, 95% of the issues fixed in the Apache Tomcat project were fixed by SpringSource engineers. For more information on how SpringSource can help your enterprise, see the SpringSource website, or call 800/444-1935.

Comments

The "<handlers></handlers>"

The "<handlers></handlers>" seems confusing. Perhaps using the normal comment syntax is more appropriate:

####
# Define custom logging handlers
####

Thanks

Thanks for spotting that. Looks like a copy/paste error. I've fixed the logging.properties extract by removing those tags.

Time based log file rotation

Using JULI how would you configure log file rotation on daily or hourly basis.

Limiting catalina.out

Hi ,

I changed the logging.properties as mentioned in the post.
I believe it is working fine.
But catalina.out seems growing to unbelievable size.
How to limit and rotate it?
My logging.properties is as follows :

# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

handlers = 1catalina.java.util.logging.FileHandler, 2localhost.java.util.logging.FileHandler, 3manager.java.util.logging.FileHandler, 4host-manager.java.util.logging.FileHandler, java.util.logging.ConsoleHandler

.handlers = 1catalina.java.util.logging.FileHandler

############################################################
# Handler specific properties.
# Describes specific configuration info for Handlers.
############################################################

1catalina.java.util.logging.FileHandler.level = FINE
1catalina.java.util.logging.FileHandler.pattern = ${catalina.base}/logs/catalina.%g.log
1catalina.java.util.logging.FileHandler.limit = 2000
1catalina.java.util.logging.FileHandler.count = 2
1catalina.java.util.logging.FileHandler.formatter = java.util.logging.SimpleFormatter

2localhost.java.util.logging.FileHandler.level = FINE
2localhost.java.util.logging.FileHandler.pattern = ${catalina.base}/logs/localhost.%g.log
2localhost.java.util.logging.FileHandler.limit = 2000
2localhost.java.util.logging.FileHandler.count = 2
2localhost.java.util.logging.FileHandler.formatter = java.util.logging.SimpleFormatter

3manager.java.util.logging.FileHandler.level = FINE
3manager.java.util.logging.FileHandler.pattern = ${catalina.base}/logs/manager.%g.log
3manager.java.util.logging.FileHandler.limit = 2000
3manager.java.util.logging.FileHandler.count = 2
3manager.java.util.logging.FileHandler.formatter = java.util.logging.SimpleFormatter

4host-manager.java.util.logging.FileHandler.level = FINE
4host-manager.java.util.logging.FileHandler.pattern = ${catalina.base}/logs/host-manager.%g.log
4host-manager.java.util.logging.FileHandler.limit = 2000
4host-manager.java.util.logging.FileHandler.count = 2
4host-manager.java.util.logging.FileHandler.formatter = java.util.logging.SimpleFormatter

java.util.logging.ConsoleHandler.level = FINE
java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter

############################################################
# Facility specific properties.
# Provides extra control for each logger.
############################################################

org.apache.catalina.core.ContainerBase.[Catalina].[localhost].level = INFO
org.apache.catalina.core.ContainerBase.[Catalina].[localhost].handlers = 2localhost.java.util.logging.FileHandler

org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/manager].level = INFO
org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/manager].handlers = 3manager.java.util.logging.FileHandler

org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/host-manager].level = INFO
org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/host-manager].handlers = 4host-manager.java.util.logging.FileHandler

# For example, set the org.apache.catalina.util.LifecycleBase logger to log
# each component that extends LifecycleBase changing state:
#org.apache.catalina.util.LifecycleBase.level = FINE

# To see debug messages in TldLocationsCache, uncomment the following line:
#org.apache.jasper.compiler.TldLocationsCache.level = FINE

and I ve done a small change to catalina.sh as per the blog here: http://www.ginnokagi.com/2012/08/tomcat_tuning_in_practice.html

What am I doing wrong here..?
When i comment out touch in line 372 as
#touch "$CATALINA_OUT"
catalina.out is not getting written at all.

Any ideas..
Thanks in advance,
Mohan
mohan@yatech.in

Post new comment

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.