Spring AOP实现后台管理系统日志管理
设计原则和思路:
- 元注解方式结合AOP,灵活记录操作日志
- 能够记录详细错误日志为运维提供支持
- 日志记录尽可能减少性能影响
1.定义日志记录元注解
package com.myron.ims.annotation;import java.lang.annotation.*;/** * 自定义注解 拦截Controller * * @author lin.r.x * */@Target({ ElementType.PARAMETER, ElementType.METHOD })@Retention(RetentionPolicy.RUNTIME)public @interface SystemControllerLog { /** * 描述业务操作 例:Xxx管理-执行Xxx操作 * @return */ String description() default "";}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
2.定义用于记录日志的实体类
package com.myron.ims.bean;import java.io.Serializable;import com.myron.common.util.StringUtils;import com.myron.common.util.UuidUtils;import com.fasterxml.jackson.annotation.JsonFormat;import java.util.Date;import java.util.Map;/** * 日志类-记录用户操作行为 * @author lin.r.x * */public class Log implements Serializable{ private static final long serialVersionUID = 1L; private String logId; private String type; private String title; private String remoteAddr; private String requestUri; private String method; private String params; private String exception; @JsonFormat(pattern="yyyy-MM-dd HH:mm:ss", timezone = "GMT+8") private Date operateDate; private String timeout; private String userId; public String getLogId() { return StringUtils.isBlank(logId) ? logId : logId.trim(); } public void setLogId(String logId) { this.logId = logId; } public String getType() { return StringUtils.isBlank(type) ? type : type.trim(); } public void setType(String type) { this.type = type; } public String getTitle() { return StringUtils.isBlank(title) ? title : title.trim(); } public void setTitle(String title) { this.title = title; } public String getRemoteAddr() { return StringUtils.isBlank(remoteAddr) ? remoteAddr : remoteAddr.trim(); } public void setRemoteAddr(String remoteAddr) { this.remoteAddr = remoteAddr; } public String getRequestUri() { return StringUtils.isBlank(requestUri) ? requestUri : requestUri.trim(); } public void setRequestUri(String requestUri) { this.requestUri = requestUri; } public String getMethod() { return StringUtils.isBlank(method) ? method : method.trim(); } public void setMethod(String method) { this.method = method; } public String getParams() { return StringUtils.isBlank(params) ? params : params.trim(); } public void setParams(String params) { this.params = params; } /** * 设置请求参数 * @param paramMap */ public void setMapToParams(Map<String, String[]> paramMap) { if (paramMap == null){ return; } StringBuilder params = new StringBuilder(); for (Map.Entry<String, String[]> param : ((Map<String, String[]>)paramMap).entrySet()){ params.append(("".equals(params.toString()) ? "" : "&") + param.getKey() + "="); String paramValue = (param.getValue() != null && param.getValue().length > 0 ? param.getValue()[0] : ""); params.append(StringUtils.abbr(StringUtils.endsWithIgnoreCase(param.getKey(), "password") ? "" : paramValue, 100)); } this.params = params.toString(); } public String getException() { return StringUtils.isBlank(exception) ? exception : exception.trim(); } public void setException(String exception) { this.exception = exception; } public Date getOperateDate() { return operateDate; } public void setOperateDate(Date operateDate) { this.operateDate = operateDate; } public String getTimeout() { return StringUtils.isBlank(timeout) ? timeout : timeout.trim(); } public void setTimeout(String timeout) { this.timeout = timeout; } public String getUserId() { return StringUtils.isBlank(userId) ? userId : userId.trim(); } public void setUserId(String userId) { this.userId = userId; }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
3.定义日志AOP切面类
package com.myron.ims.aop;import java.lang.reflect.Method;import java.text.SimpleDateFormat;import java.util.Date;import java.util.Map;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpSession;import org.aspectj.lang.JoinPoint;import org.aspectj.lang.annotation.After;import org.aspectj.lang.annotation.AfterThrowing;import org.aspectj.lang.annotation.Aspect;import org.aspectj.lang.annotation.Before;import org.aspectj.lang.annotation.Pointcut;import org.aspectj.lang.reflect.MethodSignature;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.core.NamedThreadLocal;import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;import org.springframework.stereotype.Component;import com.myron.common.util.DateUtils;import com.myron.common.util.UuidUtils;import com.myron.ims.annotation.SystemControllerLog;import com.myron.ims.annotation.SystemServiceLog;import com.myron.ims.bean.Log;import com.myron.ims.bean.User;import com.myron.ims.service.LogService;/** * 系统日志切面类 * @author lin.r.x * */@Aspect@Componentpublic class SystemLogAspect { private static final Logger logger = LoggerFactory.getLogger(SystemLogAspect. class); private static final ThreadLocal<Date> beginTimeThreadLocal = new NamedThreadLocal<Date>("ThreadLocal beginTime"); private static final ThreadLocal<Log> logThreadLocal = new NamedThreadLocal<Log>("ThreadLocal log"); private static final ThreadLocal<User> currentUser=new NamedThreadLocal<>("ThreadLocal user"); @Autowired(required=false) private HttpServletRequest request; @Autowired private ThreadPoolTaskExecutor threadPoolTaskExecutor; @Autowired private LogService logService; /** * Controller层切点 注解拦截 */ @Pointcut("@annotation(com.myron.ims.annotation.SystemControllerLog)") public void controllerAspect(){} /** * 前置通知 用于拦截Controller层记录用户的操作的开始时间 * @param joinPoint 切点 * @throws InterruptedException */ @Before("controllerAspect()") public void doBefore(JoinPoint joinPoint) throws InterruptedException{ Date beginTime=new Date(); beginTimeThreadLocal.set(beginTime); if (logger.isDebugEnabled()){ logger.debug("开始计时: {} URI: {}", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS") .format(beginTime), request.getRequestURI()); } HttpSession session = request.getSession(); User user = (User) session.getAttribute("ims_user"); currentUser.set(user); } /** * 后置通知 用于拦截Controller层记录用户的操作 * @param joinPoint 切点 */ @SuppressWarnings("unchecked") @After("controllerAspect()") public void doAfter(JoinPoint joinPoint) { User user = currentUser.get(); if(user !=null){ String title=""; String type="info"; String remoteAddr=request.getRemoteAddr(); String requestUri=request.getRequestURI(); String method=request.getMethod(); Map<String,String[]> params=request.getParameterMap(); try { title=getControllerMethodDescription2(joinPoint); } catch (Exception e) { e.printStackTrace(); } long beginTime = beginTimeThreadLocal.get().getTime(); long endTime = System.currentTimeMillis(); if (logger.isDebugEnabled()){ logger.debug("计时结束:{} URI: {} 耗时: {} 最大内存: {}m 已分配内存: {}m 已分配内存中的剩余空间: {}m 最大可用内存: {}m", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(endTime), request.getRequestURI(), DateUtils.formatDateTime(endTime - beginTime), Runtime.getRuntime().maxMemory()/1024/1024, Runtime.getRuntime().totalMemory()/1024/1024, Runtime.getRuntime().freeMemory()/1024/1024, (Runtime.getRuntime().maxMemory()-Runtime.getRuntime().totalMemory()+Runtime.getRuntime().freeMemory())/1024/1024); } Log log=new Log(); log.setLogId(UuidUtils.creatUUID()); log.setTitle(title); log.setType(type); log.setRemoteAddr(remoteAddr); log.setRequestUri(requestUri); log.setMethod(method); log.setMapToParams(params); log.setUserId(user.getId()); Date operateDate=beginTimeThreadLocal.get(); log.setOperateDate(operateDate); log.setTimeout(DateUtils.formatDateTime(endTime - beginTime)); threadPoolTaskExecutor.execute(new SaveLogThread(log, logService)); logThreadLocal.set(log); } } /** * 异常通知 记录操作报错日志 * @param joinPoint * @param e */ @AfterThrowing(pointcut = "controllerAspect()", throwing = "e") public void doAfterThrowing(JoinPoint joinPoint, Throwable e) { Log log = logThreadLocal.get(); log.setType("error"); log.setException(e.toString()); new UpdateLogThread(log, logService).start(); } /** * 获取注解中对方法的描述信息 用于service层注解 * @param joinPoint切点 * @return discription */ public static String getServiceMthodDescription2(JoinPoint joinPoint) { MethodSignature signature = (MethodSignature) joinPoint.getSignature(); Method method = signature.getMethod(); SystemServiceLog serviceLog = method .getAnnotation(SystemServiceLog.class); String discription = serviceLog.description(); return discription; } /** * 获取注解中对方法的描述信息 用于Controller层注解 * * @param joinPoint 切点 * @return discription */ public static String getControllerMethodDescription2(JoinPoint joinPoint) { MethodSignature signature = (MethodSignature) joinPoint.getSignature(); Method method = signature.getMethod(); SystemControllerLog controllerLog = method .getAnnotation(SystemControllerLog.class); String discription = controllerLog.description(); return discription; } /** * 保存日志线程 */ private static class SaveLogThread implements Runnable { private Log log; private LogService logService; public SaveLogThread(Log log, LogService logService) { this.log = log; this.logService = logService; } @Override public void run() { logService.createLog(log); } } /** * 日志更新线程 */ private static class UpdateLogThread extends Thread { private Log log; private LogService logService; public UpdateLogThread(Log log, LogService logService) { super(UpdateLogThread.class.getSimpleName()); this.log = log; this.logService = logService; } @Override public void run() { this.logService.updateLog(log); } }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 170
- 171
- 172
- 173
- 174
- 175
- 176
- 177
- 178
- 179
- 180
- 181
- 182
- 183
- 184
- 185
- 186
- 187
- 188
- 189
- 190
- 191
- 192
- 193
- 194
- 195
- 196
- 197
- 198
- 199
- 200
- 201
- 202
- 203
- 204
- 205
- 206
- 207
- 208
- 209
- 210
- 211
- 212
- 213
- 214
- 215
- 216
- 217
- 218
- 219
- 220
- 221
- 222
- 223
- 224
- 225
- 226
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 170
- 171
- 172
- 173
- 174
- 175
- 176
- 177
- 178
- 179
- 180
- 181
- 182
- 183
- 184
- 185
- 186
- 187
- 188
- 189
- 190
- 191
- 192
- 193
- 194
- 195
- 196
- 197
- 198
- 199
- 200
- 201
- 202
- 203
- 204
- 205
- 206
- 207
- 208
- 209
- 210
- 211
- 212
- 213
- 214
- 215
- 216
- 217
- 218
- 219
- 220
- 221
- 222
- 223
- 224
- 225
- 226
4.spring 配置扫描切面,开启@AspectJ注解的支持
<aop:aspectj-autoproxy/> <context:component-scan base-package="com.myron.ims.aop" /> <context:component-scan base-package="com.myron.ims.service"/> <bean id="taskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor"> <property name="corePoolSize" value="5" /> <property name="maxPoolSize" value="10" /> <property name="WaitForTasksToCompleteOnShutdown" value="true" /> </bean>
5.使用范例LoginController方法中添加日志注解
/** *系统登入 */ @RequestMapping("/login.do") @SystemControllerLog(description="登入系统") @ResponseBody public Map<String, Object> login(String username, String password, Boolean rememberMe, HttpServletRequest req){ } /** * 安全退出登入 * @return */ @SystemControllerLog(description="安全退出系统") @RequestMapping("logout.do") public String logout(){ Subject subject=SecurityUtils.getSubject(); if(subject.isAuthenticated()){ subject.logout(); } return "/login.jsp"; }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
6.运行效果