Browse Source

fix:调整全局日志打印url

tags/freeze
白凤吉 3 months ago
parent
commit
3674621d7d
  1. 16
      src/main/java/com/iflytop/gd/common/handler/GlobalExceptionHandler.java
  2. 2
      src/main/java/com/iflytop/gd/infrastructure/drivers/WebSocketCommandBusImpl.java

16
src/main/java/com/iflytop/gd/common/handler/GlobalExceptionHandler.java

@ -21,13 +21,13 @@ public class GlobalExceptionHandler {
@ExceptionHandler(HttpRequestMethodNotSupportedException.class) @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
public Result<?> handleMethodNotSupported(HttpRequestMethodNotSupportedException ex, HttpServletRequest request) { public Result<?> handleMethodNotSupported(HttpRequestMethodNotSupportedException ex, HttpServletRequest request) {
String method = request.getMethod(); String method = request.getMethod();
String uri = request.getRequestURI();
String url = request.getRequestURL().toString();
String allowed = ex.getSupportedHttpMethods() != null String allowed = ex.getSupportedHttpMethods() != null
? ex.getSupportedHttpMethods().toString() ? ex.getSupportedHttpMethods().toString()
: "[]"; : "[]";
String msg = String.format( String msg = String.format(
"请求方法 '%s' 不支持 (URL: %s)。支持的方法有:%s", "请求方法 '%s' 不支持 (URL: %s)。支持的方法有:%s",
method, uri, allowed);
method, url, allowed);
log.error(msg, ex); log.error(msg, ex);
return Result.failed(ResultCode.METHOD_NOT_ALLOWED.getCode(), msg); return Result.failed(ResultCode.METHOD_NOT_ALLOWED.getCode(), msg);
} }
@ -64,12 +64,18 @@ public class GlobalExceptionHandler {
} }
@ExceptionHandler(Exception.class) @ExceptionHandler(Exception.class)
public Result<?> handleException(Exception ex) {
public Result<?> handleException(Exception ex, HttpServletRequest request) {
// 获取请求方法和请求路径
String method = request.getMethod();
String url = request.getRequestURL().toString();
if (ex instanceof AppException ae) { if (ex instanceof AppException ae) {
log.warn("AppException:", ae);
log.warn("AppException at {} {}:", method, url, ae);
return Result.failed(ae.getResultCode()); return Result.failed(ae.getResultCode());
} }
log.error("Unhandled exception:", ex);
// 在日志中打印出请求地址
log.error("Unhandled exception at {} {}:", method, url, ex);
return Result.failed(ResultCode.SYSTEM_ERROR.getCode(), ex.getMessage()); return Result.failed(ResultCode.SYSTEM_ERROR.getCode(), ex.getMessage());
} }

2
src/main/java/com/iflytop/gd/infrastructure/drivers/WebSocketCommandBusImpl.java

@ -53,7 +53,7 @@ public class WebSocketCommandBusImpl implements CommandBus {
try { try {
this.session = container.connectToServer(this, endpointURI); this.session = container.connectToServer(this, endpointURI);
} catch (Exception e) { } catch (Exception e) {
log.error(e.getMessage(), e);
// log.error(e.getMessage(), e);
} }
} }

Loading…
Cancel
Save