【问题标题】:Customize HTTP access log in WSO2 API Manager 3.2.0 for custom request headers在 WSO2 API Manager 3.2.0 中为自定义请求标头自定义 HTTP 访问日志
【发布时间】:2022-12-09 21:48:11
【问题描述】:

我们按照以下文档在 HTTP 访问日志中打印自定义请求标头,但面临一些挑战

网址: https://apim.docs.wso2.com/en/3.2.0/administer/logging-and-monitoring/logging/monitoring-http-access-logs/


**Option 1**: We added below entries in /repository/conf/deployment.toml file
[http_access_log]
useLogger = true
pattern = "%{X-Forwarded-For}i %h %l %u %t %r %s %b %{Referer}i %{User-Agent}i %{X-Custom-Header}i %T"

The changes are reflecting in HOME\repository\conf\tomcat\catlina-server.xml but the custom headers are not getting printed.

**Option 2**: Created a separate log file as mentioned in "Configuring access logs for PassThrough or NIO transports in API Gateway" section. With this we are able to print the custom headers but the response code is not getting printed. It is appearing as "-". 


Log format: access_log_pattern=%{X-Forwarded-For}i %h %l %u %t %r %s \"%{Referer}i\" \"%{User-Agent}i\" %{X-Custom-Header}i %B
Example log entry: - 10.73.122.48 - - [06/Dec/2022:14:49:36.628 +0530] "GET /mcm-stubs/testservice HTTP/1.1" **-** "-" "Synapse-PT-HttpComponents-NIO" "-" -

请帮忙

【问题讨论】:

    标签: wso2 wso2-api-manager


    【解决方案1】:

    选项 2 是记录 API 流量的正确方法。如果您按照 [1] 中的步骤进行操作,对于每个请求, http_gw_<date>.log 文件中将打印四行日志。这些将基于,

    1. 网关收到请求
    2. 请求传递到后端
    3. 从后端收到响应
    4. 从网关响应

      您将无法在第一行和第二行日志中记录响应代码,因为此时服务器仍未收到响应。

      如果您的要求是记录从网关收到的自定义标头,您可以使用下面的调解器轻松实现此目的。

      <log level="custom">
           <property name="Header" expression="get-property('transport','X-Custom-Header')"/>
      </log> 
      

      [1]-https://apim.docs.wso2.com/en/3.2.0/administer/logging-and-monitoring/logging/monitoring-http-access-logs/#configuring-access-logs-for-passthrough-or-nio-transports-in-api-gateway

    【讨论】:

    【解决方案2】:

    API Manager 中有两组访问日志。一个用于 Servlet 组件(Store/Publisher 等,您通过端口 9443 和 9763 访问的内容),第二个用于直通传输(通过 8280 和 8243 访问)。 Sevlet 访问日志由 Tomcat 值处理,这些配置可以通过更改 deployment.toml 来更改,这将反映在 catlina-server.xml 中。这些日志将被写入一个单独的文件。

    Passthrough 传输是您将接收实际 API 流量并更改格式的地方。首先,将记录器配置添加到&lt;APIM_HOME&gt;/repository/conf/log4j2.properties

    # Add the new logger to this line
    loggers = PassThroughAccess, AUDIT_LOG, SERVICE_LOGGER, trace-messages,..........
    
    # Define the new logger
    logger.PassThroughAccess.name = org.apache.synapse.transport.http.access
    logger.PassThroughAccess.level = INFO
    

    然后在&lt;APIM_HOME&gt;/repository/conf目录下新建一个名为access-log.properties的文件,内容如下。

    # Default access log pattern examples
    #access_log_pattern=%{X-Forwarded-For}i %h %l %u %t ”%r” %s %b ”%{Referer}i” ”%{User-Agent}i”
    #access_log_pattern=time=%t remoteHostname=%h localPort=%p localIP=%A requestMethod=%m requestURL=%U remoteIP=%a requestProtocol=%H HTTPStatusCode=%s queryString=%q
    
    # combinded log pattern
    access_log_pattern=%h %l %u %t "%r" %s %b "%{Referer}i" "%{User-Agent}i" "%{X-Custom-Header}i"
    
    # file prefix
    access_log_prefix=http_gw
    # file suffix
    access_log_suffix=.log
    # file date format
    access_log_file_date_format=yyyy-MM-dd
    

    现在重新启动服务器,当您访问 API 时,访问日志将记录到一个新文件中,该文件的名称与您在上述配置中提供的名称相同。使用上面的配置日志将被写入&lt;APIM_HOME&gt;/repository/logs/http_gw.log


    更新

    因为这是一个响应头。您需要使用以下模式来获取标头。

    # To get a custom response Header
    %{X-Custom-Header}o
    
    # TO Get response code
    %s
    
    # A complete pattern
    access_log_pattern=%h %l %u %t "%r" %s %b "%{User-Agent}i" customHeader=%{X-Custom-Header}o responseCode=%s
    

    【讨论】:

    • 是的,我们做了相同的更改,但问题是它没有记录响应代码。有没有更好的方法来记录客户标头和响应代码?
    • @PradeepKumar 自定义标头是响应标头还是请求标头?
    • 这是一个请求标头。要求是使用自定义请求标头 ID 跟踪请求。
    • 您可以将stackoverflow.com/a/74703690/11414612 中提到的中介序列添加到请求流中。
    猜你喜欢
    • 2023-03-21
    • 1970-01-01
    • 2014-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-04
    相关资源
    最近更新 更多