在tomcat中配置方法:
打开tomcat/bin/catalina.sh
找的JAVA_OPTS 变量,添加行
JAVA_OPTS="$JAVA_OPTS -Dnet.spy.log.LoggerImpl=net.spy.memcached.compat.log.Log4JLogger"
在spymemcached中是这样处理的
spy的LoggerFactory类中
@SuppressWarnings("unchecked") private void getConstructor() { Class<? extends Logger> c = DefaultLogger.class; String className = System.getProperty("net.spy.log.LoggerImpl"); if (className != null) { try { c = (Class<? extends Logger>) Class.forName(className); } catch (NoClassDefFoundError e) { System.err.println("Warning: " + className + " not found while initializing" + " net.spy.compat.log.LoggerFactory"); e.printStackTrace(); c = DefaultLogger.class; } catch (ClassNotFoundException e) { System.err.println("Warning: " + className + " not found while initializing" + " net.spy.compat.log.LoggerFactory"); e.printStackTrace(); c = DefaultLogger.class; } } // Find the best constructor try { // Try to find a constructor that takes a single string Class<?>[] args = { String.class }; instanceConstructor = c.getConstructor(args); } catch (NoSuchMethodException e) { try { // Try to find an empty constructor Class<?>[] args = {}; instanceConstructor = c.getConstructor(args); } catch (NoSuchMethodException e2) { System.err.println("Warning: " + className + " has no appropriate constructor, using defaults."); // Try to find a constructor that takes a single string try { Class<?>[] args = { String.class }; instanceConstructor = DefaultLogger.class.getConstructor(args); } catch (NoSuchMethodException e3) { // This shouldn't happen. throw new NoSuchMethodError("There used to be a constructor that " + "takes a single String on " + DefaultLogger.class + ", but I " + "can't find one now."); } // SOL } // No empty constructor } // No constructor that takes a string } // getConstructor
然后再log4j.properties文件中添加
#memcached logger log4j.logger.net.spy=debug, memcachedLog log4j.appender.memcachedLog=org.apache.log4j.DailyRollingFileAppender log4j.appender.memcachedLog.File=d:/memcached2.log log4j.appender.memcachedLog.DatePattern='_'yyyy-MM-dd'.log' log4j.appender.memcachedLog.layout=org.apache.log4j.PatternLayout log4j.appender.memcachedLog.layout.ConversionPattern=%d %p [%t] | %m%n