You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4j-user@logging.apache.org by "Bryant, Thomas J - St Louis, MO" <Th...@usps.gov> on 2010/04/29 15:58:05 UTC
Configure Appender Programatically
I am getting a warning with this code that I don't undrstand. I have set the appender file but it complains that I haven't and doesn't create the log. Is there something I am doing wrong?
import java.io.File;
import java.io.IOException;
import org.apache.log4j.DailyRollingFileAppender;
import org.apache.log4j.Logger;
import org.apache.log4j.PatternLayout;
import org.apache.log4j.PropertyConfigurator;
public class Log4jTest {
protected static org.apache.log4j.Logger logger = Logger.getLogger(Log4jTest.class);
/**
* @param args
*
*/
public static void main(String[] args) {
File currentDirectory = new File (".");
/**
* Use this to set up appenders that every process should use.
* This would be for a common logging among all such as to log
* to the console.
*/
try {
PropertyConfigurator.configure(currentDirectory.getCanonicalPath() +
File.separator + "config" + File.separator + "test_log4j.properties");
} catch (IOException e) {
e.printStackTrace();
}
/**
* Set up an appender.
*/
DailyRollingFileAppender appender = new DailyRollingFileAppender();
appender.setName("File Appender");
appender.setDatePattern("'.'yyyy-MM-dd");
appender.setAppend(true);
PatternLayout layout = new PatternLayout();
layout.setConversionPattern("%-5p %d{ISO8601} [%C][%t] - %m%n");
appender.setLayout(layout);
/**
* Set the log file name for which ever process is running in this instance.
*/
if(args[0].equals("one"))
appender.setFile("logs/one_test.log");
else
appender.setFile("logs/two_test.log");
/**
* Add the appender to your logger
*/
Logger.getRootLogger().addAppender(appender);
/**
* log your face off
*/
logger.info("Logging your face off");
}
}
/** log4j:ERROR No output stream or file set for the appender named [File Appender]. */
TJ Bryant
Senior Programmer/Analyst
STL IBSSC, United States Postal Service