rename the package
This commit is contained in:
53
src/main/java/xyz/mcutils/backend/log/TransactionLogger.java
Normal file
53
src/main/java/xyz/mcutils/backend/log/TransactionLogger.java
Normal file
@ -0,0 +1,53 @@
|
||||
package cc.fascinated.log;
|
||||
|
||||
import cc.fascinated.common.IPUtils;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import lombok.NonNull;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.core.MethodParameter;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.converter.HttpMessageConverter;
|
||||
import org.springframework.http.server.ServerHttpRequest;
|
||||
import org.springframework.http.server.ServerHttpResponse;
|
||||
import org.springframework.http.server.ServletServerHttpRequest;
|
||||
import org.springframework.web.bind.annotation.ControllerAdvice;
|
||||
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
@ControllerAdvice
|
||||
@Slf4j(topic = "Req Transaction")
|
||||
public class TransactionLogger implements ResponseBodyAdvice<Object> {
|
||||
@Override
|
||||
public Object beforeBodyWrite(Object body, @NonNull MethodParameter returnType, @NonNull MediaType selectedContentType,
|
||||
@NonNull Class<? extends HttpMessageConverter<?>> selectedConverterType, @NonNull ServerHttpRequest rawRequest,
|
||||
@NonNull ServerHttpResponse rawResponse) {
|
||||
HttpServletRequest request = ((ServletServerHttpRequest) rawRequest).getServletRequest();
|
||||
|
||||
// Get the request ip ip
|
||||
String ip = IPUtils.getRealIp(request);
|
||||
|
||||
// Getting params
|
||||
Map<String, String> params = new HashMap<>();
|
||||
for (Entry<String, String[]> entry : request.getParameterMap().entrySet()) {
|
||||
params.put(entry.getKey(), Arrays.toString(entry.getValue()));
|
||||
}
|
||||
|
||||
// Logging the request
|
||||
log.info(String.format("[Req] %s | %s | '%s', params=%s",
|
||||
request.getMethod(),
|
||||
ip,
|
||||
request.getRequestURI(),
|
||||
params
|
||||
));
|
||||
return body;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean supports(@NonNull MethodParameter returnType, @NonNull Class<? extends HttpMessageConverter<?>> converterType) {
|
||||
return true;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user