1. Put the following log4j.properties file at root directory at the Eclipse JUnit project
####################################
# Categories & Levels
####################################
#For develop
log4j.rootCategory=DEBUG, ConApp, FileApp
####################################
# Appender
####################################
# ConApp is set to be a ConsoleAppender
log4j.appender.ConApp=org.apache.log4j.ConsoleAppender
log4j.appender.ConApp.layout=org.apache.log4j.PatternLayout
log4j.appender.ConApp.layout.ConversionPattern=%d [%t] %-5p %l - %m%n
# FileApp is to create log-file using FileAppender
log4j.appender.FileApp=org.apache.log4j.FileAppender
log4j.appender.FileApp.File=./test.log
log4j.appender.FileApp.ImmediateFlush=true
log4j.appender.FileApp.Append=false
log4j.appender.FileApp.layout=org.apache.log4j.PatternLayout
log4j.appender.FileApp.layout.ConversionPattern=%d [%t] %-5p %l - %m%n
2. Add static initializer to JUnit test class
private static Logger gLogger = Logger.getLogger(TestClass.class);
static
{
PropertyConfigurator.configure("log4j.properties");
}
Run a JUnit test, then log4j log will be shown at console and file.