reactor-netty源码:AccessLog访问日志
发布日期:2021-06-30 21:31:38 浏览次数:3 分类:技术文章

本文共 2380 字,大约阅读时间需要 7 分钟。

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());		}	}}

 

转载地址:https://lizz6.blog.csdn.net/article/details/110670312 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:Spring Boot Configuration Annotation Processor not configured
下一篇:gateway、webflux、reactor-netty请求日志输出

发表评论

最新留言

初次前来,多多关照!
[***.217.46.12]2024年04月20日 07时52分21秒