溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

如何配置accesslog以及accesslog 的各種占位符

發(fā)布時(shí)間:2021-09-27 10:03:56 來(lái)源:億速云 閱讀:207 作者:柒染 欄目:編程語(yǔ)言

這期內(nèi)容當(dāng)中小編將會(huì)給大家?guī)?lái)有關(guān)如何配置accesslog以及accesslog 的各種占位符,文章內(nèi)容豐富且以專(zhuān)業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。

accesslog 相關(guān)配置

server:
  undertow:
    # access log相關(guān)配置
    accesslog:
      # 存放目錄,默認(rèn)為 logs
      dir: ./log
      # 是否開(kāi)啟
      enabled: true
      # 格式,各種占位符后面會(huì)詳細(xì)說(shuō)明
      pattern: '{
                  "transportProtocol":"%{TRANSPORT_PROTOCOL}",
                  "scheme":"%{SCHEME}",
                  "protocol":"%{PROTOCOL}",
                  "method":"%{METHOD}",
                  "reqHeaderUserAgent":"%{i,User-Agent}",
                  "cookieUserId": "%{c,userId}",
                  "queryTest": "%{q,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":"%{o,userSession}",
                  "respCookieUserId":"%{resp-cookie,userId}",
                  "timeUsed":"%Dms, %Ts, %{RESPONSE_TIME}ms, %{RESPONSE_TIME_MICROS} us, %{RESPONSE_TIME_NANOS} ns",
                }'
      # 文件前綴,默認(rèn)為 access_log
      prefix: access.
      # 文件后綴,默認(rèn)為 log
      suffix: log
      # 是否另起日志文件寫(xiě) access log,默認(rèn)為 true
      # 目前只能按照日期進(jìn)行 rotate,一天一個(gè)日志文件
      rotate: true

注意點(diǎn) 1:日志文件 rotate 目前只能按照日期

Undertow 的 accesslog 處理核心類(lèi)抽象是 io.undertow.server.handlers.accesslog.AccesslogReceiver。由于目前 Undertow 的 AccesslogReceiver 只有一種實(shí)現(xiàn)在使用,也就是 io.undertow.server.handlers.accesslog.DefaultAccessLogReceiver。

查看 DefaultAccessLogReceiver 的 rotate 時(shí)機(jī):

DefaultAccessLogReceiver

/**
 * 計(jì)算 rotate 時(shí)間點(diǎn)
 */
private void calculateChangeOverPoint() {
    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.SECOND, 0);
    calendar.set(Calendar.MINUTE, 0);
    calendar.set(Calendar.HOUR_OF_DAY, 0);
    //當(dāng)前時(shí)間日期 + 1,即下一天
    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 時(shí)機(jī)是下一天的 0 點(diǎn)
    changeOverPoint = calendar.getTimeInMillis();
}

accesslog 占位符

其實(shí) Undertow 中的 accesslog 占位符,就是之前我們提到的 Undertow Listener 解析請(qǐng)求后抽象的 HTTP server exchange 的屬性。

官網(wǎng)文檔的表格并不是最全的,并且注意點(diǎn)并沒(méi)有說(shuō)明,例如某些占位符必須打開(kāi)某些 Undertow 特性才能使用等等。這里我們列出下。

首先先提出一個(gè)注意點(diǎn),參數(shù)占位符,例如 %{i,你要看的header值} 查看 header 的某個(gè) key 的值。逗號(hào)后面注意不要有空格,因?yàn)檫@個(gè)空格會(huì)算入 key 里面導(dǎo)致拿不到你想要的 key。

請(qǐng)求相關(guān)屬性

