Here's a little helper class for all users who want to write their webapp's log file(s) to a directory relative to the context directory without any extra configuration.
A usage example is included in the javadoc comments of the class. --- package com.toolbox.log4j; import java.io.File; import org.apache.log4j.Appender; import org.apache.log4j.Category; import org.apache.log4j.FileAppender; import org.apache.log4j.LogManager; import org.apache.log4j.spi.HierarchyEventListener; /** * Helper class used to configure log4j file appenders with a path that * is computed at runtime. Initially implemented to enable logging to * a context-relative location when running in a web container like tomcat. * * <p>Usage example:</p> * * Use a relative filename in the log4j configuration. * <pre> * log4j.rootCategory=ERROR, WEBAPP * log4j.appender.WEBAPP=org.apache.log4j.DailyRollingFileAppender * log4j.appender.WEBAPP.File=logs/webapp.log * (Rest of configuration omited.) * </pre> * * Use the configurator in your initialization code like this: * <pre> * final String path = config.getServletContext().getRealPath("/WEB-INF"); * * new LogDirectoryConfigurator(path) { * @Override * protected void doConfigure() { * PropertiesConfigurator.configure(path + "/log4j.properties"); * } * }.configure(); * </pre> * * The logfile will be created as <tt>/WEB-INF/logs/webapp.log</tt>. * * @author fischer * @version $Revision: 1.0 $ $Date: 2007/06/26 11:00:50 $ */ public abstract class LogDirectoryConfigurator implements HierarchyEventListener { private final File directory; /** * Creates a new [EMAIL PROTECTED] LogDirectoryConfigurator} that uses the specified * base path. * * @param basePath the base path. */ public LogDirectoryConfigurator(String basePath) { this(new File(basePath)); } /** * Creates a new [EMAIL PROTECTED] LogDirectoryConfigurator} that uses the * base directory represented by the specified file. * * @param baseDirectory the base directory. */ public LogDirectoryConfigurator(File baseDirectory) { this.directory = baseDirectory; } /** * Performs the configuration of the log4j system. * * @see #doConfigure() */ public void configure() { LogManager.getLoggerRepository().addHierarchyEventListener(this); doConfigure(); } /** * Scans the configuration of the specified appender. If the configured * filename denotes a relative path, the appender is re-configured to * write to the absolute path <tt><base directory> * <relative path></tt>. * * @param category [EMAIL PROTECTED] * @param appender [EMAIL PROTECTED] */ public void addAppenderEvent(Category category, Appender appender) { if (! (appender instanceof FileAppender)) return; FileAppender fileAppender = (FileAppender) appender; File file = new File(fileAppender.getFile()); if (file.isAbsolute()) return; String relativePath = fileAppender.getFile(); String absolutePath = new File(directory, relativePath).getAbsolutePath(); fileAppender.setFile(absolutePath); } /** * [EMAIL PROTECTED] */ public void removeAppenderEvent(Category category, Appender appender) { // Intentionally left empty. } /** * Implement this method to read the log4j configuration. All relative * paths configured for any [EMAIL PROTECTED] FileAppender} are prefixed with * the base directory. */ protected abstract void doConfigure(); } -- GMX startet ShortView.de. Hier findest Du Leute mit Deinen Interessen! Jetzt dabei sein: http://www.shortview.de/[EMAIL PROTECTED] --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]