首页 > 代码库 > 三种方案实现日志记录功能
三种方案实现日志记录功能
三种方案实现日志记录功能
方案一、使用拦截器实现日志记录功能
步骤一、首先需要我们自定义一个注解类
package cn.hmy.Util; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; /** * 自定义操作日志注解接口类 * @author Dream * */ //表示在什么级别保存该注解信息 @Retention(RetentionPolicy.RUNTIME) //表示该注解用于什么地方 @Target(ElementType.METHOD) public @interface OperateLog { //操作日志的内容 String content(); //模块名 String moduleName(); //操作类型 String operateType() default ""; //操作编号 String code() default ""; }
步骤二、在我们的action的方法上添加我们自定义的注解
// 判断用户是否成功登录 @OperateLog(moduleName="用户管理",operateType="登录",content="登录系统") public String login() throws Exception { // 获取所有用户信息 List<PsUser> findAll = service.findAll(); // 进行对比 for (PsUser ps_user : findAll) { if (model.getUserCode().equals(ps_user.getUserCode())) {// 登录账号相同 // 验证密码是否相同 if (model.getUserPassWord().equals(ps_user.getUserPassWord())) { // 将登录信息放入session ServletActionContext.getRequest().getSession() .setAttribute("loginuser", ps_user); // 查找当前用户的所有一级权限的集合 List<PsFunction> topFunctionByUid = functionService.getTopFunctionByUid(ps_user.getId().toString()); //查找该用户拥有的所有的权限集合 List<PsFunction> AllFunctionByUid = functionService.getAllFunctionByUid(ps_user.getId().toString()); //放入上下文 ActionContext.getContext().put("topFunctionByUid", topFunctionByUid); ActionContext.getContext().put("AllFunctionByUid", AllFunctionByUid); //登录成功之后,立即更新用户最后一次登录时间 SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd"); Date date = sdf.parse(sdf.format(new Date())); System.out.println(date); ps_user.setLastLoginTime(date); service.update(ps_user); return "login"; } } } return "input"; }
步骤三、定义日志记录的拦截器
package cn.hmy.intercepter; import java.lang.reflect.Method; import java.text.SimpleDateFormat; import java.util.Date; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.struts2.ServletActionContext; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import cn.hmy.Util.OperateLog; import cn.hmy.Util.SpringContextHelper; import cn.hmy.base.BaseService; import cn.hmy.beans.PsLogs; import cn.hmy.beans.PsUser; import cn.hmy.service.PsLogsService; import com.opensymphony.xwork2.ActionInvocation; import com.opensymphony.xwork2.interceptor.AbstractInterceptor; public class OperateLogIntercepter extends AbstractInterceptor{ private static final Log log=LogFactory.getLog(OperateLogIntercepter.class); /*=(PsLogsService) new PsLogsServiceImpl();*/ @Override public String intercept(ActionInvocation invocation) throws Exception { /*Object bean = SpringContextHelper.getBean("psLogsService");*/ ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml"); PsLogsService bean = (PsLogsService)context.getBean("psLogsService"); System.out.println("日志拦截器已经开始启动"); Class<? extends Object> actionClass=invocation.getAction().getClass(); String methodName=invocation.getProxy().getMethod(); //获取当前方法 Method currentMethod=actionClass.getMethod(methodName); System.out.println("当前方法+++++++++++++=="+currentMethod); boolean flag = currentMethod.isAnnotationPresent(OperateLog.class); if(flag){ OperateLog ol=currentMethod.getAnnotation(OperateLog.class); System.out.println(ol.content()); PsLogs logs=new PsLogs(); PsUser user=(PsUser)ServletActionContext.getRequest().getSession().getAttribute("loginuser"); if(user==null){ String userCode = ServletActionContext.getRequest().getParameter("userCode"); String userPassWord= ServletActionContext.getRequest().getParameter("userPassWord"); SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date date = sdf.parse(sdf.format(new Date())); logs.setOperateInfo(ol.content()); logs.setOperatedatetime(date); logs.setUserCode(userCode); ((BaseService<PsLogs>) bean).save(logs); }else{ SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date date = sdf.parse(sdf.format(new Date())); logs.setOperateInfo(ol.content()); logs.setOperatedatetime(date); logs.setUserCode(user.getUserCode()); logs.setUserName(user.getUserName()); ((BaseService<PsLogs>) bean).save(logs); } } return invocation.invoke(); } public static Log getLog() { return log; } }
步骤四、在struts.xml文件中 配置我们的拦截器
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <constant name="struts.ui.theme" value="simple"></constant> <constant name="struts.devMode" value="false"></constant> <constant name="struts.enable.DynamicMethodInvocation" value="true"/> <package name="default" namespace="/" extends="struts-default"> <!--拦截器 --> <interceptors> <!--声明拦截器 --> <!--权限设置拦截器 --> <interceptor name="checkPrivilege" class="cn.hmy.intercepter.CheckPrivilegeIntercepter"></interceptor> <!--操作日志的拦截器 --> <interceptor name="operateLogIntercepter" class="cn.hmy.intercepter.OperateLogIntercepter"></interceptor> <!--配置一个拦截器栈 --> <interceptor-stack name="mystack"> <interceptor-ref name="checkPrivilege"></interceptor-ref> <interceptor-ref name="operateLogIntercepter"></interceptor-ref> <interceptor-ref name="defaultStack"></interceptor-ref> </interceptor-stack> </interceptors> <!--默认使用这个拦截器栈 --> <default-interceptor-ref name="mystack"></default-interceptor-ref> <!--配置全局的result配置 --> <global-results> <result name="loginUI">/jsp/login.jsp</result> <result name="noPrivilegeError">/noPrivilegeError.jsp</result> </global-results> </package> <include file="cn/hmy/action/Ps_userAction.xml"></include> <include file="cn/hmy/action/Ps_FunctionAction.xml"></include> <include file="cn/hmy/action/Ps_roleAction.xml"></include> <include file="cn/hmy/action/Ps_LogsAction.xml"></include> </struts>
方案二、使用aop实现日志记录
步骤一、定义自定义的注解类
package cn.hmy.Util; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; /** * 自定义操作日志注解接口类 * @author Dream * */ //表示在什么级别保存该注解信息 @Retention(RetentionPolicy.RUNTIME) //表示该注解用于什么地方 @Target(ElementType.METHOD) public @interface OperateLog { //操作日志的内容 String content(); //模块名 String moduleName(); //操作类型 String operateType() default ""; //操作编号 String code() default ""; }
步骤二、定义方法的前置增强
package cn.hmy.advicer; import java.lang.reflect.Method; import java.util.Date; import org.apache.struts2.ServletActionContext; import org.springframework.aop.MethodBeforeAdvice; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import cn.hmy.Util.OperateLog; import cn.hmy.beans.PsLogs; import cn.hmy.beans.PsUser; import cn.hmy.service.PsLogsService; public class LogBeforeAdvice implements MethodBeforeAdvice{ public void before(Method method, Object[] args, Object target) throws Throwable { System.out.println("日志记录开启"); OperateLog annotation=method.getAnnotation(OperateLog.class); if(annotation!=null){ ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml"); PsLogsService service = (PsLogsService)context.getBean("psLogsService"); //获取session中用户的信息 PsUser user=(PsUser)ServletActionContext.getRequest().getSession().getAttribute("loginuser"); PsLogs logs=new PsLogs(); logs.setOperatedatetime(new Date()); logs.setOperateInfo(annotation.content()); if(user==null){ String userCode = ServletActionContext.getRequest().getParameter("userCode"); String userPassWord= ServletActionContext.getRequest().getParameter("userPassWord"); logs.setUserCode(userCode); }else{ logs.setUserCode(user.getUserCode()); logs.setUserName(user.getUserName()); } service.save(logs); } } }
步骤三、在applicationContext.xml文件中配置
<!--aop log cfg 自动代理 --> <bean name="logBefore" class="cn.hmy.advicer.LogBeforeAdvice"></bean> <aop:config proxy-target-class="true"> <aop:pointcut expression="execution(* *..action.*.*(..))" id="pointcut"/> <aop:advisor advice-ref="logBefore" pointcut-ref="pointcut"/> </aop:config>
方案三、使用过滤器实现日志记录
三种方案实现日志记录功能
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。