关于springMVC的日志管理
主要是基于在spring aop特性.
1. 创建一个系统日志的操作类,类里面提供一个方法,可以向数据库或者表中写入:访问用户名,访问IP,操作时间,访问包名,具体函数名.
1 /** 2 * @Name SystemLogUtils 3 * @Descr 系统日志工具 4 * @author lne 5 */ 6 public class SystemLogUtils { 7 private ISystemLogService logService; 8 9 public void setLogService(ISystemLogService logService) { 10 this.logService = logService; 11 } 12 13 public void writeLog(JoinPoint joinPoint) throws Exception { 14 Object serviceObj = joinPoint.getTarget(); 15 16 if ((serviceObj instanceof ISystemLogService)) { 17 return; 18 } 19 @SuppressWarnings("rawtypes") 20 Class serviceClz = joinPoint.getTarget().getClass(); 21 22 String methodName = joinPoint.getSignature().getName(); 23 24 // 创建日志对象 25 SystemLog log = new SystemLog(); 26 // 封装日志属性 27 log.setOpUser(UserContextUtil.getUser()); 28 log.setOpTime(new Date()); 29 log.setOpIp(UserContextUtil.getRequest().getRemoteAddr()); 30 31 String function = serviceClz.getName(); 32 log.setMethod(function); 33 log.setParams(methodName); 34 35 // 保存日志 36 logService.save(log); 37 } 38 }
2. 在sping配置文件中加入系统日志的配置
1 <!-- 系统日志工具类 --> 2 <bean id="logUtils" class="cn.crmx.crm.util.SystemLogUtils"> 3 <property name="logService" ref="systemLogServiceImpl"></property> 4 </bean> 5 6 <!-- 切入点配置 --> 7 <aop:config> 8 <aop:pointcut expression="execution(* cn.crmx.crm.service..*.*(..))" id="logPointcut" /> 9 <aop:aspect ref="logUtils"> 10 <aop:after method="writeLog" pointcut-ref="logPointcut" /> 11 </aop:aspect> 12 </aop:config>
3.然后提供相关系统日志的其它层文件.
这种方法的好处在与,可以相信配置操作系统日志的权限.
第二种是纯基于框架配置,引入jar支持包,然后在固定位置,输出文件.
就到这.
一介书生,敲敲键盘而已。