版权声明:第一次写文章,有什么需要补充的还望各位大神多多指教。 https://blog.csdn.net/mengxiangxingdong/article/details/82193781
目标:页面访问到指定的controller时,能够显示出访问的是哪个url 且能够点击就到指定的类,以及页面,且能够通过配置开启或者关闭,提升调试效率
目前实现能够点击定位到类,但是不能点击到页面,只能打印返回的页面
效果图:例如点击添加操作
思路:
以下是代码 需要引用aop jar包
详情代码可以查看 https://gitee.com/hugo110/springboot-test
package com.fanfan.core.aop;
import java.lang.reflect.Method;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang.ObjectUtils;
import org.apache.commons.lang.StringUtils;
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.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import com.fanfan.config.ConfigurationKit;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
/**
* 日志记录 打印需要的信息
*
* @author fengshuonan
* @date 2016年12月6日 下午8:48:30
*/
@Aspect
@Component
@Slf4j
public class LogExtAop {
@Pointcut(value = "@annotation(org.springframework.web.bind.annotation.RequestMapping)")
public void cutService() {
}
@Around("cutService()")
public Object recordSysLog(ProceedingJoinPoint point) throws Throwable {
// 检查是否开始了 输出配置打印参数配置
// HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
// 先执行业务
Object result = point.proceed();
// action 和 jsp 日志打印
boolean out = ConfigurationKit.logKit.getBoolean("log4j.out", false);
boolean outAction = ConfigurationKit.logKit.getBoolean("log4j.out.jsp", false);
if (out && outAction) {
try {
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
MethodSignature signature = (MethodSignature) point.getSignature();
LogExtModel logExtModel = new LogExtModel(signature.getMethod());
logExtModel.setMethodArgs(point.getArgs());
logExtModel.setResult(result);
logExtModel.setUrl(request.getRequestURL().toString()); // 获得方法进来的url
logExtModel.setIp(request.getRemoteAddr()); // 获得方法进来的ip
System.err.println(logExtModel.toString());
} catch (Exception e) {
log.error("日志记录出错!", e);
}
}
return result;
}
}
/**
*
* @ClassName LogExtModel
* @author <a href="[email protected]" target="_blank">于国帅</a>
* @date 2018年8月28日 下午4:27:19
*
*/
@Data
final class LogExtModel {
private String url; // 访问的url
private String ip; // 到指定的ip
private Object result; // 返回到前台的数据
private Object[] methodArgs; // 方法的参数
// ====================参考 StackTraceElement 对象
private String declaringClass;
private String methodName;
private String fileName;
private int lineNumber;
public LogExtModel(Method method) {
this.declaringClass = method.getDeclaringClass().getName();
this.methodName = method.getName();
this.fileName = method.getDeclaringClass().getSimpleName() + ".java";
this.lineNumber = 1;
}
// ====================参考 StackTraceElement 对象
public String getUrlMaping() { // 获得映射的地址
return declaringClass + "." + methodName + "(" + StringUtils.join(methodArgs, ", ") + ") "
+ (isNativeMethod() ? "(Native Method)"
: (fileName != null && lineNumber >= 0 ? "(" + fileName + ":" + lineNumber + ")"
: (fileName != null ? "(" + fileName + ")" : "(Unknown Source)")));
}
public String getUrl() {
return this.url + " "
+ (isNativeMethod() ? "(Native Method)"
: (fileName != null && lineNumber >= 0 ? "(" + fileName + ":" + lineNumber + ")"
: (fileName != null ? "(" + fileName + ")" : "(Unknown Source)")));
}
@Override
public String toString() {
// 先输出ip,url ,映射,返回值
String msg = "开发 debug 日志模式 ";
msg += "{ @ip===" + this.getIp() + "\r\n";
msg += " @请求url===" + this.getUrl() + "点击左侧 () 可快速定位到文件 \r\n";
msg += " @类.方法(参数值) 定位 ===" + this.getUrlMaping() + "\r\n";
msg += " @方法返回值result===" + this.getResult() + "\r\n";
if (this.result != null && this.result instanceof String) {
msg += " @跳转的页面===" + this.getView() + " eclipse 采用 ctrl+shift +r 选中()内的页面名字可以快捷打开 } ";
}
return msg;
}
private String getView() { // 检查返回值 如果符合一定的规则 那么补充对应的页面的后缀
String suffix = ConfigurationKit.logKit.getString("log4j.out.suffix", ".jsp");
String result = (String) ObjectUtils.defaultIfNull(this.result.toString(), "");
result = StringUtils.substringAfterLast(result, "/");
result = result == "" ? this.result.toString() : result;
return "(" + result + suffix + ")"; // 跳转的页面
}
public boolean isNativeMethod() {
return lineNumber == -2;
}
}
@Slf4j // log 注解
@RestController // 返回界面以字符串的形式
@RequestMapping(value = "/springmvc") // 路由映射
public class SpringmvcTest {
@RequestMapping(value = "/logExt", method = RequestMethod.GET)
public String toAdd() {
return "logExt";
}
}