Return to Snippet

Revision: 4709
at January 17, 2008 03:41 by narkisr


Updated Code
import com.alphacsp.common.log.Log;
import org.apache.log4j.Category;
import org.apache.log4j.FileAppender;
import org.apache.log4j.AppenderSkeleton;

import java.lang.reflect.Field;
import java.util.Enumeration;

public class FileAppenderManipulation {

    public void setAppenderFilenameForLogger(Log log, String filename, String appenderName) {
        try {
            Category category = getCategory(log);
            final FileAppender fileAppender = (FileAppender) locateAppender(category, appenderName);
            fileAppender.setFile(filename);
            fileAppender.activateOptions();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

    }

    private AppenderSkeleton locateAppender(Category category, String name) {
        do {
            final Enumeration appenders = category.getAllAppenders();
            while (appenders.hasMoreElements()) {
                AppenderSkeleton appenderSkeleton = (AppenderSkeleton) appenders.nextElement();
                if (appenderSkeleton.getName().equals(name)) {
                    return appenderSkeleton;
                }
            }
        } while ((category = category.getParent()) != null);
        throw new RuntimeException("The requested appender dosn't exists!");
    }

    private Category getCategory(Log log) throws NoSuchFieldException, IllegalAccessException {
        Field field = log.getClass().getDeclaredField("category");
        field.setAccessible(true);
        Category category = (Category) field.get(log);
        return category;
    }
}

Revision: 4708
at January 17, 2008 03:00 by narkisr


Updated Code
public static void setAppenderForLogger(Log log, String filename) {
		try {
			Field field = log.getClass().getDeclaredField("category");
			field.setAccessible(true);
			Category category = (Category) field.get(log);
			FileAppender fileAppender = (FileAppender) category.getAppender("AppenderName");
			fileAppender.setFile(filename);
			fileAppender.activateOptions();

		} catch (NoSuchFieldException e) {
                  e.printStackTrace();
		} catch (IllegalAccessException e) {
                  e.printStackTrace();
		}

	}

Revision: 4707
at January 17, 2008 02:59 by narkisr


Initial Code
public static void setAppenderForLogger(Log log, String filename) {
		try {
			Field field = log.getClass().getDeclaredField("category");
			field.setAccessible(true);
			Category category = (Category) field.get(log);
			FileAppender fileAppender = (FileAppender) category.getAppender("F2");
			fileAppender.setFile(filename);
			fileAppender.activateOptions();

		} catch (NoSuchFieldException e) {
           e.printStackTrace();
		} catch (IllegalAccessException e) {
           e.printStackTrace();
		}

	}

Initial URL

                                

Initial Description

                                

Initial Title
Setting file appender log path on runtime

Initial Tags
java

Initial Language
Java