log4j.xml

Introduction

You can also configure log4j in an xml format.  This is required if you want to use some of the new features from apache-log4j-extra, such as the TimeBasedRollingPolicy, which rolls your files at midnight and gzips them!

Configuration

In order to configure your portal project this way you need to do a few things.  If you want to do it the quick way, just apply the patch I've attached to this page. Otherwise keep reading.

 log4j.xml

Delete your log4j.properties file and create: uportal-war/src/main/webapp/WEB-INF/log4j.xml

There's some pretty good documentation on the log4j.xml format here.

If you want dtd validation copy log4j.dtd from the log4j source to that directory.

Here's the file CalPoly uses:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">

<!-- Note that this file is refreshed by the server every 60seconds, as specified in web.xml -->

<log4j:configuration debug="true">

	<appender name="ROLL" class="org.apache.log4j.rolling.RollingFileAppender">
		<!-- The active file to log to -->
		<param name="file" value="/applogs/myportal/portal.log" />
		<param name="append" value="true" />
		<param name="encoding" value="UTF-8" />

		<rollingPolicy class="org.apache.log4j.rolling.TimeBasedRollingPolicy">
			<!-- The file to roll to, this is a fairly intelligent parameter, if the file
			ends in .gz, it gzips it, based on the date stamp it rolls at that time,
			default is yyyy-MM-dd, (rolls at midnight)
			See: http://logging.apache.org/log4j/companions/extras/apidocs/org/apache/log4j/rolling/TimeBasedRollingPolicy.html -->
			<param name="FileNamePattern" value="/applogs/myportal/portal.%d.log.gz" />
		</rollingPolicy>

		<layout class="org.apache.log4j.PatternLayout">
			<!-- The log message pattern -->
			<param name="ConversionPattern" value="%5p %d{ISO8601} [%t][%x] %c - %m%n" />
		</layout>
	</appender>

	<!-- Loggers to filter out various class paths -->

	<logger name="org.hibernate.engine.loading.LoadContexts" additivity="false">
		<level value="error"/>
		<appender-ref ref="ROLL" />
	</logger>

	<!-- Debugging loggers -->

	<!-- Uncomment to enable debug on calpoly code only -->
	<!--
	<logger name="edu.calpoly">
		<level value="debug"/>
		<appender-ref ref="ROLL" />
	</logger>
	-->

	<root>
		<priority value="info" />
		<appender-ref ref="ROLL" />
	</root>

</log4j:configuration>

Note that you can also do advanced filtering on message contents with <filter /> but the documentation is a bit scarce and I had no need for it. (This is something you couldn't do with log4j.properties)

Modify web.xml

Modify the web.xml file in the same directory and change log4j.properties to log4j.xml:

<context-param>
        <param-name>log4jConfigLocation</param-name>
        <param-value>/WEB-INF/log4j.xml</param-value>
    </context-param>

Add the apache-log4j-extra dependency to Maven

If you want to use TimeBasedRollingPolicy you'll need to add apache-log4j-extra to your pom.xml files:

Edit the root pom.xml:

Add a version line in the appropriate spot:

<log4j.extras.version>1.0</log4j.extras.version>

Further down add a dependency section bellow the main log4j one:

<dependency>
	<groupId>log4j</groupId>
    	<artifactId>apache-log4j-extras</artifactId>
    	<version>${log4j.extras.version}</version>
</dependency>

Now edit uportal-war/pom.xml

Add the following section:

<!-- ===== Runtime Time Dependencies ============================== -->
<dependency>
	<groupId>log4j</groupId>
	<artifactId>apache-log4j-extras</artifactId>
	<scope>runtime</scope>
/dependency>