springboot prints http requests and handles system exception logs

@Configuration
public class YuncaiRequestLoggingFilter {
	private static final int MAX_PAYLOAD_LENGTH = 10000;

	@Autowired
	private CustomServletContextRequestLoggingFilter customServletContextRequestLoggingFilter;

	@Bean
	public ServletContextRequestLoggingFilter requestLoggingFilter() {
		ServletContextRequestLoggingFilter loggingFilter = customServletContextRequestLoggingFilter;
		loggingFilter.setIncludeClientInfo(true);
		loggingFilter.setIncludeQueryString(true);
		loggingFilter.setIncludePayload(true);
		loggingFilter.setIncludeHeaders(true);
		loggingFilter.setMaxPayloadLength(MAX_PAYLOAD_LENGTH);
		return loggingFilter;
	}
}

@ Slf4j
@Component
public class CustomServletContextRequestLoggingFilter extends ServletContextRequestLoggingFilter {

	@Autowired
	private EmailLogService emailLogService;

	private void sendLogEmail(String exceptionMessage, String requestMessage) {
		StringBuilder stringBuilder = new StringBuilder();
		stringBuilder.append("Timestamp: ").append(System.currentTimeMillis()).append("\n").append("Time: ")
				.append(LocalDateTime.now().toString()).append("\n").append("Thread Id: ")
				.append(Thread.currentThread().getId()).append("\n").append("Thread Name: ")
				.append(Thread.currentThread().getName()).append("\n").append("Request: \n").append(requestMessage)
				.append("\n").append("Exception: \n").append(exceptionMessage).append("\n");
		emailLogService.sendMail(stringBuilder.toString());
	}

	@Override
	protected void afterRequest(HttpServletRequest request, String message) {
		super.afterRequest(request, message);
		if (ExceptionContext.getExceptionMessage() != null) {
			try {
				
//		        ExceptionMessage exceptionMessage = new ExceptionMessage();
// //TODO exception message sent
//		        exceptionSendComponent.sendExceptionMessage(exceptionMessage);
		        
				sendLogEmail(ExceptionContext.getExceptionMessage(), message);
				ExceptionContext.destroy();
			} catch (Exception e) {
				log.error("error", e);
			}
		}

	}
}
2018-04-26 15:14:57.952  INFO 1464 --- [http-nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : After request [uri=/scan?code=111111111&uid=111111111&userId=1&payType=0;client=0:0:0:0:0:0:0:1;headers={host=[localhost:8080], connection=[keep-alive], accept=[*/*], user-agent=[Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36], privatetenantcode=[zyd], referer=[http://localhost:8080/swagger-ui.html], accept-encoding=[gzip, deflate, br], accept-language=[zh-CN,zh;q=0.9,en;q=0.8]}]

The AbstractRequestLoggingFilter class defines two methods, beforeRequest and afterRequest, which are used to set the operations performed before and after filtering. It has three subclasses, CommonsRequestLoggingFilter, ServletContextRequestLoggingFilter, and Log4jNestedDiagnosticContextFilter. These three subclasses implement their own beforeRequest and afterRequest respectively. Among them, CommonsRequestLoggingFilter prints a piece of debug information before and after filtering; ServletContextRequestLoggingFilter writes a piece of log information to the log file before and after filtering, and the log file can be specified by log4j.properties; Log4jNestedDiagnosticContextFilter stores the log information in NDC, and NDC uses A stack-like mechanism is used to push and pot context information. Each thread stores context information independently. For example, a servlet can create a corresponding NDC for each request and store information such as client addresses.




Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325569472&siteId=291194637