1、创建一个注解
/**
* 自定义操作日志注解
* @author Mr peng
*/
@Target(ElementType.METHOD) //注解放置的目标位置,METHOD是可注解在方法级别上
@Retention(RetentionPolicy.RUNTIME) //注解在哪个阶段执行
@Documented
public @interface AnnotationLog {
/**
* 操作内容
* @return
*/
String operText() default "";
}
2、添加一个mvc拦截器
/**
* 自定义操作日志拦截器配置类
*/
@Configuration
public class CustomLogConfig implements WebMvcConfigurer {
@Autowired
private CustomLogInterCeptor logInterCeptor;
@Override
public void addInterceptors(InterceptorRegistry registry) {
//添加拦截器,配置拦截地址
registry.addInterceptor(logInterCeptor).addPathPatterns("/**");
}
}
3、实现mvc拦截器,重写方法执行前后等方法
/**
* 自定义拦截器,拦截访问请求,保存请求日志信息
*/
@Slf4j
@Component
public class CustomLogInterCeptor implements HandlerInterceptor {
/**
* 重写preHandle方法,在controller调用前执行
*/
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
log.info("请求开始~~~~~url:"+request.getRequestURI());
return true;
}
/**
* 重写afterCompletion,在controller调用后,DispatcherServlet渲染了视图再执行
*/
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
//保存日志
SysLogUtil.saveLog(request,handler,ex,null);
log.info("请求结束~~~~~~~~~");
if (ex != null){
log.error("报错信息:"+ex);
}
}
}
4、日志管理工具类
/**
* 日志管理工具类
*/
public class SysLogUtil {
private static SysLogMapper sysLogMapper = CustomBeanUtil.getApplicationContext().getBean(SysLogMapper.class);
/**
* 保存日志
* @throws UnsupportedEncodingException
*/
public static boolean saveLog(HttpServletRequest request,Object handler,Exception ex,String tile) throws UnsupportedEncodingException{
// 将handler强转为HandlerMethod
HandlerMethod handlerMethod = (HandlerMethod) handler;
// 从方法处理器中获取出要调用的方法
Method method = handlerMethod.getMethod();
// 获取出方法上的Access注解
AnnotationLog annotationLog = method.getAnnotation(AnnotationLog.class);
if (annotationLog == null) {
// 如果注解为null, 说明不需要记录日志信息
return true;
}
if (annotationLog != null) {
// 如果自定义注解不为空, 则取出配置值
String operText = annotationLog.operText();
// 保存操作日志
SysLog log = new SysLog();
log.setLogType(ex == null?"info":"error");
String methodStr = request.getMethod();
String uri = request.getRequestURI();
String authType = request.getAuthType();
String string = request.getQueryString();
if (string != null){
string = URLDecoder.decode(string, "utf-8");
}
log.setLogText("{请求的URI为:"+uri+"; 请求方式为:"+methodStr+"; 请求参数为:"+string+"}");
log.setLogText(operText);
log.setUserId(SecurityContextUtils.getPartyId());
sysLogMapper.insert(log);
return true;
}
return false;
}
}
5、利用spring中的Aware创建获取bean的工具类
/**
* Bean获取工具类
*/
@Component
public class CustomBeanUtil implements ApplicationContextAware {
private static ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
if (CustomBeanUtil.applicationContext == null){
CustomBeanUtil.applicationContext = applicationContext;
}
}
//获取applicationContext
public static ApplicationContext getApplicationContext(){
return applicationContext;
}
//通过name获取Bean
public static Object getBean(String name){
return getApplicationContext().getBean(name);
}
//通过class获取Bean
public static <T> T getBean(Class<T> clazz){
return getApplicationContext().getBean(clazz);
}
//通过name以及clazz返回指定的Bean
public static <T> T getBean(String name,Class<T> clazz){
return getApplicationContext().getBean(name,clazz);
}
}