实现记录业务日志和异常业务日志的操作 springboot( 三 )
10.获取缓存对象的beanimport org.springframework.context.annotation.Scope;import org.springframework.stereotype.Component;import org.springframework.web.context.WebApplicationContext; import java.io.Serializable;@Component@Scope(scopeName = WebApplicationContext.SCOPE_SESSION)public class LogObjectHolder implements Serializable{private Object object = null;public void set(Object obj) {this.object = obj;}public Object get() {return object;}public static LogObjectHolder me(){LogObjectHolder bean = SpringContextHolder.getBean(LogObjectHolder.class);return bean;} }
11.运行时异常的获取@ControllerAdvicepublic class GlobalExceptionHandler extends BasicMemberJSONController {private Logger log = LoggerFactory.getLogger(this.getClass());@ExceptionHandler(RuntimeException.class)@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)@ResponseBodypublic String notFount(RuntimeException e) {String userName = curManage().get("USERNAME").toString();LogManager.me().executeLog(LogTaskFactory.exceptionLog(userName, e));log.error("运行时异常:", e);return fail();}}
12.使用线程池创建操作日志import java.util.Date;public class LogFactory {/*** 创建操作日志*/public static DataRow createOperationLog(LogType logType, String userName, String bussinessName, String clazzName, String methodName, String msg, LogSucceed succeed) {DataRow operationLog = new DataRow();operationLog.put("log_type", logType.getMessage());operationLog.put("USER_NAME", userName);operationLog.put("log_name", bussinessName);operationLog.put("CLASS_NAME", clazzName);operationLog.put("METHOD", methodName);operationLog.put("CREATE_TIME", new Date());operationLog.put("SUCCEED", succeed.getMessage());if (msg.length() > 800) {msg = msg.substring(0, 800);operationLog.put("MESSAGE", msg);} else {operationLog.put("MESSAGE", msg);}return operationLog;}}
import java.util.TimerTask;import java.util.concurrent.ScheduledThreadPoolExecutor;import java.util.concurrent.TimeUnit;public class LogManager {//日志记录操作延时private final int OPERATE_DELAY_TIME = 10;//异步操作记录日志的线程池private ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(10);private LogManager() {}public static LogManager logManager = new LogManager();public static LogManager me() {return logManager;}public void executeLog(TimerTask task) {executor.schedule(task, OPERATE_DELAY_TIME, TimeUnit.MILLISECONDS);} }
public enum LogSucceed {SUCCESS("成功"),FAIL("失败");String message;LogSucceed(String message) {this.message = message;}public String getMessage() {return message;}public void setMessage(String message) {this.message = message;} }
import com.qihoinfo.dev.log.annotation.RedisDb;import com.qihoinfo.dev.log.util.ToolUtil;import org.anyline.entity.DataRow;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.context.annotation.DependsOn;import org.springframework.data.redis.core.StringRedisTemplate;import org.springframework.stereotype.Component; import java.util.TimerTask;@Component@DependsOn("springContextHolder")public class LogTaskFactory {private static Logger logger = LoggerFactory.getLogger(LogManager.class);private static StringRedisTemplate redisTemplate = RedisDb.getMapper(StringRedisTemplate.class);public static TimerTask bussinessLog(final String userName, final String bussinessName, final String clazzName, final String methodName, final String msg) {return new TimerTask() {@Overridepublic void run() {DataRow operationLog = LogFactory.createOperationLog(LogType.BUSSINESS, userName, bussinessName, clazzName, methodName, msg, LogSucceed.SUCCESS);try {redisTemplate.opsForList().rightPush("sys_operation_log", operationLog.getJson());} catch (Exception e) {logger.error("创建业务日志异常!", e);}}};}public static TimerTask exceptionLog(final String userName, final Exception exception) {return new TimerTask() {@Overridepublic void run() {String msg = ToolUtil.getExceptionMsg(exception);DataRow operationLog = LogFactory.createOperationLog(LogType.EXCEPTION, userName, "", null, null, msg, LogSucceed.FAIL);try {redisTemplate.opsForList().rightPush("sys_operation_log", operationLog.getJson());} catch (Exception e) {logger.error("创建异常日志异常!", e);}}};} }
public enum LogType {EXCEPTION("异常日志"),BUSSINESS("业务日志");String message;LogType(String message) {this.message = message;}public String getMessage() {return message;}public void setMessage(String message) {this.message = message;} }
13.将日志记录到redis数据库package com.qihoinfo.dev.log.annotation;import com.qihoinfo.dev.log.util.SpringContextHolder;import org.springframework.data.redis.core.StringRedisTemplate; public class RedisDb<T> {private Class<T> clazz;private StringRedisTemplate baseMapper;private RedisDb(Class clazz) {this.clazz = clazz;this.baseMapper = (StringRedisTemplate) SpringContextHolder.getBean(clazz);}public static <T> RedisDb<T> create(Class<T> clazz) {return new RedisDb<T>(clazz);}public StringRedisTemplate getMapper() {return this.baseMapper;}public static <T> T getMapper(Class<T> clazz) {return SpringContextHolder.getBean(clazz);}}
推荐阅读
- 欢乐斗地主怎么看记录 欢乐斗地主如何查看自己的游戏战绩
- 微信搜索记录该咋地才可以查找
- 美团聊天怎样彻底删除记录
- 收款记录删除了还能查到吗
- 收款记录添加备忘对方能看到吗
- 聊天记录删了怎么恢复聊天记录
- 26.30%全球第一 隆基宣布刷新HJT光伏电池记录
- 抖音怎么一键清空聊天记录
- 全民k歌怎么看谁听过的记录
- 退役军人走访记录怎么填写
