日期:2014-05-20 浏览次数:21407 次
package testLog; import org.apache.log4j.PropertyConfigurator; public class Log4jConfig { private static boolean isReload = true; public static void load() { String path = Log4jConfig.class.getClass().getResource("/") .getPath() + "testLog/log4j.properties"; //String path="config/log4j.properties"; System.out.println("log4j configfile path=" + path); PropertyConfigurator.configureAndWatch(path,1000); } private static void reload() { if (isReload) { load(); } isReload = false; } public void setReload(boolean flag) { isReload = flag; } }
package testLog; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; public class Logger { private Log log = null; static{ Log4jConfig.load();//装载log4j配置文件 } private Logger() { log = LogFactory.getLog(this.getClass()); } private Logger(Class c) { log = LogFactory.getLog(c); } private Logger(String className) { log = LogFactory.getLog(className); } public static Logger getLogger() { return new Logger(); } public static Logger getLogger(Class c) { return new Logger(c); } public static Logger getLogger(String className) { return new Logger(className); } public void trace(String info) { if (log.isTraceEnabled()) log.trace(info); } public void debug(String info) { if (log.isDebugEnabled()) log.debug(info); } public void info(String info) { if (log.isInfoEnabled()) log.info(info); } public void warn(String info) { if (log.isWarnEnabled()) log.warn(info); } public void error(String info) { if (log.isErrorEnabled()) log.error(info); } public void fatal(String info) { if (log.isFatalEnabled()) log.fatal(info); } public boolean isTraceEnabled() { return log.isTraceEnabled(); } public boolean isDebugEnabled() { return log.isDebugEnabled(); } public boolean isInfoEnabled() { return log.isInfoEnabled(); } public boolean isWarnEnabled() { return log.isWarnEnabled(); } public boolean isErrorEnabled() { return log.isErrorEnabled(); } public boolean isFatalEnabled() { return log.isFatalEnabled(); } }
log_home=../log
log4j.rootLogger=file,Console
log4j.appender.logfile=org.apache.log4j.DailyRollingFileAppender
log4j.appender.logfile.File=C:\gwlog.log
log4j.appender.logfile.DatePattern=.yyyy-MM-dd
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - <%m>%n