REQ-3581: 添加日志信息

This commit is contained in:
yanglin 2025-03-05 11:31:42 +08:00
parent 175c610b96
commit babe961401
4 changed files with 106 additions and 1 deletions

View File

@ -1,6 +1,8 @@
package cn.axzo.nanopart.ess.server.entity;
import org.apache.commons.lang3.StringUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.annotation.TableField;
@ -9,6 +11,7 @@ import com.baomidou.mybatisplus.extension.handlers.FastjsonTypeHandler;
import com.google.common.base.Throwables;
import cn.axzo.nanopart.ess.api.utils.YesOrNo;
import cn.axzo.nanopart.ess.server.http.RequestNoInterceptor;
import cn.axzo.pokonyan.config.mybatisplus.BaseEntity;
import lombok.Getter;
import lombok.Setter;
@ -49,10 +52,17 @@ public class EssLog extends BaseEntity<EssLog> {
public void addLogContent(String key, Object value) {
if (logContent == null)
logContent = new JSONObject();
initializeLogContent();
logContent.put(key, value);
}
private void initializeLogContent() {
logContent = new JSONObject();
String requestNo = RequestNoInterceptor.getRequestNo();
if (StringUtils.isNotBlank(requestNo))
logContent.put("requestNo", requestNo);
}
public void setError(Exception exception) {
if (exception == null)
return;

View File

@ -0,0 +1,24 @@
package cn.axzo.nanopart.ess.server.http;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import lombok.RequiredArgsConstructor;
/**
* @author yanglin
*/
@Configuration
@RequiredArgsConstructor
public class EssWebMvcConfigurer implements WebMvcConfigurer {
private final RequestNoInterceptor requestNoInterceptor;
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(requestNoInterceptor)
.addPathPatterns("/**");
}
}

View File

@ -0,0 +1,45 @@
package cn.axzo.nanopart.ess.server.http;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import cn.axzo.nanopart.ess.server.utils.UUIDUtil;
import lombok.NonNull;
/**
* @author yanglin
*/
@Component
public class RequestNoInterceptor implements HandlerInterceptor {
private static final ThreadLocal<String> REQUEST_NO = new ThreadLocal<>();
@Override
public boolean preHandle(@NonNull HttpServletRequest request, @NonNull HttpServletResponse response,
@NonNull Object handler) {
REQUEST_NO.set(UUIDUtil.uuidString());
return true;
}
@Override
public void postHandle(@NonNull HttpServletRequest request, @NonNull HttpServletResponse response,
@NonNull Object handler, ModelAndView modelAndView) {
REQUEST_NO.remove();
}
@Override
public void afterCompletion(@NonNull HttpServletRequest request, @NonNull HttpServletResponse response,
@NonNull Object handler, Exception ex) {
REQUEST_NO.remove();
}
public static String getRequestNo() {
return REQUEST_NO.get();
}
}

View File

@ -0,0 +1,26 @@
package cn.axzo.nanopart.ess.server.utils;
import java.util.UUID;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
/**
* UUID生成器
*
* @author cold_blade
* @date 2023/10/5
* @version 1.0
*/
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class UUIDUtil {
public static String uuidString() {
String str = UUID.randomUUID().toString();
return str.replaceAll("-", "");
}
public static String uuidRawString() {
return UUID.randomUUID().toString();
}
}