Commit e08a5d78 authored by huangcb's avatar huangcb

优化日志输出

parent 4c077489
...@@ -49,6 +49,11 @@ public class CommonConstants { ...@@ -49,6 +49,11 @@ public class CommonConstants {
**/ **/
public static final String SPRING_BOOT_ADMIN_PREFIX_URL = "/actuator"; public static final String SPRING_BOOT_ADMIN_PREFIX_URL = "/actuator";
/**
* 文件下载URL
**/
public static final String FILE_DOWNLOAD_PREFIX_URL = "/file/download/";
/** /**
* 默认字符编码 * 默认字符编码
**/ **/
......
...@@ -28,9 +28,16 @@ import java.util.Map; ...@@ -28,9 +28,16 @@ import java.util.Map;
@Slf4j @Slf4j
public class RestLogFilter implements Filter { public class RestLogFilter implements Filter {
private Map<String, Integer> API_BODY_LOG_MAP = new HashMap<>();
@Override @Override
public void init(FilterConfig filterConfig) { public void init(FilterConfig filterConfig) {
// 设置API日志级别[5->4->3->2->1->0]:Fatal->error->warn->info->debug->trace
API_BODY_LOG_MAP.put("/file/upload/single", 1);
API_BODY_LOG_MAP.put("/file/download/getFileData", 2);
API_BODY_LOG_MAP.put("/file/delete/single", 2);;
API_BODY_LOG_MAP.put("/file/ocr/idcard/front", 1);
API_BODY_LOG_MAP.put("/file/upload/imageWithWatermark", 1);
} }
@Override @Override
...@@ -132,9 +139,18 @@ public class RestLogFilter implements Filter { ...@@ -132,9 +139,18 @@ public class RestLogFilter implements Filter {
// 日志输出请求体 // 日志输出请求体
if (url.startsWith(CommonConstants.SPRING_BOOT_ADMIN_PREFIX_URL)) { if (url.startsWith(CommonConstants.SPRING_BOOT_ADMIN_PREFIX_URL)) {
log.debug("[IP={}]收到{}请求,url:{},params:{}", ReqUtils.getHttpClientIp(requestWrapper), method, url, reqBody); if (log.isDebugEnabled()) {
log.debug("[IP={}]收到{}请求,url:{},params:{}", ReqUtils.getHttpClientIp(requestWrapper), method, url, reqBody);
}
} else { } else {
log.info("[IP={}]收到{}请求,url:{},body:{}", ReqUtils.getHttpClientIp(requestWrapper), method, url, reqBody); log.info("[IP={}]收到{}请求,url:{}", ReqUtils.getHttpClientIp(requestWrapper), method, url);
if (API_BODY_LOG_MAP.containsKey(url) && 1 < API_BODY_LOG_MAP.get(url)) {
log.info("body:{}", reqBody);
} else {
if (log.isDebugEnabled()) {
log.debug("body:{}", reqBody);
}
}
} }
} }
...@@ -163,13 +179,19 @@ public class RestLogFilter implements Filter { ...@@ -163,13 +179,19 @@ public class RestLogFilter implements Filter {
log.error(e.getMessage(), e); log.error(e.getMessage(), e);
} }
String url = requestWrapper.getRequestURI(); String url = requestWrapper.getRequestURI();
if (CommonConstants.LOG_MAX_LENGTH < resBody.length()) {
resBody = resBody.substring(0, CommonConstants.LOG_CUT_LENGTH) + "……";
}
if (url.startsWith(CommonConstants.SPRING_BOOT_ADMIN_PREFIX_URL)) { if (url.startsWith(CommonConstants.SPRING_BOOT_ADMIN_PREFIX_URL)) {
log.debug("请求响应:{}", resBody); if (log.isDebugEnabled()) {
log.debug("请求响应:{}", resBody);
}
} else if (url.startsWith(CommonConstants.FILE_DOWNLOAD_PREFIX_URL)) {
if (log.isDebugEnabled()) {
log.debug("请求响应:{}", resBody);
} else {
log.info("请求响应文件数据");
}
} else { } else {
log.info("请求响应:{}", resBody); log.info("请求响应:{}", resBody);
} }
} }
} }
\ No newline at end of file
...@@ -47,6 +47,10 @@ ...@@ -47,6 +47,10 @@
<appender-ref ref="CONSOLE_APPENDER" /> <appender-ref ref="CONSOLE_APPENDER" />
<appender-ref ref="FILE_APPENDER" /> <appender-ref ref="FILE_APPENDER" />
</logger> </logger>
<logger name="com.esv.freight.file.common.filter" level="INFO" additivity="false">
<appender-ref ref="CONSOLE_APPENDER" />
<appender-ref ref="FILE_APPENDER" />
</logger>
<!--控制台和日志文件输出级别--> <!--控制台和日志文件输出级别-->
<root level="INFO" additivity="false"> <root level="INFO" additivity="false">
......
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