001 package com.hs.mail.aop;
002
003 import org.apache.log4j.Logger;
004 import org.aspectj.lang.ProceedingJoinPoint;
005 import org.springframework.util.StopWatch;
006 import org.springframework.util.StringUtils;
007
008 public class SimpleProfiler {
009
010 protected Logger logger = Logger.getLogger(getClass());
011
012 protected String logName = null;
013
014 public void setLogName(String logName) {
015 this.logName = logName;
016 }
017
018 public Object profile(ProceedingJoinPoint call) throws Throwable {
019 if (logger == null) {
020 synchronized (this) {
021 logger = Logger.getLogger((logName != null) ? logName
022 : getClass().getName());
023 }
024 }
025 StopWatch clock = new StopWatch(call.toShortString());
026 clock.setKeepTaskList(false);
027 clock.start();
028 try {
029 return call.proceed();
030 } finally {
031 clock.stop();
032 logger.info(shortSummary(call, clock));
033 }
034 }
035
036 private String shortSummary(ProceedingJoinPoint call, StopWatch clock) {
037 return call.toShortString() + ": running time (millis) = "
038 + clock.getTotalTimeMillis() + " {"
039 + StringUtils.arrayToCommaDelimitedString(call.getArgs()) + "}";
040 }
041
042 }