实现记录业务日志和异常业务日志的操作 springboot
springboot,实现记录业务日志和异常业务日志的操作,博智网带你了解详细信息 。
日志记录到redis展现形式





3.基于@Aspect注解,实现日志扫描,并且记录日志import org.aspectj.lang.ProceedingJoinPoint;import org.aspectj.lang.Signature;import org.aspectj.lang.annotation.Around;import org.aspectj.lang.annotation.Aspect;import org.aspectj.lang.annotation.Pointcut;import org.aspectj.lang.reflect.MethodSignature;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.stereotype.Component; import java.lang.reflect.Method;import java.util.Map; /** * 日志记录 * */@Aspect@Componentpublic class LogAop {private Logger log = LoggerFactory.getLogger(this.getClass());@Pointcut(value = "https://www.yf-zs.com/redian/@annotation(com.stylefeng.guns.core.common.annotion.BussinessLog)")public void cutService() {}@Around("cutService()")public Object recordSysLog(ProceedingJoinPoint point) throws Throwable {//先执行业务Object result = point.proceed();try {handle(point);} catch (Exception e) {log.error("日志记录出错!", e);}return result;}private void handle(ProceedingJoinPoint point) throws Exception {//获取拦截的方法名Signature sig = point.getSignature();MethodSignature msig = null;if (!(sig instanceof MethodSignature)) {throw new IllegalArgumentException("该注解只能用于方法");}msig = (MethodSignature) sig;Object target = point.getTarget();Method currentMethod = target.getClass().getMethod(msig.getName(), msig.getParameterTypes());String methodName = currentMethod.getName();//如果当前用户未登录,不做日志ShiroUser user = ShiroKit.getUser();if (null == user) {return;}//获取拦截方法的参数String className = point.getTarget().getClass().getName();Object[] params = point.getArgs();//获取操作名称BussinessLog annotation = currentMethod.getAnnotation(BussinessLog.class);String bussinessName = annotation.value();String key = annotation.key();Class dictClass = annotation.dict();StringBuilder sb = new StringBuilder();for (Object param : params) {sb.append(param);sb.append(" & ");}//如果涉及到修改,比对变化String msg;if (bussinessName.contains("修改") || bussinessName.contains("编辑")) {Object obj1 = LogObjectHolder.me().get();Map<String, String> obj2 = HttpContext.getRequestParameters();msg = Contrast.contrastObj(dictClass, key, obj1, obj2);} else {Map<String, String> parameters = HttpContext.getRequestParameters();AbstractDictMap dictMap = (AbstractDictMap) dictClass.newInstance();msg = Contrast.parseMutiKey(dictMap, key, parameters);}log.info("[记录日志][RESULT:{}]",user.getId()+bussinessName+className+methodName+msg.toString());LogManager.me().executeLog(LogTaskFactory.bussinessLog(user.getId(), bussinessName, className, methodName, msg));}}
4.比较两个对象的工具类import java.beans.PropertyDescriptor;import java.lang.reflect.Field;import java.lang.reflect.Method;import java.util.Date;import java.util.Map; /** * 对比两个对象的变化的工具类 * * @author ... * @Date 2017/3/31 10:36 */public class Contrast {//记录每个修改字段的分隔符public static final String separator = ";;;";/*** 比较两个对象,并返回不一致的信息** @author ...* @Date 2017/5/9 19:34*/public static String contrastObj(Object pojo1, Object pojo2) {String str = "";try {Class clazz = pojo1.getClass();Field[] fields = pojo1.getClass().getDeclaredFields();int i = 1;for (Field field : fields) {if ("serialVersionUID".equals(field.getName())) {continue;}PropertyDescriptor pd = new PropertyDescriptor(field.getName(), clazz);Method getMethod = pd.getReadMethod();Object o1 = getMethod.invoke(pojo1);Object o2 = getMethod.invoke(pojo2);if (o1 == null || o2 == null) {continue;}if (o1 instanceof Date) {o1 = DateUtil.getDay((Date) o1);}if (!o1.toString().equals(o2.toString())) {if (i != 1) {str += separator;}str += "字段名称" + field.getName() + ",旧值:" + o1 + ",新值:" + o2;i++;}}} catch (Exception e) {e.printStackTrace();}return str;}/*** 比较两个对象pojo1和pojo2,并输出不一致信息** @author ...* @Date 2017/5/9 19:34*/public static String contrastObj(Class dictClass, String key, Object pojo1, Map<String, String> pojo2) throws IllegalAccessException, InstantiationException {AbstractDictMap dictMap = (AbstractDictMap) dictClass.newInstance();String str = parseMutiKey(dictMap, key, pojo2) + separator;try {Class clazz = pojo1.getClass();Field[] fields = pojo1.getClass().getDeclaredFields();int i = 1;for (Field field : fields) {if ("serialVersionUID".equals(field.getName())) {continue;}PropertyDescriptor pd = new PropertyDescriptor(field.getName(), clazz);Method getMethod = pd.getReadMethod();Object o1 = getMethod.invoke(pojo1);Object o2 = pojo2.get(StrKit.firstCharToLowerCase(getMethod.getName().substring(3)));if (o1 == null || o2 == null) {continue;}if (o1 instanceof Date) {o1 = DateUtil.getDay((Date) o1);} else if (o1 instanceof Integer) {o2 = Integer.parseInt(o2.toString());}if (!o1.toString().equals(o2.toString())) {if (i != 1) {str += separator;}String fieldName = dictMap.get(field.getName());String fieldWarpperMethodName = dictMap.getFieldWarpperMethodName(field.getName());if (fieldWarpperMethodName != null) {Object o1Warpper = DictFieldWarpperFactory.createFieldWarpper(o1, fieldWarpperMethodName);Object o2Warpper = DictFieldWarpperFactory.createFieldWarpper(o2, fieldWarpperMethodName);str += "字段名称:" + fieldName + ",旧值:" + o1Warpper + ",新值:" + o2Warpper;} else {str += "字段名称:" + fieldName + ",旧值:" + o1 + ",新值:" + o2;}i++;}}} catch (Exception e) {e.printStackTrace();}return str;}/*** 比较两个对象pojo1和pojo2,并输出不一致信息** @author ...* @Date 2017/5/9 19:34*/public static String contrastObjByName(Class dictClass, String key, Object pojo1, Map<String, String> pojo2) throws IllegalAccessException, InstantiationException {AbstractDictMap dictMap = (AbstractDictMap) dictClass.newInstance();String str = parseMutiKey(dictMap, key, pojo2) + separator;try {Class clazz = pojo1.getClass();Field[] fields = pojo1.getClass().getDeclaredFields();int i = 1;for (Field field : fields) {if ("serialVersionUID".equals(field.getName())) {continue;}String prefix = "get";int prefixLength = 3;if (field.getType().getName().equals("java.lang.Boolean")) {prefix = "is";prefixLength = 2;}Method getMethod = null;try {getMethod = clazz.getDeclaredMethod(prefix + StrKit.firstCharToUpperCase(field.getName()));} catch (NoSuchMethodException e) {System.err.println("this className:" + clazz.getName() + " is not methodName: " + e.getMessage());continue;}Object o1 = getMethod.invoke(pojo1);Object o2 = pojo2.get(StrKit.firstCharToLowerCase(getMethod.getName().substring(prefixLength)));if (o1 == null || o2 == null) {continue;}if (o1 instanceof Date) {o1 = DateUtil.getDay((Date) o1);} else if (o1 instanceof Integer) {o2 = Integer.parseInt(o2.toString());}if (!o1.toString().equals(o2.toString())) {if (i != 1) {str += separator;}String fieldName = dictMap.get(field.getName());String fieldWarpperMethodName = dictMap.getFieldWarpperMethodName(field.getName());if (fieldWarpperMethodName != null) {Object o1Warpper = DictFieldWarpperFactory.createFieldWarpper(o1, fieldWarpperMethodName);Object o2Warpper = DictFieldWarpperFactory.createFieldWarpper(o2, fieldWarpperMethodName);str += "字段名称:" + fieldName + ",旧值:" + o1Warpper + ",新值:" + o2Warpper;} else {str += "字段名称:" + fieldName + ",旧值:" + o1 + ",新值:" + o2;}i++;}}} catch (Exception e) {e.printStackTrace();}return str;}/*** 解析多个key(逗号隔开的)** @author ...* @Date 2017/5/16 22:19*/public static String parseMutiKey(AbstractDictMap dictMap, String key, Map<String, String> requests) {StringBuilder sb = new StringBuilder();if (key.indexOf(",") != -1) {String[] keys = key.split(",");for (String item : keys) {String fieldWarpperMethodName = dictMap.getFieldWarpperMethodName(item);String value = https://www.yf-zs.com/redian/requests.get(item);if (fieldWarpperMethodName != null) {Object valueWarpper = DictFieldWarpperFactory.createFieldWarpper(value, fieldWarpperMethodName);sb.append(dictMap.get(item) +"=" + valueWarpper + ",");} else {sb.append(dictMap.get(item) + "=" + value + ",");}}return StrKit.removeSuffix(sb.toString(), ",");} else {String fieldWarpperMethodName = dictMap.getFieldWarpperMethodName(key);String value = https://www.yf-zs.com/redian/requests.get(key);if (fieldWarpperMethodName != null) {Object valueWarpper = DictFieldWarpperFactory.createFieldWarpper(value, fieldWarpperMethodName);sb.append(dictMap.get(key) +"=" + valueWarpper);} else {sb.append(dictMap.get(key) + "=" + value);}return sb.toString();}} }
推荐阅读
- 欢乐斗地主怎么看记录 欢乐斗地主如何查看自己的游戏战绩
- 微信搜索记录该咋地才可以查找
- 美团聊天怎样彻底删除记录
- 收款记录删除了还能查到吗
- 收款记录添加备忘对方能看到吗
- 聊天记录删了怎么恢复聊天记录
- 26.30%全球第一 隆基宣布刷新HJT光伏电池记录
- 抖音怎么一键清空聊天记录
- 全民k歌怎么看谁听过的记录
- 退役军人走访记录怎么填写
