系统操作日志作用:主要是为了记录登录到系统中的人什么时间对什么模块的某条数据进行了操作。方便追踪操作责任。
本文讲的是基于log4j日志打印的,来生成系统操作日志。其实就是每次执行一个操作的时候调用了一个公共方法,将“登录人”、“操作时间”、“操作的模块及方法”保存到了数据库,用一张表来存储起来。
实现:
每个control继承BaseController.java
package com.kentra.controller.base; import java.util.Date; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.ServletRequestAttributes; import org.springframework.web.servlet.ModelAndView; import com.kentra.entity.Page; import com.kentra.entity.system.Log; import com.kentra.service.system.log.LogManager; import com.kentra.service.system.log.impl.LogService; import com.kentra.util.Const; import com.kentra.util.Logger; import com.kentra.util.PageData; import com.kentra.util.PropertyUtil; import com.kentra.util.UuidUtil; /** * @author KENTRASOFT * 修改时间:2015、12、11 */ public class BaseController { protected Logger logger = Logger.getLogger(this.getClass()); private static final long serialVersionUID = 6357869213649815390L; private static Log log=null; private static LogService logService = null; /** new PageData对象 * @return */ public PageData getPageData(){ return new PageData(this.getRequest()); } /**得到ModelAndView * @return */ public ModelAndView getModelAndView(){ return new ModelAndView().addObject("systemName", PropertyUtil.readValue(Const.sysName)); } /**得到request对象 * @return */ public HttpServletRequest getRequest() { HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest(); return request; } /**得到32位的uuid * @return */ public String get32UUID(){ return UuidUtil.get32UUID(); } /**得到分页列表的信息 * @return */ public Page getPage(){ return new Page(); } public void logBefore(Logger logger, String busId,String addUser,String remark) throws Exception{ if(logService==null)logService = (LogService)Const.webAppContext.getBean("logService"); log = log == null ? new Log() : log; log.setId(UuidUtil.get32UUID()); log.setBusId(busId); log.setAddUser(addUser); log.setAddTime(new Date()); log.setRemark(remark); logService.save(log); } }
②每个方法中调用logBefore方法。
/**保存 * @param * @throws Exception */ @RequestMapping(value="/save") @ResponseBody public ResultUtil save(Supervision supervision,String fileGrids) throws Exception{ String busId = this.get32UUID(); if(!Jurisdiction.buttonJurisdiction(this.getPageData().getString("key"))){return null;} //校验权限 supervision.setId(busId); supervision.setCreatTime(DateUtil.getTime()); supervisionService.save(supervision); logger.info("-----------日志打印------------"); logBefore(logger, busId, Jurisdiction.getUsername(), "督办单-新增"); return ResultUtil.success("添加成功!", "supervision", "", "closeCurrent"); }
这样就生成了操作日志。