Unverified Commit 39638224 by Jason Song Committed by GitHub

Merge pull request #1229 from nianjiang/nian1

更改非法请求的log级别从ERROR到Warn (2)
parents a204560b 4b0f72bd
...@@ -8,6 +8,7 @@ import com.ctrip.framework.apollo.tracer.Tracer; ...@@ -8,6 +8,7 @@ import com.ctrip.framework.apollo.tracer.Tracer;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.slf4j.event.Level;
import org.springframework.http.HttpHeaders; import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
...@@ -49,7 +50,7 @@ public class GlobalDefaultExceptionHandler { ...@@ -49,7 +50,7 @@ public class GlobalDefaultExceptionHandler {
@ExceptionHandler({HttpRequestMethodNotSupportedException.class, HttpMediaTypeException.class}) @ExceptionHandler({HttpRequestMethodNotSupportedException.class, HttpMediaTypeException.class})
public ResponseEntity<Map<String, Object>> badRequest(HttpServletRequest request, public ResponseEntity<Map<String, Object>> badRequest(HttpServletRequest request,
ServletException ex) { ServletException ex) {
return handleError(request, BAD_REQUEST, ex); return handleError(request, BAD_REQUEST, ex, Level.WARN);
} }
@ExceptionHandler(HttpStatusCodeException.class) @ExceptionHandler(HttpStatusCodeException.class)
...@@ -67,23 +68,19 @@ public class GlobalDefaultExceptionHandler { ...@@ -67,23 +68,19 @@ public class GlobalDefaultExceptionHandler {
//处理自定义Exception //处理自定义Exception
@ExceptionHandler({AbstractApolloHttpException.class}) @ExceptionHandler({AbstractApolloHttpException.class})
public ResponseEntity<Map<String, Object>> badRequest(HttpServletRequest request, AbstractApolloHttpException ex) { public ResponseEntity<Map<String, Object>> badRequest(HttpServletRequest request, AbstractApolloHttpException ex) {
return handleError(request, ex); return handleError(request, ex.getHttpStatus(), ex, Level.ERROR);
} }
private ResponseEntity<Map<String, Object>> handleError(HttpServletRequest request, private ResponseEntity<Map<String, Object>> handleError(HttpServletRequest request,
AbstractApolloHttpException ex) { HttpStatus status, Throwable ex) {
return handleError(request, ex.getHttpStatus(), ex); return handleError(request, status, ex, Level.ERROR);
} }
private ResponseEntity<Map<String, Object>> handleError(HttpServletRequest request, private ResponseEntity<Map<String, Object>> handleError(HttpServletRequest request,
HttpStatus status, Throwable ex) { HttpStatus status, Throwable ex, Level logLevel) {
String message = ex.getMessage(); String message = ex.getMessage();
printLog(message, ex, logLevel);
logger.error(message, ex);
Tracer.logError(ex);
Map<String, Object> errorAttributes = new HashMap<>(); Map<String, Object> errorAttributes = new HashMap<>();
boolean errorHandled = false; boolean errorHandled = false;
...@@ -113,4 +110,27 @@ public class GlobalDefaultExceptionHandler { ...@@ -113,4 +110,27 @@ public class GlobalDefaultExceptionHandler {
return new ResponseEntity<>(errorAttributes, headers, status); return new ResponseEntity<>(errorAttributes, headers, status);
} }
//打印日志, 其中logLevel为日志级别: ERROR/WARN/DEBUG/INFO/TRACE
private void printLog(String message, Throwable ex, Level logLevel) {
switch (logLevel.toString()) {
case "ERROR":
logger.error(message, ex);
break;
case "WARN":
logger.warn(message, ex);
break;
case "DEBUG":
logger.debug(message, ex);
break;
case "INFO":
logger.info(message, ex);
break;
case "TRACE":
logger.trace(message, ex);
break;
}
Tracer.logError(ex);
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment