Tomcat Log4j

broken image


The configuration file for Tomcat is in common/classes directory, the configuration file for a application is placed in the WEB-INF/classes folder of the application. If you do not want Tomcat to use log4j to log but only your application, you can place log4j in the WEB-INF-lib directory of your application as well. The configuration described here results in the creation of two log files for Tomcat 5.5 and three log files for Tomcat 5.0: the Servlet log file (only with Tomcat 5.0), which will roll over to a new file every night; the Commons Logging data via log4j (which will also roll over.

  1. Tomcat Log4j2
  2. Tomcat Log4j Configuration
  3. Tomcat Log4j2
  4. Tomcat Log4j Configuration File Location

Logging for Apache Tomcat and Velocity using Log4j

Written by Geoff Mottram (geoff at minaret dot biz).

Placed in the public domain on August 28, 2004 by the author.

Last updated: January 27, 2005.

This document is provided 'as is', without warranty of any kind, express or implied, including but not limited to the warranties of merchantability, fitness for a particular purpose and noninfringement. In no event shall the author be liable for any claim, damages or other liability, whether in an action of contract, tort or otherwise, arising from, out of or in connection with this document or the use or other dealings in this document.

The Wix Logo Maker is an online design tool powered by Artificial Intelligence (AI) that allows you to create and customize a professional logo for your brand. Answer a few simple questions about your brand identity and personal style and the Wix Logo Maker will create a. FreeLogoDesign is a free logo maker for entrepreneurs, small businesses, freelancers and organizations to create professional looking logos in minutes. Get a free logo for your website, business cards. BrandCrowd Logo Maker is free to use. Browse thousands of different logo designs, edit and save as many as you like. It's fast - create a logo in 2 minutes Pick a design you like and start editing it. Free logo maker Designing a logo doesn't have to be daunting. Canva's logo maker provides all of the ingredients you need to create a custom logo, fast – and free. Learn how to design. Logo design maker.

Contents
Introduction
Tomcat 5.0 Logging
Tomcat 5.5 Logging
Tomcat Commons Logging
Log4j
Log4j Properties File
Caveat
Tomcat Bootstrapping
Tomcat and Jakarta Velocity
WEB-INF Logging
Logger Identifiers in Apache Code
Appendix 1 - Logger Identifiers in Tomcat 5.0.27
Appendix 2 - Logger Identifiers in Jakarta Struts 1.1

Introduction
It took a number of hours to get Apache Tomcat version 5.0.27 and Apache Velocity 1.4 to create log files the way I wanted them for a production environment. This document describes some of the things I discovered and provides step by step instructions for configuring Tomcat to generate log files that rotate nightly. This document covers both Tomcat 5.0 and 5.5.

Please note: The new alpha version of Log4j 1.3 has added support for a daily rolling log file. See Log4j Version 1.3 and Apache Tomcat for instructions on how to install and configure this new feature.

Tomcat Log4j2

Tomcat 5.0 Logging
Tomcat 5.0 has two primary channels for logging information. The first is usually output to the catalina.out file and the other is the Servlet log, which typically saves its messages to files with a date as part of their name (i.e. catalina.2004-08-27.log).

The data in catalina.out comes from three sources: data that is written to standard output and standard error, and information that is logged via the Apache Commons Logging interface. This last category includes all manner of output from the Tomcat server about its state of affairs and is also used by other many other systems (like Jakarta Struts). Packages that use the Commons Logging interface can output varying amounts of log data according to how you configure your logging system (more on that in a moment).

The data in the Servlet log is generated by calls to the log() method of the ServletContext class. For example:

To save the output from the Server log in a file, all you need is a FileLogger directive in your Tomcat server.xml file (in the Tomcat conf directory). It typically looks something like this: The Servlet log is either on (by including a FileLogger directive) or off (by not having a FileLogger) but there is no way to specify a logging level (i.e. include only INFO messages and higher). While the Tomcat documentation mentions that all of its Loggers support a verbosity value, setting it doesn't seem to do anything.

When configuring your Tomcat server, you want to capture both streams of logging data into files that 'roll over' every day (i.e. change their name to reflect the date they were made). Using the FileLogger directive in your Tomcat configuration provides a standard way to capture the Servlet log stream to a file that changes each day. This allows you to backup, remove, analyze or whatever else you like to do with your server logs.

It would be nice if there was a Catalina Logger that utilized the Commons Logging interface. That would give you more control over the formatting of the output stream and such a module would be fairly easy to write, but who's got the time? The next section describes how to tame the catalina.out log stream.

Tomcat 5.5 Logging
Tomcat 5.5 has two avenues for logging data. Anything written to standard out and standard error is always sent to the catalina.out file. Everything else has gone through the Apache Commons Logging interface. If you do not configure an alternate logger (like log4j), all logged messages (including everything that goes through commons-logging) ends up in an endlessly growing catalina.out.

One improvement in Tomcat 5.5 over version 5.0 is that the Servlet log no longer exists. This used to receive any messages that were passed to the log() method of the ServletContext class. For example:

Tomcat now sends this information through the commons-logging interface as INFO level messages. By configuring a logger like log4j, you can redirect virtually all of your logging messages into files that 'roll over' every day (i.e. change their name to reflect the date they were made). This allows you to backup, remove, analyze or whatever else you like to do with your server logs.

Tomcat Commons Logging
As mentioned above, much of the data in the catalina.out file is produced by calls to the Apache Commons Logging interface. Commons Logging gives you run-time control over the level of logging detail and permits you to use any number of underlying logging systems. The standard Tomcat distribution uses SimpleLog as the underlying logger. SimpleLog sends all of its output to standard error, which in Tomcat, is redirected to the file catalina.out by default. The biggest problem with this arrangement is that the catalina.out file grows forever -- there is no file rotation.

Log4j
Log4j is a powerful log manager that supports the Commons Logging interface. It can be dropped into your Tomcat installation as a replacement for SimpleLog to provide log rotation and all manner of configuration options. In fact, you are not limited to saving your logs to a file. They can be sent to the syslog, a remote port or other destinations. You can also send them to more than one place. To use Log4j as your log manager in Tomcat (see the separate instructions on how to install and configure the new alpha version of Log4j 1.3 with Apache Tomcat):

  1. Shutdown Tomcat if it is currently running.
  2. Download the Commons Logging package from the Apache web site (unless you already have it).
  3. Copy the commons-logging.jar file from the distribution into your Tomcat common/lib directory.
  4. Download the Log4j package from the Apache web site (unless you already have it).
  5. Copy the log4j.jar file from the distribution into your Tomcat common/lib directory.
  6. Create a log4j.properties file in your Tomcat common/classes directory (see next section).
  7. Restart Tomcat.
The configuration described here results in the creation of two log files for Tomcat 5.5 and three log files for Tomcat 5.0: the Servlet log file (only with Tomcat 5.0), which will roll over to a new file every night; the Commons Logging data via log4j (which will also roll over every night) and the catalina.out file, which will only contain messages that have been printed to standard output and standard error. The last file will grow forever but well behaved applications within your Tomcat server should not be printing to standard out or error, so this should not really be an issue (in general, the file should remain zero length).

Note that the commons-logging.jar and log4j.jar files are installed in the Tomcat common/lib directory and the log4j.properties file is installed in the common/classes directory. This arrangement allows both the Tomcat server and your web applications access to the log4j system. If your web applications use the Commons Logging interface, their log data will be output to the Tomcat server log. You can configure the system to output your application data to a different file by changing the configuration of the Log4j properties file (for help with that, you will have to look elsewhere).

Log4j Properties File
Your log4j.properties file should look something like this:

The only change you have to make is to edit the line with log4j.appender.R.File=/usr/local/tomcat/logs/tomcat.log to point to your Tomcat logs directory.

The above log4j configuration will log info messages as well as those that are more important (warning, error and fatal) to the tomcat.log file in your Tomcat logs directory. The first message that is logged after midnight every day will cause the log file to be renamed with the current date appended to the name tomcat.log and a new tomcat.log file to be created.

The one fly in the ointment with the log4j DailyRollingFileAppender is that in order for the log file to be renamed, a message must be logged to trigger the change. This is a problem if you run a cron job every night to do something with your log files. The Tomcat FileLogger solves this problem by always using the current date as part of the current log's file name. I have written a custom Log4j appender that mimics the behavior of the Tomcat FileLogger which you are free to use (source code included). A separate technical tip describes the DatedFileAppender for Log4j along with installation and configuration instructions.

Please note: The new alpha version of Log4j 1.3 has added support for a daily rolling log file. See Log4j Version 1.3 and Apache Tomcat for instructions on how to install and configure this new feature.

You can override the default logging level for specific applications with the commands at the end of the configuration file. In production, you may want even less logging and you could change the log4j.rootLogger=INFO, R line at the top of the file to read log4j.rootLogger=ERROR, R instead.

Caveat
Be careful when setting the logging level to DEBUG (particularly something like log4j.logger.org.apache, as an enormous amount of information can be generated, slowing your system down considerably.

Tomcat Bootstrapping
Please note, you MUST install commons-logging.jar into your common/lib directory. You might have noticed that there is a commons-logging-api.jar file in the bin (bootstrap) directory of Tomcat. This jar file appears to be a stripped down version of the full commons-logging.jar file which only implements SimpleLog or something like it. Basically, just enough of a logging system to get bootstrapped. While you can't boot without the API version of the logging system, you also can't replace it with a full blown commons-loging.jar file without adding your logging system (i.e. Log4j) to the bootstrap classpath.

Through some classloading voodoo during bootstrapping, if you have the full commons-logging.jar file in your common/lib directory, it replaces the classes from the commons-logging-api.jar file and will reinitialize the logging system and attempt to locate log4j or whatever other logging system you may be using.

Application Logging
You can log messages in your own Servlet applications by either writing to the Servlet log (i.e. servletContext.log('Some message')) or by utilizing the Commons Logging interface. This section describes the latter and far superior method of logging and demonstrates how it integrates with the logging enhancements to Tomcat as described above.

A sample application:

The Log object can be shared between classes and may be declared as a static class variable to facilitate sharing. In other words, your entire application can share a single Log object or you can use multiple Log objects to provide more run-time control over log generation. You pass the LogFactory.getLog() method a class, from which the fully qualified name of that class is used as a unique name for that logging object. In the above example, the name would be com.acme.webapp.MyApplication. By giving each log object a name, it can be controlled in the log4j.properties file using that same identifier. In this case, you could add the following line to your properties file to log DEBUG messages in your application:

The name you give each Log object can be any arbitrary string. However, the general convention is to use the name of the class that creates the Log object. The log4j FAQ describes an alternate naming system.

Tomcat Log4j

Appendix 1 contains a list of Log objects used by Tomcat. One of the more powerful features of log4j is the ability to control hierarchies of Log objects with a single command. For example, the following entry in the log4j.properties file would produce DEBUG level output from all of the Tomcat login (or realm) handlers:

All the best Circle Sketch 38+ collected on this page. Feel free to explore, study and enjoy paintings with PaintingValley.com. Circle sketch. Draw a circle around this. 1 0 0 0% Have another go! Too close to dot; Draw a full circle; Too slow; Wrong way; New best score; Best: 0.0%. Tweet Share WhatsApp. In Sketch hit the O-key (o like oval, not zero) and draw a circle while holding down the Shift-key to make it nice and even. Start with a circle Apply a border, give it a color (I used a gradient) and a thickness. Set the border position to center. Hand drawn circles sketch frame set. Scribble line circle. Hand drawn circle line sketch set. Art design round circular scribble. Circular doodle frame, hand drawn pen stroke circle and circled frames isolated vector set.

This would affect JAASMemoryLoginModule, JAASRealm, MemoryRealm and RealmBase.

Tomcat and Jakarta Velocity
Version 1.4 of the Velocity template engine supports the log4j logger, amongst others. By default, Velocity will try to use the Jakarta Avalon Logkit. To change this behavior, you must either:

  • Make sure that no version of the logkit jar file exists in your Tomcat configuration; or
  • Add the following line to your velocity.properties file:
The latter option is preferable.

That just gets Velocity using Log4j. Velocity will still send its log data to a file named velocity.log. To get Velocity to use the Tomcat system log instead, add the following directive to your velocity.properties file:

This is the name of the Log object to use. Just about any class name would work here, but this is consistent with recommended practice. To change the log level of the Velocity logger to INFO, for example, add this line to your log4j.properties file:

WEB-INF Logging
You can override the use of the system log by your applications and set up customized logging to other files or places (using remote sockets). This is done by installing the commons-logging.jar and log4j.jar files in the WEB-INF/lib directory of the application in question, along with a custom log4j.properties file in the corresponding WEB-INF/classes directory. However, I have not tried this, so you are on your own with this one.

Logger Identifiers in Apache Code
To see what classes are creating Log objects in an Apache project, run the following command in the corresponding source code directory:

Code that uses the older and deprecated log4j API can be scanned thus:

Assuming you know what you are looking for, you can use this information to enable more intense logging in a particular area of an Apache product to pinpoint your problem.

Appendix 1 - Logger Identifiers in Tomcat 5.0.27
The following is a list of logger names in Tomcat 5.0.27:

Appendix 2 - Logger Identifiers in Jakarta Struts 1.1
The following is a list of logger names in Jakarta Struts 1.1:

DatedFileAppender for Log4j and Tomcat

Written by Geoff Mottram (geoff at minaret dot biz).

Placed in the public domain on August 28, 2004 by the author.

Last updated: January 27, 2005.

This document and all associated software is provided 'as is', without warranty of any kind, express or implied, including but not limited to the warranties of merchantability, fitness for a particular purpose and noninfringement. In no event shall the author be liable for any claim, damages or other liability, whether in an action of contract, tort or otherwise, arising from, out of or in connection with this document and associated software or the use or other dealings in same.

Contents
Introduction
File Loggers
Installing DatedFileAppender
Compiling DatedFileAppender
Configuring DatedFileAppender
Log File Directory
Change History

Introduction
DatedFileAppender is an 'appender' object designed for use with the Apache Log4j logging system. It provides an Apache Tomcat style FileLogger implementation that differs from the file loggers provided with Log4j. While the DatedFileAppender was written with Apache Tomcat in mind, it does not depend on Tomcat and may be used in any other environment.

Please note: The new alpha version of Log4j 1.3 has added support for a daily rolling log file. See Log4j Version 1.3 and Apache Tomcat for instructions on how to install and configure this new feature.

A separate technical tip describes how to configure Log4J to be the default system Logger for Tomcat. In brief, you must:

  1. Shutdown Tomcat if it is currently running.
  2. Download the Commons Logging package from the Apache web site (unless you already have it).
  3. Copy the commons-logging.jar file from the distribution into your Tomcat common/lib directory.
  4. Download the Log4j package from the Apache web site (unless you already have it).
  5. Copy the log4j.jar file from the distribution into your Tomcat common/lib directory.
  6. To use DatedFileAppender as the file logger in Tomcat, install the datedFileAppender-1.0.2.jar file into your Tomcat common/lib directory.
  7. Install a log4j.properties file (a sample file comes with the DatedFileAppender distribution) into your Tomcat common/classes directory.
  8. Restart Tomcat.

The DatedFileAppender source code, binaries and associated documentation have all been placed in the public domain.

File Loggers
The standard Log4J DailyRollingFileAppender writes to a log file (say tomcat.log) until some pre-established criteria has been met (i.e. the current day has changed). At that point, the existing log file is closed and renamed using some sort of file name extension (i.e. moved to tomcat.log.2004-08-28). The original log file is then re-created and the system continues to chug along. In order to trigger a log rotation, a message must be logged to the log file. This creates a problem if you want to be able to run a cron job at night to analyze, compress or remove your old log files. This is because you can't be sure that the log files have been rotated (if you are logging lots of data, the logs will probably have been rotated, but if you are only logging ERROR level messages, you have no guarantee).

The FileLogger implementation that comes with Tomcat to log Servlet messages avoids this problem by always using dates in the log file names. Any time after midnight, you know that Tomcat's Servlet logs will not get any more information. There is no guarantee that a previous log file has been closed by the Tomcat FileLogger because it too relies on logging a new message in order to trigger a log file change. However, for your purposes it doesn't matter since the old log files will not be receiving any more messages.

The DatedFileAppender, described here, is a drop in replacement for the Log4J DailyRollingFileAppender but which operates in a manner similar to Tomcat's FileLogger.

Installing DatedFileAppender
DatedFileAppender is available as a zip file that contains this documentation, a sample log4j.properties file for Tomcat, a jar file and the source code for the DatedFileAppender class. The directions and supporting shell scripts were written for a Unix-like system. However, users of other operating systems should have no problem making the appropriate adjustments.

Stand alone installation instructions:

  1. Download the datedFileAppender-1.0.2.zip file to your computer.
  2. Unzip the contents of the zip file. This will create a datedFileAppender-1.0.2 subdirectory in your current directory.
  3. Change to the datedFileAppender-1.0.2 subdirectory.
  4. Copy the datedFileAppender-1.0.2.jar to the same directory as your other application jar files and add the jar file name to your CLASSPATH.
  5. Modify your log4j.properties file to use the DatedFileAppender class (see below for configuration options).

Installation instructions for use as the Tomcat system logger:

  1. Shutdown Tomcat if it is currently running.
  2. Download the datedFileAppender-1.0.2.zip file to your computer.
  3. Unzip the contents of the zip file. This will create a datedFileAppender-1.0.2 subdirectory in your current directory.
  4. Change to the datedFileAppender-1.0.2 subdirectory.
  5. Copy the datedFileAppender-1.0.2.jar to your Tomcat common/lib directory.
  6. Copy the log4j.properties to your Tomcat common/classes directory. If you already have a properties file, edit it as appropriate to use the DatedFileAppender class (see below for configuration options).
  7. Restart Tomcat.
If you use the log4j.properties file that is provided, three log files will be created in your Tomcat logs directory: catalina.out,

Tomcat Log4j Configuration

servlet.yyyy-mm-dd.log and

Tomcat Log4j2

tomcat.yyyy-mm-dd.log; where yyyy-mm-dd will be replaced with today's date.

The servlet log contains output from calls to the Java servlet log() method. The tomcat log will contain messages that have been output by applications using the Apache Commons Logging interface and catalina.out contains any spurious output from standard output and standard error.

Compiling DatedFileAppender
The source code for DatedFileAppender can be found in the src subdirectory. If you are inclined to recompile the DatedFileAppender source code, a shell script has been provided (compile.sh) to assist you. Before running the script, you must edit it and change the CLASSPATH environment variable to point to your log4j jar file.

You run the script by typing:

Once run, the compiled class file and associated jar file can be found in the build subdirectory.

Configuring DatedFileAppender
The full name of the DatedFileAppender class is:

DatedFileAppender supports buffered writes to its log file. By default, no buffering is used and log entries are written to the log file as soon as they are received. This insures that no new entries will be added to a log file after midnight of any given day. On a heavily loaded system there could be cases where the logger is preempted while writing an entry just before midnight such that the log file would not be physically updated until a little after midnight. Any log file related cron jobs should be run a reasonable time after midnight to be on the safe side.

If you enable buffering by setting the BufferedIO=true property in your log4j.properties file, there is no guarantee that DatedFileAppender has finished writing to a daily log file after midnight. A new message must be logged after midnight to force the previous day's log file to be flushed and closed.

If you are logging so much data that you require write buffering, you will probably log a message soon after midnight, forcing a log file roll-over. To be absolutely sure, any cron based external log file processing that is done could wait for the new day's log file to appear before starting its work on what is now yesterday's log file.

The log4j.properties file included with the distribution can be used to configure a DatedFileAppender as the Tomcat system logger. This file also illustrates all of the configuration options and default values for this class. The file is reproduced here:

Log File Directory
You can set the directory that DatedFileAppender uses for its log files with the Directory property in the Log4J properties file. An example using the default directory is:

If the directory name is absolute, as determined by the Java File.isAbsolute() method (under Unix, this means having a leading slash), DatedFileAppender will use that directory to store its log files.

If a relative directory name is used, DatedFileAppender will look for the system property 'catalina.base'. This is the base directory for Tomcat and is set by Tomcat when it is running. If this property is set, DatedFileAppender will use this as its base directory, appending the name you provide in the Directory property to create the full path name of the logs directory. If there is no 'catalina.base' system property, the current directory is used as the base directory.

If you are not using Tomcat but you want DatedFileAppender to use a base directory other than the directory you start your application in, set the 'catalina.base' system property when you run your application. The best place to do this is when you start your application, like this:

If there are spaces in the directory name, you will need quotes, as such:

Tomcat Log4j Configuration File Location

Change History
VersionDateDescription
1.0.2September 15, 2004Replaced calls to Calendar.getTimeInMillis() and Calendar.getTimeInMillis() for backwards compatibility with JVM's prior to 1.4 in which these methods are protected.
1.0.1September 8, 2004Documentation changes only.
  1. Added documentation on the issue of I/O buffering.
  2. Fixed example log4j.properties BufferSize entry.
1.0August 28, 2004Initial release.




broken image