develog

LogAspect 본문

Dev/Spring

LogAspect

냐옴 2013. 12. 11. 19:06


import java.util.List;


import org.aspectj.lang.JoinPoint;

import org.aspectj.lang.Signature;


public class LogAspect {


public void before(JoinPoint joinPoint) {

StringBuffer sb = new StringBuffer();

sb.append(getFullName(joinPoint));

sb.append(" (before)");

sb.append(getArgs(joinPoint));

MyLogger.debug(sb.toString());

}

public void afterReturning(JoinPoint joinPoint, Object ret) {

        StringBuffer sb = new StringBuffer();

        sb.append(getFullName(joinPoint));

        sb.append(" (afterReturning)");

        

        if (ret instanceof List) {

            List list = (List) ret;

            if (list == null) {

                sb.append("\n" + "    " + "LIST, " + "null");

            } else {

                sb.append("\n" + "    " + "LIST, " + list.size() + " 건");

            }

        } else {

            sb.append("\n" + "    " + ret);

        }

        

MyLogger.debug(sb.toString());

}

public void afterThrowing(JoinPoint joinPoint, Exception ex) {

StringBuffer sb = new StringBuffer();

sb.append(getFullName(joinPoint));

sb.append(" (afterThrowing)");

sb.append(String.valueOf(ex));

MyLogger.debug(sb.toString());

}

public void exceptionThrowing(JoinPoint joinPoint, Exception ex) {

StringBuffer sb = new StringBuffer();

sb.append(getFullName(joinPoint));

sb.append(" (exceptionThrowing)");

sb.append(String.valueOf(ex));

MyLogger.debug(sb.toString());

}

private String getFullName(JoinPoint joinPoint) {

Signature signature = joinPoint.getSignature();


StringBuffer sb = new StringBuffer();

sb.append(signature.getDeclaringTypeName());

sb.append(".");

sb.append(signature.getName());

return sb.toString();

}

private String getArgs(JoinPoint joinPoint) {

StringBuffer sb = new StringBuffer();

Object[] args = joinPoint.getArgs();

for (int i = 0; i < args.length; i++) {

sb.append("\n" + "    " + i + ". " + args[i]);

}

return sb.toString();

}

}



'Dev > Spring' 카테고리의 다른 글

[Spring] beans xml schema  (0) 2014.10.15
Spring 에서 HttpServletRequest 접근  (0) 2013.12.11
Spring AOP Pointcut expression  (0) 2013.12.11
Spring AOP 설정  (0) 2013.12.11
Spring AOP LoggingAdvice  (0) 2013.11.29
Comments