使用Struts2拦截器对日志的管理

最近一个项目客户要求能够监控用户对系统的详细操作步骤,比如某人在某时间删除了某个模块的某条数据,经过一番斟酌,决定用struts2的拦截器功能实现日志管理系统,因为系统是旧系统(SSH架构)了,用struts2就可以不用修改系统的原有的类或者方法,而且可以精确到用户的具体操作,并且所有的日志管理代码直接写在拦截器里即可。代码如下:
   Java代码
@SuppressWarnings("serial")   
public class LogInterceptor extends AbstractInterceptor{   
    private String logName;   
    private String logContent;   
    protected Logger log = Logger.getLogger(getClass());   
       
    public void init() {   
     }   
  
    @Override  
    public String intercept(ActionInvocation ai) throws Exception   {   
        Map session = ai.getInvocationContext().getSession();   
        Object action = ai.getAction();   
        String method = ai.getProxy().getMethod();   
       try{   
           if(StringUtils.isBlank(method)) method = "method";   
            SysUser sysUser = (SysUser)session.get(Constants.SESSION_USER_KEY);   
            String userName = "";   
           if(sysUser!=null) userName = sysUser.getUserName();   
            String currentTime = TimeHelper.getCurrentTime();   
            String logContentHead = "用户"+userName+currentTime;   
            ai.invoke();//执行被拦截action   
              
           if (action instanceof SysOrgAction) {   
               if(method.equals("save")){   
                    logName = "保存部门";   
                    logContent = logContentHead +"保存部门:"+ai.getStack().findValue("sysOrg.orgName");   
                    log.info(logContent);   
                    addSysLog(logName,logContent);   
                }   
               if(method.equals("delete")){   
                    logName = "删除部门";   
                    logContent = logContentHead +"删除"+((String[])(ai.getStack().findValue("flag_id"))).length+"条部门信息";   
                    log.info(logContent);   
                    addSysLog(logName,logContent);   
                }   
            }   
           if (action instanceof SysOrgForAdmAction) {   
               if(method.equals("save")){   
                    logName = "保存单位";   
                    logContent = logContentHead +"保存单位:"+ai.getStack().findValue("sysOrg.orgName");   
                    log.info(logContent);   
                    addSysLog(logName,logContent);   
                }   
               if(method.equals("delete")){   
                    logName = "删除单位";   
                    logContent = logContentHead +"删除1条单位信息";   
                    log.info(logContent);   
                    addSysLog(logName,logContent);   
                }   
            }   
           if (action instanceof SysUserAction || action instanceof SysUserForAdmAction) {   
               if(method.equals("save")){   
                    logName = "保存用户";   
                    logContent = logContentHead +"保存用户:"+ai.getStack().findValue("sysUser.userName");   
                    log.info(logContent);   
                    addSysLog(logName,logContent);   
                }   
               if(method.equals("delete")){   
                    logName = "删除用户";   
                    logContent = logContentHead +"删除"+((String[])(ai.getStack().findValue("flag_id"))).length+"条用户信息";   
                    log.info(logContent);   
                    addSysLog(logName,logContent);   
                }   
            }   
              
        }catch(Exception e){   
            e.printStackTrace();   
        }   
       return Action.SUCCESS;   
    }   
      
    /**
      * 插入系统日志
      * @param logName
      * @param logContent
      */  
   private void addSysLog(String logName,String logContent){   
        HttpServletRequest request = ServletActionContext.getRequest();   
        SysLogService sysLogService = (SysLogService)ApplicationContextFactory.getApplicationContext().getBean("sysLogService");   
        SysLog sysLog = new SysLog();   
        sysLog.setLogName(logName);   
        sysLog.setLogContent(logContent);   
        sysLog.setLogType(Constants.LOG_TYPE_SYS);   
        sysLog.setLogTime(TimeHelper.getCurrentTime());   
        sysLog.setLogIp(request.getRemoteAddr());   
        sysLog.setLogKey(logName);   
        sysLogService.saveLog(sysLog);   
    }   
}  
[color=orange][/color]

你可能感兴趣的:(项目管理,ssh)