Skip to content

MDC

  1. 将用户 IP 记录在日志中
  2. 将用户 工号 记录在日志中
  3. 将用户 请求ID 记录在日志中
  4. 将用户 请求地址 记录在日志中

配置

shell
package cloud.xuxiaowei.core.filter;

import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.MDC;
import org.springframework.core.Ordered;
import org.springframework.stereotype.Component;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 日志 过滤器
 *
 * @author xuxiaowei
 * @since 0.0.1
 */
@Slf4j
@Setter
@Component
public class LogHttpFilter extends HttpFilter implements Ordered {

    public static final int ORDERED = Ordered.HIGHEST_PRECEDENCE;

    private int order = ORDERED;

    @Override
    public int getOrder() {
        return order;
    }

    @Override
    protected void doFilter(HttpServletRequest request, HttpServletResponse response, FilterChain chain)
            throws IOException, ServletException {

        String remoteHost = request.getRemoteHost();
        String userId = "获取用户工号";
        String requestId = "获取请求ID";
        String uri = request.getRequestURI();

        MDC.put("IP", remoteHost);
        MDC.put("USER_ID", userId);
        MDC.put("REQUEST_ID", requestId);
        MDC.put("REQUEST_URI", uri);

        super.doFilter(request, response, chain);
    }

}
shell
package cloud.xuxiaowei.core.filter;

import jakarta.servlet.FilterChain;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpFilter;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.MDC;
import org.springframework.core.Ordered;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * 日志 过滤器
 *
 * @author xuxiaowei
 * @since 0.0.1
 */
@Slf4j
@Setter
@Component
public class LogHttpFilter extends HttpFilter implements Ordered {

	public static final int ORDERED = Ordered.HIGHEST_PRECEDENCE;

	private int order = ORDERED;

	@Override
	public int getOrder() {
		return order;
	}

	@Override
	protected void doFilter(HttpServletRequest request, HttpServletResponse response, FilterChain chain)
			throws IOException, ServletException {

		String remoteHost = request.getRemoteHost();
		String userId = "获取用户工号";
		String requestId = "获取请求ID";
		String uri = request.getRequestURI();

		MDC.put("IP", remoteHost);
		MDC.put("USER_ID", userId);
		MDC.put("REQUEST_ID", requestId);
		MDC.put("REQUEST_URI", uri);

		super.doFilter(request, response, chain);
	}

}
shell
package cloud.xuxiaowei.gateway.filter;

import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.MDC;
import org.springframework.core.Ordered;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.lang.NonNull;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.server.WebFilter;
import org.springframework.web.server.WebFilterChain;
import reactor.core.publisher.Mono;

import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.URI;

/**
 * 日志 过滤器
 *
 * @author xuxiaowei
 * @since 0.0.1
 */
@Slf4j
@Setter
@Component
public class LogWebFilter implements WebFilter, Ordered {

	public static final int ORDERED = Ordered.HIGHEST_PRECEDENCE;

	private int order = ORDERED;

	@Override
	public int getOrder() {
		return order;
	}

	@NonNull
	@Override
	public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {

		ServerHttpRequest request = exchange.getRequest();

		InetSocketAddress remoteAddress = request.getRemoteAddress();
		InetAddress address = remoteAddress.getAddress();
		String hostAddress = address.getHostAddress();

		String userId = "获取用户工号";
		String requestId = "获取请求ID";
		URI uri = request.getURI();

		MDC.put("IP", hostAddress);
		MDC.put("USER_ID", userId);
		MDC.put("REQUEST_ID", requestId);
		MDC.put("REQUEST_URI", uri.toString());

		return chain.filter(exchange);
	}

}

使用

# 在日志输出格式中添加下列配置,即可在打印日志中附带相关信息
%X{IP} : %X{USER_ID} : %X{REQUEST_ID} : %X{REQUEST_URI}