In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
In this issue, the editor will bring you all kinds of placeholders about how to configure accesslog and accesslog. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.
Accesslog related configuration server: undertow: # accesslog related configuration accesslog: # Storage directory. Default is logs dir:. / log # whether to enable enabled: true # format The various placeholders are followed by details of pattern:'{"transportProtocol": "% {TRANSPORT_PROTOCOL}", "scheme": "% {SCHEME}", "protocol": "% {PROTOCOL}", "method": "% {METHOD}", "reqHeaderUserAgent": "% {iMagle UserMuir Agent}" "cookieUserId": "% {cmae UserID}", "queryTest": "% {qtraining test}", "queryString": "% Q", "relativePath": "% R,% {REQUEST_PATH},% {RESOLVED_PATH}", "requestLine": "% r" "uri": "% U", "thread": "% I", "hostPort": "% {HOST_AND_PORT}", "localIp": "% A", "localPort": "% p", "localServerName": "% v" "remoteIp": "% a", "remoteHost": "% h", "bytesSent": "% b", "time": "% {time,yyyy-MM-dd HH:mm:ss.S}", "status": "% s", "reason": "% {RESPONSE_REASON_PHRASE}" "respHeaderUserSession": "% {resp-cookie,userId userSession}", "respCookieUserId": "% {resp-cookie,userId}", "timeUsed": "% Dms,% Ts,% {RESPONSE_TIME} ms,% {RESPONSE_TIME_MICROS} us,% {RESPONSE_TIME_NANOS} ns",}'# file prefix The default is access_log prefix: access. # File suffix, default is log suffix: log # whether to write access log from another log file, default is true # currently, rotate can only be performed by date, one log file per day rotate: true Note 1: log file rotate can only be written by date
The core class abstraction of Undertow's accesslog processing is io.undertow.server.handlers.accesslog.AccesslogReceiver. Because there is currently only one implementation of Undertow's AccesslogReceiver in use, that is, io.undertow.server.handlers.accesslog.DefaultAccessLogReceiver.
Check the rotate timing of DefaultAccessLogReceiver:
DefaultAccessLogReceiver
/ * calculate rotate time point * / private void calculateChangeOverPoint () {Calendar calendar = Calendar.getInstance (); calendar.set (Calendar.SECOND, 0); calendar.set (Calendar.MINUTE, 0); calendar.set (Calendar.HOUR_OF_DAY, 0); / / current time date + 1, that is, the next day calendar.add (Calendar.DATE, 1); SimpleDateFormat df = new SimpleDateFormat ("yyyy-MM-dd", Locale.US) CurrentDateString = df.format (new Date ()); / / if there is an existing default log file, use the date last modified instead of the current date if (Files.exists (defaultLogFile)) {try {currentDateString = df.format (new Date (Files.getLastModifiedTime (defaultLogFile). ToMillis ());} catch (IOException e) {/ / ignore. Use the current date if exception happens. }} / / rotate timing is 0 o'clock on the next day changeOverPoint = calendar.getTimeInMillis ();} accesslog placeholder
In fact, the accesslog placeholder in Undertow is the attribute of HTTP server exchange abstracted after the Undertow Listener parses the request as we mentioned earlier.
The table of the official website documentation is not the most complete, and the note is not specified, for example, some placeholders must be turned on certain Undertow features to use, and so on. Here, let's list.
First of all, put forward a note, parameter placeholder, such as% {I, the header value you want to see} look at the value of a key in header. Be careful not to have spaces after the comma, because this space will be counted in the key so that you can't get the key you want.
Request related attribute description abbreviation placeholder full name placeholder parameter placeholder source code request transport protocol, equivalent to request protocol no% {TRANSPORT_PROTOCOL} no TransportProtocolAttribute request mode, such as http, https, etc.
% {SCHEME} no RequestSchemeAttribute request protocol, such as HTTP/1.1,% H% {PROTOCOL}, no RequestProtocolAttribute request method, such as GET, POST,% m% {METHOD} No RequestMethodAttribute request, some value of Header without RequestMethodAttribute request, no% {I, the header value you want to see} a value of RequestHeaderAttributeCookie, no% {c, the cookie value you want to see, or% {req-cookie, the cookie value} corresponds to CookieAttribute and RequestCookieAttribute path parameter PathVariable, respectively, because it is not parsed by Undertow Listener or Handler. So you can't intercept it, and you can't confirm whether it's a PathVariable or a url path. So, PathVariable placeholders don't work. No% {p, the path parameters you want to view key} PathParameterAttribute request parameters, that is, url? After the key-value pair, you can choose to view the value of a key. No% {Q, the request parameter you want to view key} QueryParameterAttribute request parameter string, that is, url? All subsequent characters}% Q (excluding?)% {QUERY_STRING} (excluding?);% {BARE_QUERY_STRING} (including?) No QueryStringAttribute request relative path (in Spring Boot environment, RequestPath is equivalent to RelativePath and ResolvedPath in most cases), that is, excluding host,port, the path of request parameter string% R% {RELATIVE_PATH} or% {REQUEST_PATH} or% {RESOLVED_PATH} does not correspond to RelativePathAttribute and RequestPathAttribute and ResolvedPathAttribute request overall strings, respectively, including request method, request relative path, request parameter string, request protocol For example, Get / test?a=b HTTP/1.1%r% {REQUEST_LINE} No RequestLineAttribute request URI, including request relative path, request parameter string% U% {REQUEST_URL} No RequestURLAttribute processing thread% I% {THREAD_NAME} No ThreadNameAttribute
Note:
The path parameter PathVariable cannot be intercepted because it is not parsed by Undertow's Listener or Handler, and whether it is a PathVariable or a url path cannot be confirmed. So, PathVariable placeholders don't work.
Request address related description abbreviated placeholder full name placeholder parameters placeholder source code host and port, which is generally the Host value in the HTTP request Header. If Host is empty, the local address and port are obtained. If the port is not obtained, the default port (http:80) is used according to the protocol. Https:443) No% {HOST_AND_PORT} No HostAndPortAttribute request local address IP%A% {LOCAL_IP} No LocalIPAttribute request local port Port%p% {LOCAL_PORT} No LocalPortAttribute request local hostname, usually the Host value in the HTTP request Header, if Host is empty, get the local address% v% {LOCAL_SERVER_NAME} No LocalServerNameAttribute request remote host name, get the remote host address h% {REMOTE_HOST} without RemoteHostAttribute request remote IP through connection Get the remote IP%a% {REMOTE_IP} no RemoteIPAttribute through connection
Note:
Generally speaking, the remote address of the request is not obtained from the request connection, but through X-forwarded-for or X-real-ip in Http Header, because requests are sent through various VPN and load balancer.
Response related attributes describe the number of bytes sent by the abbreviation placeholder full name placeholder parameter placeholder source code, except for Http Header, b (- if empty) or B (0 if empty)% {BYTES_SENT} (0 if empty) no BytesSentAttributeaccesslog time, which is not the time to receive the request Instead, the response time% t% {DATE_TIME}% {time, the format of the SimpleDateFormat in your custom java} DateTimeAttributeHTTP response status code% s% {RESPONSE_CODE} No ResponseCodeAttributeHTTP response reason no% {RESPONSE_REASON_PHRASE} No ResponseReasonPhraseAttribute response Header a value without% {o, the header value you want to see} a value of the ResponseHeaderAttribute response Cookie without% {resp-cookie, the cookie value you want to see} ResponseCookieAttribute response time, the default undertow does not have statistics within the open request time Need to be turned on to count response time% D (milliseconds, for example 56 for 56ms)% T (seconds, for example 5.067 represents 5.067 seconds)% {RESPONSE_TIME} (equivalent to% D)% {RESPONSE_TIME_MICROS} (microseconds)% {RESPONSE_TIME_NANOS} (nanoseconds) no ResponseTimeAttribute
Note: by default, undertow does not enable statistics within the request time. You need to open it to calculate the response time. How to enable it? You can register a WebServerFactoryCustomizer into Spring ApplicationContext. Please look at the following code (project address: https://github.com/HashZhang/spring-cloud-scaffold/blob/master/spring-cloud-iiford/):
Spring.factories (omitting extraneous code)
# AutoConfigurationorg.springframework.boot.autoconfigure.EnableAutoConfiguration=\ com.github.hashjang.spring.cloud.iiford.service.common.auto.UndertowAutoConfiguration
UndertowAutoConfiguration
/ / set proxyBeanMethods=false, because methods without @ Bean need to return the same Bean every time they call each other, and there is no need for a proxy. Close and increase the startup speed @ Configuration (proxyBeanMethods=false) @ Import (WebServerConfiguration.class) public class UndertowAutoConfiguration {}.
WebServerConfiguration
/ / set proxyBeanMethods=false, because methods without @ Bean need to return the same Bean every time they call each other, and there is no need for a proxy. Close and increase the startup speed @ Configuration (proxyBeanMethods=false) public class WebServerConfiguration {@ Bean public WebServerFactoryCustomizer undertowWebServerAccessLogTimingEnabler (ServerProperties serverProperties) {return new DefaultWebServerFactoryCustomizer (serverProperties);}}.
DefaultWebServerFactoryCustomizer
Public class DefaultWebServerFactoryCustomizer implements WebServerFactoryCustomizer {private final ServerProperties serverProperties; public DefaultWebServerFactoryCustomizer (ServerProperties serverProperties) {this.serverProperties = serverProperties;} @ Override public void customize (ConfigurableUndertowWebServerFactory factory) {String pattern = serverProperties.getUndertow () .getAccesslog () .getPattern () / / if the response time is printed in the accesslog configuration, open the record request start time to configure if (logRequestProcessingTiming (pattern)) {factory.addBuilderCustomizers (builder-> builder.setServerOption (UndertowOptions.RECORD_REQUEST_START_TIME, true));}} private boolean logRequestProcessingTiming (String pattern) {if (StringUtils.isBlank (pattern)) {return false } / / determine whether accesslog is configured to view response time return pattern.contains (ResponseTimeAttribute.RESPONSE_TIME_MICROS) | | pattern.contains (ResponseTimeAttribute.RESPONSE_TIME_MILLIS) | | pattern.contains (ResponseTimeAttribute.RESPONSE_TIME_NANOS) | | pattern.contains (ResponseTimeAttribute.RESPONSE_TIME_MILLIS_SHORT) | | pattern.contains (ResponseTimeAttribute.RESPONSE_TIME_SECONDS_SHORT) }} other
There are also security-related attributes (SSL-related, login-authentication Authentication-related), which are generally not needed for internal calls to micro-services, so we will not repeat them here. Other built-in properties are generally not used in Spring Boot environments, so we won't discuss them here.
Give an example
The example request for our initial configuration of accesslog is returned as follows (the result after JSON formatting):
{"transportProtocol": "http/1.1", "scheme": "http", "protocol": "HTTP/1.1", "method": "GET", "reqHeaderUserAgent": "PostmanRuntime/7.26.10", "cookieUserId": "testRequestCookieUserId", "queryTest": "1", "queryString": "? test=1&query=2" "relativePath": "/ test, / test, -", "requestLine": "GET / test?test=1&query=2 HTTP/1.1", "uri": "/ test", "thread": "XNIO-2 task-1", "hostPort": "127.0.0.1 test", "localIp": "127.0.0.1", "localPort": "8102" "localServerName": "127.0.0.1", "remoteIp": "127.0.0.1", "remoteHost": "127.0.0.1", "bytesSent": "26", "time": "2021-04-08 00 time", "status": "200", "reason": "OK" "respHeaderUserSession": "testResponseHeaderUserSession", "respCookieUserId": "testResponseCookieUserId", "timeUsed": "3683ms, 3.683s, 3683ms, 3683149 us, 3683149200 ns".} these are the various placeholders of accesslog and accesslog shared by the editor. If you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.