写点什么

mysql 分表 spring 拦截器进行日志采集

  • 2021 年 11 月 11 日
  • 本文字数:5875 字

    阅读完需:约 19 分钟

public void setEnd_time(Date end_time) {


this.end_time = end_time;


}


}


import lombok.Data;


import java.util.Date;


/**


  • 项目名称:

  • 类 名 称:TbLoggingSelect

  • 类 描 述:TODO

  • 创建时间:2020/6/11 19:30

  • 创 建 人:heng


*/


@Data


public class TbLoggingSelect {


//id


private Integer id;//id


//类名


private String class_name;//类名


//方法名


private String method_name;//方法名


//参数


private String param;//参数


//访问的 url


private String url;//访问的 url


//操作人 ip


private String ip;//操作人 ip


//创建人 id


private String create_user_id;//创建人 id


//创建时间


private Date create_time;//创建时间


//执行开始时间


private Date state_time;//执行开始时间


//执行结束时间


private Date end_time;//执行结束时间


//耗时


private String long_time;//耗时


//请求方式 GET、POST...


private String method;//请求方式 GET、POST...


//操作模块描述


private String module_name;//操作模块描述 //操作模块描述


//返回值


private String return_value;//返回值


//当前操作的浏览器


private String browser;//当前操作的浏览器


//是否删除 0、未删除 1、删除


private Integer is_delete;//是否删除 0、未删除 1、删除


private String state;//操作模块描述


public Date getCreate_time() {


return create_time;


}


public void setCreate_time(Date create_time) {


this.create_time = create_time;


}


public Date getState_time() {


return state_time;


}


public void setState_time(Date state_time) {


this.state_time = state_time;


}


public Date getEnd_time() {


return end_time;


}


public void setEnd_time(Date end_time) {


this.end_time = end_time;


}


}


拦截器采集日志


id 是用 redis 生成的小伙伴可以用别的


新增语句这里就不贴出来了


以下操作查询的放一个表


其他放一张表


package com.web.common.intercept;


import cn.hutool.core.date.DateUtil;


import com.alibaba.fastjson.JSON;


import com.alibaba.fastjson.serializer.SerializerFeature;


import com.web.common.controller.BaseController;


import com.web.entity.TbLogging;


import com.web.entity.TbLoggingSelect;


import nl.bitwalker.useragentutils.UserAgent;


import org.apache.commons.beanutils.BeanUtils;


import org.apache.commons.lang3.StringUtils;


import org.slf4j.Logger;


import org.slf4j.LoggerFactory;


import org.springframework.beans.factory.annotation.Autowired;


import org.springframework.web.method.HandlerMethod;


import org.springframework.web.servlet.ModelAndView;


import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;


import redis.clients.jedis.Jedis;


import web.dao.hsdao.TbLoggingMapper;


import web.dao.hsdao.TbLoggingSelectMapper;


import web.util.JedisUtil;


import javax.servlet.http.HttpServletRequest;


import javax.servlet.http.HttpServletResponse;


import java.util.Date;


/**


  • @ClassName SginAop

  • @Description

  • @Author heng

  • @Date 2020/4/26 10:41

  • @Version 1.0


*/


