关于springMVC的日志管理

时间:2021-02-22 12:09:13

主要是基于在spring aop特性.

1. 创建一个系统日志的操作类,类里面提供一个方法,可以向数据库或者表中写入:访问用户名,访问IP,操作时间,访问包名,具体函数名.

 /**
* @Name SystemLogUtils
* @Descr 系统日志工具
* @author lne
*/
public class SystemLogUtils {
private ISystemLogService logService; public void setLogService(ISystemLogService logService) {
this.logService = logService;
} public void writeLog(JoinPoint joinPoint) throws Exception {
Object serviceObj = joinPoint.getTarget(); if ((serviceObj instanceof ISystemLogService)) {
return;
}
@SuppressWarnings("rawtypes")
Class serviceClz = joinPoint.getTarget().getClass(); String methodName = joinPoint.getSignature().getName(); // 创建日志对象
SystemLog log = new SystemLog();
// 封装日志属性
log.setOpUser(UserContextUtil.getUser());
log.setOpTime(new Date());
log.setOpIp(UserContextUtil.getRequest().getRemoteAddr()); String function = serviceClz.getName();
log.setMethod(function);
log.setParams(methodName); // 保存日志
logService.save(log);
}
}

2. 在sping配置文件中加入系统日志的配置

     <!-- 系统日志工具类 -->
<bean id="logUtils" class="cn.crmx.crm.util.SystemLogUtils">
<property name="logService" ref="systemLogServiceImpl"></property>
</bean> <!-- 切入点配置 -->
<aop:config>
<aop:pointcut expression="execution(* cn.crmx.crm.service..*.*(..))" id="logPointcut" />
<aop:aspect ref="logUtils">
<aop:after method="writeLog" pointcut-ref="logPointcut" />
</aop:aspect>
</aop:config>

3.然后提供相关系统日志的其它层文件.

  这种方法的好处在与,可以相信配置操作系统日志的权限.

第二种是纯基于框架配置,引入jar支持包,然后在固定位置,输出文件.

就到这.