| www.rodneybeede.com | "I would love to change the world, but they won't give me the source code" - unknown |
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/xml/doc-files/log4j.dtd">
<!-- http://wiki.apache.org/logging-log4j/Log4jXmlFormat -->
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<!-- The appender name "rolling" is referenced in Java code to display the absolute path of where logging is occurring -->
<appender name="rolling" class="org.apache.log4j.rolling.RollingFileAppender">
<!-- The active file to log to -->
<param name="file" value="${catalina.home}/logs/WebApplication.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="${catalina.home}/logs/WebApplication.%d.log.gz" />
</rollingPolicy>
<layout class="org.apache.log4j.PatternLayout">
<!-- %d = date, %p = level, %t = thread, %c = category (usually class),
%m = message, %n = newline -->
<param name="ConversionPattern"
value="%d{yyyy-MM-dd HH:mm:ss,SSS Z}\t%-5p\tThread=%t\t%c\t%m%n" />
</layout>
</appender>
<appender name="ASYNC" class="org.apache.log4j.AsyncAppender">
<param name="BufferSize" value="500" />
<appender-ref ref="rolling" />
<!-- Don't log to console as that fills up the web container log (ex: catalina.log) which may not roll -->
</appender>
<root>
<priority value="all" />
<appender-ref ref="ASYNC" />
</root>
</log4j:configuration>
<dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.16</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>apache-log4j-extras</artifactId> <version>1.0</version> </dependency>
private static final Logger log = Logger.getLogger(SOMECLASS.class);
This however may cause your IDE to go out on the Internet to retrieve the DTD which could be slow if Apache's website is busy. You'll just get a hang with no useful error message when you rebuild.
package com.rodneybeede.cloudinabottle.utils;
import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Enumeration;
import org.apache.log4j.Appender;
import org.apache.log4j.FileAppender;
import org.apache.log4j.Logger;
import org.apache.log4j.spi.AppenderAttachable;
public class LoggingUtils {
/**
* @param logger Usually {@link Logger#getRootLogger()}
* @return File handle objects to all appenders that subclass {@link FileAppender} and have file settings != null. Recursive.
*/
public static Collection getAppenderFiles(final org.apache.log4j.Logger logger) {
@SuppressWarnings("unchecked")
final Enumeration childrenEnumeration = logger.getAllAppenders();
return getAppenderFiles(childrenEnumeration);
}
public static Collection getAppenderFiles(final Enumeration appenderEnumeration) {
final Collection files = new ArrayList();
while(appenderEnumeration.hasMoreElements()) {
final Appender currAppender = appenderEnumeration.nextElement();
if(currAppender instanceof FileAppender) {
final FileAppender fileAppender = (FileAppender) currAppender;
if(null != fileAppender.getFile()) {
files.add(new File(fileAppender.getFile()).getAbsoluteFile());
}
}
if(currAppender instanceof AppenderAttachable) {
// Recursive
@SuppressWarnings("unchecked")
final Enumeration childrenEnumeration = (((AppenderAttachable) currAppender).getAllAppenders());
files.addAll(getAppenderFiles(childrenEnumeration));
}
}
return files;
}
}