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
| package com.aprildawn.questionaire_survey.utils;
import eu.bitwalker.useragentutils.UserAgent; import jakarta.servlet.http.HttpServletRequest; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Pointcut; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.mongodb.core.MongoTemplate; import org.springframework.data.mongodb.core.query.BasicQuery; import org.springframework.stereotype.Component;
import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Arrays; import java.util.Date; import java.util.List;
@Component @Aspect public class MyLogsConfig {
@Autowired private MongoTemplate mongoTemplate;
@Autowired private HttpServletRequest request;
@Pointcut("execution(* com.aprildawn.questionaire_survey.controller.*.*(..))") public void logPoinCut(){}
@Around(value="logPoinCut()") public Object afterLog(ProceedingJoinPoint joinpoint) throws Throwable {
System.out.println("===================================================="); System.out.println("[AOP 环绕通知] 日志记录开始"); MyLogs myLogs = new MyLogs();
myLogs.setPackages(joinpoint.getTarget().getClass().getPackageName()); myLogs.setClassname(joinpoint.getTarget().getClass().getSimpleName()); myLogs.setMethod(joinpoint.getSignature().getName()); Object[] args = joinpoint.getArgs(); List list = Arrays.asList(args); if (list.get(0).toString()!= null&&list.get(0).toString()!="{}"){ myLogs.setReqparam(list.toString()); }else { myLogs.setReqparam("NULL"); } Logger logger = LoggerFactory.getLogger(joinpoint.getTarget().getClass()); myLogs.setLevel(getLogLevel(logger)); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); myLogs.setDate(sdf.format(new Date())); String userAgent = request.getHeader("User-Agent"); UserAgent agent = UserAgent.parseUserAgentString(userAgent); String BrowserName = agent.getBrowser().getName(); String SystemName = agent.getOperatingSystem().getName(); System.out.println("客户端系统信息:\n浏览器:"+BrowserName+"\n操作系统:"+SystemName);
String ip = request.getRemoteAddr(); int port = request.getRemotePort(); int port1 = request.getServerPort(); int port2 = request.getLocalPort();
myLogs.setIp(ip); myLogs.setBrowser(BrowserName); myLogs.setSystem(SystemName); myLogs.setRemoteport(String.valueOf(port)); myLogs.setServerport(String.valueOf(port1)); myLogs.setLocalport(String.valueOf(port2));
MyLogs a = mongoTemplate.save(myLogs);
System.out.println("[AOP 环绕通知] 日志记录完成!"); System.out.println("===================================================="); System.out.println("返回值:"+ a); BasicQuery query = new BasicQuery("{ level : 'INFO' }"); BasicQuery query1 = new BasicQuery("{ date : 'INFO' }"); List<MyLogs> result = mongoTemplate.find(query, MyLogs.class); System.out.println("MongoDB查询测试:"); for (MyLogs logs : result) { System.out.println(logs.toString()); }
System.out.println("====================================================");
return joinpoint.proceed(args); }
public String getLogLevel(Logger logger){ if (logger.isTraceEnabled()){ return "TRACE"; } else if (logger.isDebugEnabled()) { return "DEBUG"; } else if (logger.isInfoEnabled()) { return "INFO"; } else if (logger.isWarnEnabled()) { return "WARN"; } else if (logger.isErrorEnabled()) { return "ERROR"; }else { return "NULL"; } } }
|