public class LogInterceptor extends HandlerInterceptorAdapter {


private final String redisKey = "LOG:LOGGING_KEY";


private final static Logger LOGGER = LoggerFactory.getLogger(LogInterceptor.class);


//请求开始时间标识


private static final String LOGGER_SEND_TIME = "_send_time";


//请求日志实体标识


private static final String LOGGER_ENTITY = "_logger_entity";


@Autowired


private TbLoggingSelectMapper tbLoggingSelectMapper;


@Autowired


private TbLoggingMapper tbLoggingMapper;


public Long incr(String key) {


Jedis jedis =JedisUtil.getJedis();


try {


return jedis.incr(key);


} finally {


if (null != jedis) {


jedis.close();


}


}


}


@Override


public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {


if (handler.getClass().isAssignableFrom(HandlerMethod.class)) {


try {


TbLogging tbLogging = new TbLogging();


Long start_long_time = System.currentTimeMillis();


tbLogging.setState_time(DateUtil.date(start_long_time));


//获取 ip


tbLogging.setIp(getRemoteHost(request));


//获取控制器的名字


tbLogging.setClass_name(((HandlerMethod) handler).getBean().getClass().getName());


//获取方法名


tbLogging.setMethod_name(((HandlerMethod) handler).getMethod().getName());


//获取请求参数信息


String param = JSON.toJSONString(request.getParameterMap(),


SerializerFeature.DisableCircularReferenceDetect,


SerializerFeature.WriteMapNullValue);


tbLogging.setParam(param);


try {


tbLogging.setCreate_user_id(new BaseController().obtainLoginUserId(request));


}catch (Exception ex){


LOGGER.info("拿到当前登录人失败");


}


//请求方法


tbLogging.setMethod(request.getMethod());


//请求 url 到后端的 url


tbLogging.setUrl(request.getRequestURI());


//浏览器


//获取浏览器信息


String ua = request.getHeader("User-Agent");


//转成 UserAgent 对象


UserAgent userAgent = UserAgent.parseUserAgentString(ua);


tbLogging.setBrowser(userAgent.toString());


//获取返回值


//tbLogging.setReturn_value(response.getWriter().toString());


//设置请求开始时间


request.setAttribute(LOGGER_SEND_TIME, start_long_time);


//设置请求实体到 request 内,方便 afterCompletion 方法调用


request.setAttribute(LOGGER_ENTITY, tbLogging);


} catch (Exception e) {


LOGGER.info("日志添加失败");


}


}


return true;


}


@Override


public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {


}


@Override


public void afterCompletion(HttpServletRequest request, HttpServletResponse response,


【一线大厂Java面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义】
浏览器打开:qq.cn.hn/FTf 免费领取
复制代码


Object handler, Exception ex) throws Exception {


try {


String uri = request.getRequestURI();


String contextPath = request.getContextPath();


if (StringUtils.length(contextPath) > 0) {


contextPath = StringUtils.substring(uri, contextPath.length());


}


TbLogging tbLogging = (TbLogging) request.getAttribute(LOGGER_ENTITY);


//获取请求错误码


int status = response.getStatus();


//当前时间


long currentTime = System.currentTimeMillis();


//请求开始时间


long time = Long.valueOf(request.getAttribute(LOGGER_SEND_TIME).toString());


//获取本次请求日志实体


tbLogging.setState(status+"");


//设置请求时间差


tbLogging.setLong_time((currentTime - time)+"");


tbLogging.setEnd_time(DateUtil.date(currentTime));


tbLogging.setCreate_time(DateUtil.date(currentTime));


// Long longId = IdUtil.createSnowflake(1, 1).nextId();


Long longId = incr(redisKey);


tbLogging.setId(longId.intValue());


if(tbLogging.getClass_name().indexOf("TbLoggingController") == -1){


if (contextPath.indexOf("search_") == -1


&& !tbLogging.getMethod_name().startsWith("search")


&& !tbLogging.getMethod_name().startsWith("get")


&& !tbLogging.getMethod_name().startsWith("query")


&& !tbLogging.getMethod_name().startsWith("find")


&& !tbLogging.getMethod_name().startsWith("select")


&& !tbLogging.getMethod_name().equals("index")) {


//执行将日志写入数据库,可以根据实际需求进行保存


// sysLogRepo.save(sysLog);


tbLoggingMapper.insertTbLogging(tbLogging);


}else {


TbLoggingSelect select = new TbLoggingSelect();


//把 tbLogging 的值给 select


BeanUtils.copyProperties(select,tbLogging);


tbLoggingSelectMapper.insertTbLoggingSelect(select);


}


}


}catch (Exception e){


LOGGER.info("日志添加失败");


}


}


public int difference(Date nowDate, String decrypted){


return Math.abs((int)(nowDate.getTime()-Long.valueOf(decrypted))/1000);


}


/**


  • @Title: getRemoteHost

  • @Description: 获取 Ip 地址

  • @return: String

  • @version V1.0


*/


public String getRemoteHost(HttpServletRequest request) {


String ip = request.getHeader("x-forwarded-for");


if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {


ip = request.getHeader("Proxy-Client-IP");


}


if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {


ip = request.getHeader("WL-Proxy-Client-IP");


}


if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {


ip = request.getHeader("HTTP_CLIENT_IP");


}


if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {


ip = request.getHeader("HTTP_X_FORWARDED_FOR");


}


if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {


ip = request.getRemoteAddr();


}


return ip.equals("0:0:0:0:0:0:0:1") ? "127.0.0.1" : ip;


}


}


以下操作用于查询


mapper.xml


/**


  • 用于分表分页查询

  • @param map

  • @return


*/


List<TbLogging> pageListTbLoggingSelectByObj(Map<String,Object> map);


int pageListTbLoggingSelectByObjCount(Map<String,Object> map);


<select id="pageListTbLoggingSelectByObj" parameterType="map" resultMap="BaseResultMap2">


SELECT <include refid="Base_Column_List" />


FROM tb_logging_select


<where>


is_delete = 0


<if test ='null != create_time and create_time != ""'>


AND date_format(create_time,'%Y-%m-%d') = #{create_time}


</if>


<if test ='null != maxId and maxId != "" '>


AND id > #{maxId}


</if>