描述縮寫(xiě)占位符全名占位符參數(shù)占位符源碼
請(qǐng)求傳輸協(xié)議,等價(jià)于請(qǐng)求協(xié)議無(wú)%{TRANSPORT_PROTOCOL}無(wú)TransportProtocolAttribute
請(qǐng)求模式,例如 http、https 等
%{SCHEME}無(wú)RequestSchemeAttribute
請(qǐng)求協(xié)議,例如 HTTP/1.1%H%{PROTOCOL}無(wú)RequestProtocolAttribute
請(qǐng)求方法,例如 GET、POST 等%m%{METHOD}無(wú)RequestMethodAttribute
請(qǐng)求 Header 的某一個(gè)值無(wú)無(wú)%{i,你要看的header值}RequestHeaderAttribute
Cookie 的某一個(gè)值無(wú)無(wú)%{c,你要看的cookie值} 或者 %{req-cookie,你要看的cookie值}分別對(duì)應(yīng) CookieAttributeRequestCookieAttribute
路徑參數(shù) PathVariable 由于并沒(méi)有被 Undertow 的 Listener 或者 Handler 解析處理,所以攔截不到,無(wú)法確認(rèn)是否是一個(gè) PathVariable 還是就是 url 路徑。所以,PathVariable 的占位符是不會(huì)起作用的。無(wú)無(wú)%{p, 你想查看的路徑參數(shù) key }PathParameterAttribute
請(qǐng)求參數(shù),即 url 的 ? 之后鍵值對(duì),這里可以選擇查看某個(gè) key 的值。無(wú)無(wú)%{q, 你想查看的請(qǐng)求參數(shù) key}QueryParameterAttribute
請(qǐng)求參數(shù)字符串,即 url 的 ? 之后的所有字符}%q(不包含 ?)%{QUERY_STRING}(不包含 ?);%{BARE_QUERY_STRING}(包含 ?)無(wú)QueryStringAttribute
請(qǐng)求相對(duì)路徑(在 Spring Boot 環(huán)境下,大多數(shù)情況 RequestPath 和 RelativePath 還有 ResolvedPath 是等價(jià)的),即除去 host,port,請(qǐng)求參數(shù)字符串的路徑%R%{RELATIVE_PATH} 或者 %{REQUEST_PATH} 或者 %{RESOLVED_PATH}無(wú)分別對(duì)應(yīng) RelativePathAttributeRequestPathAttributeResolvedPathAttribute
請(qǐng)求整體字符串,包括請(qǐng)求方法,請(qǐng)求相對(duì)路徑,請(qǐng)求參數(shù)字符串,請(qǐng)求協(xié)議,例如 Get /test?a=b HTTP/1.1%r%{REQUEST_LINE}無(wú)RequestLineAttribute
請(qǐng)求 URI,包括請(qǐng)求相對(duì)路徑,請(qǐng)求參數(shù)字符串%U%{REQUEST_URL}無(wú)RequestURLAttribute
處理請(qǐng)求的線程%I%{THREAD_NAME}無(wú)ThreadNameAttribute

注意:

  1. 路徑參數(shù) PathVariable 由于并沒(méi)有被 Undertow 的 Listener 或者 Handler 解析處理,所以攔截不到,無(wú)法確認(rèn)是否是一個(gè) PathVariable 還是就是 url 路徑。所以,PathVariable 的占位符是不會(huì)起作用的。

請(qǐng)求地址相關(guān)

描述縮寫(xiě)占位符全名占位符參數(shù)占位符源碼
host 和 port,一般就是 HTTP 請(qǐng)求 Header 中的 Host 值,如果 Host 為空則獲取本地地址和端口,如果沒(méi)獲取到端口則根據(jù)協(xié)議用默認(rèn)端口(http:80,,https:443)無(wú)%{HOST_AND_PORT}無(wú)HostAndPortAttribute
請(qǐng)求本地地址 IP%A%{LOCAL_IP}無(wú)LocalIPAttribute
請(qǐng)求本地端口 Port%p%{LOCAL_PORT}無(wú)LocalPortAttribute
請(qǐng)求本地主機(jī)名,一般就是 HTTP 請(qǐng)求 Header 中的 Host 值,如果 Host 為空則獲取本地地址%v%{LOCAL_SERVER_NAME}無(wú)LocalServerNameAttribute
請(qǐng)求遠(yuǎn)程主機(jī)名,通過(guò)連接獲取遠(yuǎn)端的主機(jī)地址%h%{REMOTE_HOST}無(wú)RemoteHostAttribute
請(qǐng)求遠(yuǎn)程 IP,通過(guò)連接獲取遠(yuǎn)端的 IP%a%{REMOTE_IP}無(wú)RemoteIPAttribute

注意:

  1. 請(qǐng)求的遠(yuǎn)程地址我們一般不從請(qǐng)求連接獲取,而是通過(guò) Http Header 里面的 X-forwarded-for 或者 X-real-ip 等獲取,因?yàn)楝F(xiàn)在請(qǐng)求都是通過(guò)各種 VPN,負(fù)載均衡器發(fā)上來(lái)的。

響應(yīng)相關(guān)屬性

