package package_name;
import java.io.IOException;
import java.util.Properties;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
public class Log {
public Log()
{
Properties props = new Properties();
try {
props.load(getClass().getResourceAsStream("/log4j.properties"));
} catch (IOException e) {
e.printStackTrace();
}
PropertyConfigurator.configure(props);//PropertyConfigurator.configure("log4j.properties");
}
public Logger getLogger(Object obj)
{
Logger logger = Logger.getLogger(Object.class);
return logger;
}
}
然后我们必须在我们的一个包中维护一个log4j.properties文件,该文件应如下所示,
log4j.properties
log4j.rootLogger=DEBUG, R,CA
log4j.appender.R = org.apache.log4j.DailyRollingFileAppender
log4j.appender.R.File = c:\\our project name+LOGSLIVE\\logs\\project short name.log
log4j.appender.R.Append = true
log4j.appender.R.DatePattern = '_'yyyy-MM-dd'.log'
log4j.appender.R.layout = org.apache.log4j.PatternLayout
#log4j.appender.R.layout.ConversionPattern = %d{yyyy-MM-dd HH:mm:ss} [%t] [%p] %m%n
log4j.appender.R.layout.ConversionPattern = %d{yyyy-MM-dd HH:mm:ss} %5p [%t] (%F:%L) - %m%n
#Console Appender
log4j.appender.CA=org.apache.log4j.ConsoleAppender
log4j.appender.CA.layout=org.apache.log4j.PatternLayout
log4j.appender.CA.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n