相信各位同道在写代码的时候,肯定会写一些日志打印,因为这对往后的运维而言,至关重要的。
那么我们请求一个restfull接口的时候,哪些信息是应该被日志记录的呢?
以下做了一个基本的简单例子,这里只是示例说明基本常规实现记录的信息,根据项目的真实情况选用:
1 . Http请求拦截器(Filter) : 从HttpServletRequest获取基本的请求信息
import name.ealen.util.HttpUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@Configuration
public class FilterConfiguration {
private static final Logger log = LoggerFactory.getLogger(FilterConfig.class);
@Bean
@Order(Integer.MIN_VALUE)
@Qualifier("filterRegistration")
public FilterRegistrationBean filterRegistration() {
FilterRegistrationBean registration = new FilterRegistrationBean<>();
registration.setFilter(controllerFilter());
registration.addUrlPatterns("
@Aspect
@Component
public class ControllerInterceptor {
private static final Logger log = LoggerFactory.getLogger(ControllerInterceptor.class);
@Resource
private Environment environment;
@Around(value = "execution (* name.ealen.web.*.*(..))")
public Object processApiFacade(ProceedingJoinPoint pjp) {
String appName;
try {
appName = environment.getProperty("spring.application.name").toUpperCase();
} catch (Exception e) {
appName = "UNNAMED";
}
long startTime = System.currentTimeMillis();
String name = pjp.getTarget().getClass().getSimpleName();
String method = pjp.getSignature().getName();
Object result = null;
HttpStatus status = null;
try {
result = pjp.proceed();
log.info("RequestTarget : " + appName + "." + name + "." + method);
log.info("RequestParam : " + JSON.toJSON(pjp.getArgs()));
if (result instanceof ResponseEntity) {
status = ((ResponseEntity) result).getStatusCode();
} else {
status = HttpStatus.OK;
}
} catch (Throwable throwable) {
status = HttpStatus.INTERNAL_SERVER_ERROR;
result = new ResponseEntity<>("{"Internal Server Error" : "" + throwable.getMessage() + ""}", status);
throwable.printStackTrace();
} finally {
log.info("ResponseEntity : {" + ""HttpStatus":"" + status.toString() + """ + ","ResponseBody": " + JSON.toJSON(result) + "}");
log.info("Internal Method Cost Time: {}ms", System.currentTimeMillis() - startTime);
}
return result;
}
}
3 . 提供一个简单的restfull接口 :
package name.ealen.web;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class SayHelloController {
@RequestMapping("/sayHello")
public String sayHello() {
return "hello world";
}
@RequestMapping("/say")
public ResponseEntity> say(@RequestBody Object o) {
return new ResponseEntity<>(o, HttpStatus.OK);
}
}
4 . 使用Postman进行基本测试 :
5 . 控制台可以看到基本效果 :
以上只是关于Controller应该记录日志的一个简单的例子,完整代码可见 https://github.com/EalenXie/springboot-controller-logger
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。



