código fuente de reactor-netty: registro de acceso de AccessLog

objeto de registro reactor-netty

package reactor.netty.http.server;

import reactor.util.Logger;
import reactor.util.Loggers;

import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
import java.util.Objects;

final class AccessLog {
	static final Logger log = Loggers.getLogger("reactor.netty.http.server.AccessLog");
	static final DateTimeFormatter DATE_TIME_FORMATTER =
			DateTimeFormatter.ofPattern("dd/MMM/yyyy:HH:mm:ss Z", Locale.US);
    //logger输出格式
	static final String COMMON_LOG_FORMAT =
			"{} - {} [{}] \"{} {} {}\" {} {} {} {} ms";
    //空值显示
	static final String MISSING = "-";
    //区域时间
	final String zonedDateTime;
    //远程remote ip
	String address;
    //请求方法
	CharSequence method;
    //请求路径
	CharSequence uri;
    //请求协议
	String protocol;
	String user = MISSING;
    //请求响应状态
	CharSequence status;
    //响应体长度
	long contentLength;
    //传输协议是否为chunked
	boolean chunked;
	long startTime = System.currentTimeMillis();
	int port;

	AccessLog() {
		this.zonedDateTime = ZonedDateTime.now().format(DATE_TIME_FORMATTER);
	}

	AccessLog address(String address) {
        //null值是抛出NPE异常
		this.address = Objects.requireNonNull(address, "address");
		return this;
	}

	AccessLog port(int port) {
		this.port = port;
		return this;
	}

	AccessLog method(CharSequence method) {
		this.method = Objects.requireNonNull(method, "method");
		return this;
	}

	AccessLog uri(CharSequence uri) {
		this.uri = Objects.requireNonNull(uri, "uri");
		return this;
	}

	AccessLog protocol(String protocol) {
		this.protocol = Objects.requireNonNull(protocol, "protocol");
		return this;
	}

	AccessLog status(CharSequence status) {
		this.status = Objects.requireNonNull(status, "status");
		return this;
	}

	AccessLog contentLength(long contentLength) {
		this.contentLength = contentLength;
		return this;
	}
    //传输协议为chunked时响应体长度累加
	AccessLog increaseContentLength(long contentLength) {
		if (chunked) {
			this.contentLength += contentLength;
		}
		return this;
	}

	AccessLog chunked(boolean chunked) {
		this.chunked = chunked;
		return this;
	}
    //请求持续时间,响应时间
	long duration() {
		return System.currentTimeMillis() - startTime;
	}

	void log() {
        //判断是否开启了netty log
        //开启配置:-Dreactor.netty.http.server.accessLogEnabled=true
		if (log.isInfoEnabled()) {
			log.info(COMMON_LOG_FORMAT, address, user, zonedDateTime,
					method, uri, protocol, status, (contentLength > -1 ? contentLength : MISSING), port, duration());
		}
	}
}

 

Supongo que te gusta

Origin blog.csdn.net/lizz861109/article/details/110670312
Recomendado
Clasificación