@Aspect
public class LogAspect {
/** The Constant LOG. */
private static final Logger LOG = Logger
.getLogger(LogAspect.class);
/**
* Gets the lOG.
*
* @return the lOG
*/
private static Logger getLOG() {
return LOG;
}
@Pointcut("execution(public * com.test.service..*.*(..))")
public void serviceCall() { }
@Pointcut("execution(public * com.test.service.message.*.pollMessage(..))")
public void servicePollMessageCall() { }
@Pointcut("execution(public * com.sony.model.dao..*.*(..))")
public void daoCall() { }
@Pointcut("execution(public * com.test.model.dao.UserDaoImpl.getByUsername(..))")
public void daoGetByUsernameCall() { }
@Pointcut("execution(public * com.test.model.dao.messagepoll.MessagePollDaoImpl.pollMessage(..))")
public void daoPollMessageCall() { }
@Around(value="(serviceCall()&&!servicePollMessageCall())||(daoCall()&&!daoGetByUsernameCall()&&!daoPollMessageCall())")
public Object doServiceAround(ProceedingJoinPoint call) throws Throwable{
Object result = null;
String className = call.getTarget().getClass().getName();
String methodName = call.getSignature().getName();
String userName = null;
try {
userName = AcegiUtils.getAcegiUtils().getLoggedUser().getUsername();
} catch(Exception e) {
userName = "Login";
}
Object[] args= call.getArgs();
String logInfoAfter ="User: "+userName+" invoke "+className+" method "+methodName+" with parameters "+argsToStr(args);
try {
result = call.proceed();
logInfoAfter += " result "+(null==result?"void":result.toString());
// System.out.println("logInfoAfter="+logInfoAfter);
getLOG().info(logInfoAfter);
} catch (Throwable e) {
getLOG().error(logInfoAfter+" error message:"+e.getMessage());
throw e;
}finally{
//this.after();
}
return result;
}
@AfterThrowing(pointcut="servicePollMessageCall()||daoPollMessageCall()",throwing="ex")
public void doServicePollMessageAround(JoinPoint call,Exception ex){
String className = call.getTarget().getClass().getName();
String methodName = call.getSignature().getName();
String userName = null;
try {
userName = AcegiUtils.getAcegiUtils().getLoggedUser().getUsername();
} catch(Exception e) {
userName = "Login";
}
Object[] args= call.getArgs();
String logInfoAfter ="User: "+userName+" invoke "+className+" method "+methodName+" with parameters "+argsToStr(args);
getLOG().error(logInfoAfter+" error message:"+ex.getMessage());
}
private String argsToStr(Object[] args) {
String argsStr = "";
for(int i=0;i<args.length;i++) {
argsStr += args[0]+",";
}
return argsStr;
}
public String getSessionId() {
if(SecurityContextHolder.getContext() == null){
return null;
}
if(SecurityContextHolder.getContext().getAuthentication() == null){
return null;
}
WebAuthenticationDetails details=(WebAuthenticationDetails)SecurityContextHolder.getContext().getAuthentication().getDetails();
if(details == null){
return null;
}
return details.getSessionId();
}
}
spring xml
<aop:aspectj-autoproxy/>
<bean id="logAspect" class="com.sony.aop.LogAspect"></bean>