spring Aspect log 日志记录

@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>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值