描述縮寫(xiě)占位符全名占位符參數(shù)占位符源碼
發(fā)送的字節(jié)數(shù)大小,除了 Http Header 以外%b (如果為空就是 -) 或者 %B (如果為空就是 0)%{BYTES_SENT} (如果為空就是 0)無(wú)BytesSentAttribute
accesslog 時(shí)間,這個(gè)不是收到請(qǐng)求的時(shí)間,而是響應(yīng)的時(shí)間%t%{DATE_TIME}%{time, 你自定義的 java 中 SimpleDateFormat 的格式}DateTimeAttribute
HTTP 響應(yīng)狀態(tài)碼%s%{RESPONSE_CODE}無(wú)ResponseCodeAttribute
HTTP 響應(yīng)原因無(wú)%{RESPONSE_REASON_PHRASE}無(wú)ResponseReasonPhraseAttribute
響應(yīng) Header 的某一個(gè)值無(wú)無(wú)%{o,你要看的header值}ResponseHeaderAttribute
響應(yīng) Cookie 的某一個(gè)值無(wú)無(wú)%{resp-cookie,你要看的cookie值}ResponseCookieAttribute
響應(yīng)時(shí)間,默認(rèn) undertow 沒(méi)有開(kāi)啟請(qǐng)求時(shí)間內(nèi)統(tǒng)計(jì),需要打開(kāi)才能統(tǒng)計(jì)響應(yīng)時(shí)間%D(毫秒,例如 56 代表 56ms) %T(秒,例如 5.067 代表 5.067 秒)%{RESPONSE_TIME}(等價(jià)于 %D) %{RESPONSE_TIME_MICROS} (微秒) %{RESPONSE_TIME_NANOS}(納秒)無(wú)ResponseTimeAttribute

注意:默認(rèn) undertow 沒(méi)有開(kāi)啟請(qǐng)求時(shí)間內(nèi)統(tǒng)計(jì),需要打開(kāi)才能統(tǒng)計(jì)響應(yīng)時(shí)間,如何開(kāi)啟呢?通過(guò)注冊(cè)一個(gè) WebServerFactoryCustomizer 到 Spring ApplicationContext 中即可。請(qǐng)看下面的代碼(項(xiàng)目地址:https://github.com/HashZhang/spring-cloud-scaffold/blob/master/spring-cloud-iiford/):

spring.factories(省略無(wú)關(guān)代碼)

# AutoConfiguration
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
    com.github.hashjang.spring.cloud.iiford.service.common.auto.UndertowAutoConfiguration

UndertowAutoConfiguration

//設(shè)置proxyBeanMethods=false,因?yàn)闆](méi)有 @Bean 的方法互相調(diào)用需要每次返回同一個(gè) Bean,沒(méi)必要代理,關(guān)閉增加啟動(dòng)速度
@Configuration(proxyBeanMethods = false)
@Import(WebServerConfiguration.class)
public class UndertowAutoConfiguration {
}

WebServerConfiguration

//設(shè)置proxyBeanMethods=false,因?yàn)闆](méi)有 @Bean 的方法互相調(diào)用需要每次返回同一個(gè) Bean,沒(méi)必要代理,關(guān)閉增加啟動(dòng)速度
@Configuration(proxyBeanMethods = false)
public class WebServerConfiguration {
    @Bean
    public WebServerFactoryCustomizer<ConfigurableUndertowWebServerFactory> undertowWebServerAccessLogTimingEnabler(ServerProperties serverProperties) {
        return new DefaultWebServerFactoryCustomizer(serverProperties);
    }
}

DefaultWebServerFactoryCustomizer

public class DefaultWebServerFactoryCustomizer implements WebServerFactoryCustomizer<ConfigurableUndertowWebServerFactory> {

    private final ServerProperties serverProperties;

    public DefaultWebServerFactoryCustomizer(ServerProperties serverProperties) {
        this.serverProperties = serverProperties;
    }

    @Override
    public void customize(ConfigurableUndertowWebServerFactory factory) {
        String pattern = serverProperties.getUndertow().getAccesslog().getPattern();
        // 如果 accesslog 配置中打印了響應(yīng)時(shí)間,則打開(kāi)記錄請(qǐng)求開(kāi)始時(shí)間配置
        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;
        }
        //判斷 accesslog 是否配置了查看響應(yīng)時(shí)間
        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);
    }
}

其他

還有安全相關(guān)的屬性(SSL 相關(guān),登錄認(rèn)證 Authentication 相關(guān)),微服務(wù)內(nèi)部調(diào)用一般用不到,我們這里就不贅述了。 其它內(nèi)置的屬性,在 Spring Boot 環(huán)境下一般用不到,我們這里就不討論了。

舉例

我們最開(kāi)始配置的 accesslog 的例子請(qǐng)求返回如下( JSON 格式化之后的結(jié)果):

{
	"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:8102",
	"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:07:50.410",
	"status": "200",
	"reason": "OK",
	"respHeaderUserSession": "testResponseHeaderUserSession",
	"respCookieUserId": "testResponseCookieUserId",
	"timeUsed": "3683ms, 3.683s, 3683ms, 3683149 us, 3683149200 ns",
}

上述就是小編為大家分享的如何配置accesslog以及accesslog 的各種占位符了,如果剛好有類(lèi)似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI