log4j.rootLogger=DEBUG
log4j.appender.SampleAppender =org.apache.log4j.FileAppender
log4j.appender.SampleAppender .File=Sample.log
log4j.appender.SampleAppender .layout=org.apache.log4j.PatternLayout
log4j.appender.SampleAppender .layout.ConversionPattern= [%t] %-5p %c{2} %d %x - %m%n
log4j.logger.org.sample=,SampleAppender
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
public class Sample {
static Logger logger = Logger.getLogger(Sample.class);
public static void debug(String message, boolean debugModeOn) {
if (debugModeOn) {
PropertyConfigurator.configure("log4j.properties");
logger.debug( message);
}
}
log4j.properties file must be in classpath..
for log format: http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/PatternLayout.html