<if test ='null != minId and minId != ""'>


AND id < #{minId}


</if>


</where>


<if test ='null != maxId and maxId != "" '>


order by id asc


</if>


<if test ='null != minId and minId != ""'>


order by id desc


</if>


<if test ='(minId == null or minId == "") and (maxId == "" or maxId ==null) '>


order by id desc


</if>


LIMIT #{pageSize}


</select>


<select id="pageListTbLoggingSelectByObjCount" parameterType="map" resultType="int">


SELECT count(1)


FROM tb_logging_select


<where>


is_delete = 0


<if test ='null != create_time and create_time != ""'>


AND date_format(create_time,'%Y-%m-%d') = #{create_time}


</if>


</where>


</select>


/**


  • 用于分表分页查询

  • @param map

  • @return


*/


List<TbLogging> pageListTbLoggingByObj(Map<String,Object> map);


int pageListTbLoggingByObjCount(Map<String,Object> map);


<select id="pageListTbLoggingByObj" parameterType="map" resultMap="BaseResultMap2">


SELECT <include refid="Base_Column_List" />


FROM tb_logging


<where>


is_delete = 0


<if test ='null != create_time and create_time != "" '>


AND date_format(create_time,'%Y-%m-%d') = #{create_time}


</if>


<if test ='null != maxId and maxId != "" '>


AND id > #{maxId}


</if>


<if test ='null != minId and minId != ""'>


AND id < #{minId}


</if>


</where>


<if test ='null != maxId and maxId != "" '>


order by id asc


</if>


<if test ='null != minId and minId != ""'>


order by id desc


</if>


<if test ='minId == "" and maxId == ""'>


order by id desc


</if>


LIMIT #{pageSize}


</select>


<select id="pageListTbLoggingByObjCount" parameterType="map" resultType="int">


SELECT count(1)


FROM tb_logging


<where>


is_delete = 0


<if test ='null != create_time and create_time != "" '>


AND date_format(create_time,'%Y-%m-%d') = #{create_time}


</if>


</where>


</select>


service


package web.service.logging;


import com.web.common.util.PropertyValueChangeUtil;


import com.web.common.util.web.BeanRefUtil;


import com.web.entity.TbLogging;


import org.springframework.beans.factory.annotation.Autowired;


import org.springframework.stereotype.Service;


import web.dao.hsdao.TbLoggingMapper;


import web.dao.hsdao.TbLoggingSelectMapper;


import web.util.ResultUtils;


import java.util.*;


/**


  • 项目名称:

  • 类 名 称:TbLoggingService

  • 类 描 述:TODO

  • 创建时间:2020/6/12 17:25

  • 创 建 人:heng


*/


@Service


public class TbLoggingService {


@Autowired


private TbLoggingMapper tbLoggingMapper;


@Autowired


private TbLoggingSelectMapper tbLoggingSelectMapper;


public ResultUtils findLoging(Map<String,Object> map){


Integer pageSize = Integer.valueOf( map.get("pageSize").toString());


List<TbLogging> tbLoggings = tbLoggingMapper.pageListTbLoggingByObj(map);


List<TbLogging> tbLoggingSelects = tbLoggingSelectMapper.pageListTbLoggingSelectByObj(map);


int count1 = tbLoggingMapper.pageListTbLoggingByObjCount(map);


int count2 = tbLoggingSelectMapper.pageListTbLoggingSelectByObjCount(map);


tbLoggings.addAll(tbLoggingSelects);


List<TbLogging> list = new ArrayList<>();


if (map.get("maxId") != null){


Collections.sort(tbLoggings, new Comparator<TbLogging>() {


@Override


public int compare(TbLogging o1, TbLogging o2) {


if (o1.getId()> o2.getId()){


return 1;


}


if(o1.getId()< o2.getId()){


return -1;


}


return 0;


}


});


if (tbLoggings.size() >= pageSize){


list = tbLoggings.subList(0,pageSize);


}else {


list = tbLoggings;


}


Collections.sort(list, new Comparator<TbLogging>() {


@Override


public int compare(TbLogging o1, TbLogging o2) {


if (o1.getId()< o2.getId()){


return 1;


}


if(o1.getId()> o2.getId()){


return -1;


}


return 0;


}


});


}else{


Collections.sort(tbLoggings, new Comparator<TbLogging>() {


@Override


public int compare(TbLogging o1, TbLogging o2) {


if (o1.getId()< o2.getId()){


return 1;


}


if(o1.getId()> o2.getId()){


return -1;


}


return 0;


}


});


if (tbLoggings.size() >= pageSize){


list = tbLoggings.subList(0,pageSize);


}else {


list = tbLoggings;

评论

发布
暂无评论
mysql分表spring拦截器进行日志采集