Update Log4j Appender at runtime

Categories: Java; Tagged with: ; @ April 17th, 2013 18:20
import java.io.IOException;

import org.apache.log4j.FileAppender;
import org.apache.log4j.Logger;
import org.apache.log4j.PatternLayout;

/**
 * Test for adding appender to log4j at runtime.
 * @author SGSCLGLD
 *
 */
public class LogingTest {
	
	static Logger log = Logger.getLogger(LogingTest.class);
	
	public static void main(String[] args) throws IOException {
		// setting up a FileAppender at runtime.
		PatternLayout patternLayout = new PatternLayout();
		patternLayout.setConversionPattern("[%d{d/M/yyyy HH:mm:ss}] %p [%t] %c{1}.%M(%L) | %m%n");
		FileAppender appender = new FileAppender(patternLayout,"D:\\ME\\WS_Share\\test1\\log.txt",false);    
		log.addAppender(appender);
		
		log.info("Hello world...");
	}
}

<->



// Proudly powered by Apache, PHP, MySQL, WordPress, Bootstrap, etc,.