You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在WSO2 API Manager的API网关HTTP访问日志中记录应用名称(App Name)

How to Add App Name to WSO2 APIM HTTP Access Logs

Alright, let's walk through adding the App Name to your WSO2 API Manager's HTTP access logs. The official docs cover setting up custom access logs but don't mention this specific tweak, so here's a practical guide for most recent APIM versions (3.2.0+, 4.x, 5.x):

Step 1: Ensure App Name is Available in MDC

WSO2 APIM's API Gateway stores the application name in the message context once a request is authenticated (via OAuth2, API keys, etc.) under the property api.ut.application.name. To make this accessible to log4j2, we need to push this value into the Mapped Diagnostic Context (MDC)—the storage log4j uses for dynamic context variables.

You have two ways to do this:

  1. Create a simple Java class that implements org.apache.synapse.rest.AbstractHandler to handle setting and clearing the MDC entry:
    import org.apache.synapse.MessageContext;
    import org.apache.synapse.rest.AbstractHandler;
    import org.slf4j.MDC;
    
    public class AppNameMDCHandler extends AbstractHandler {
    
        @Override
        public boolean handleRequest(MessageContext messageContext) {
            String appName = (String) messageContext.getProperty("api.ut.application.name");
            // Fallback to "Anonymous" for unauthenticated requests
            MDC.put("api.ut.application.name", appName != null ? appName : "Anonymous");
            return true;
        }
    
        @Override
        public boolean handleResponse(MessageContext messageContext) {
            // Clean up MDC to prevent value leakage across requests
            MDC.remove("api.ut.application.name");
            return true;
        }
    }
    
  2. Compile this class using WSO2's core dependencies (synapse-core, slf4j-api) and package it into a JAR file.
  3. Place the JAR in <APIM_HOME>/repository/components/lib/.
  4. Register the handler in your APIM config:
    Open <APIM_HOME>/repository/conf/api-manager.xml, find the <RESTAPIHandlers> section, and add:
    <handler class="com.your.package.AppNameMDCHandler"/>
    

Option B: Use a Script Mediator (Quick Test)

If you want to test without writing Java code, add a Script Mediator to your main sequence:

  1. Open <APIM_HOME>/repository/deployment/server/synapse-configs/default/sequences/main.xml and insert this early in the sequence:
    <script language="js">
        var appName = mc.getProperty("api.ut.application.name");
        org.slf4j.MDC.put("api.ut.application.name", appName || "Anonymous");
    </script>
    
  2. Add a cleanup script to the fault-sequence and out-sequence to clear the MDC entry:
    <script language="js">
        org.slf4j.MDC.remove("api.ut.application.name");
    </script>
    

Step 2: Update the Access Log Pattern

Now, modify the log4j2 configuration to include the App Name in your access logs:

  1. Open <APIM_HOME>/repository/conf/log4j2.properties.
  2. Locate the appender.ACCESS_LOG.layout.pattern property. This defines your access log format.
  3. Insert %X{api.ut.application.name} wherever you want the App Name to appear. For example:
    appender.ACCESS_LOG.layout.pattern = %d{ISO8601} [%X{api.ut.application.name}] %X{remoteAddr} %X{host} "%m" %n
    
    The %X{key} syntax pulls the value from MDC.

Step 3: Restart and Test

  1. Restart your WSO2 APIM server.
  2. Send an authenticated request to one of your APIs (using an app's key/token).
  3. Check the access log at <APIM_HOME>/repository/logs/access.log—you should see the App Name included in each log entry.

Notes:

  • For unauthenticated requests, the log will show "Anonymous" (or whatever fallback value you set).
  • Ensure api.ut.application.name is populated: this property is only set after successful authentication. If you're using custom authentication flows, verify the property is being added to the message context.

内容的提问来源于stack exchange,提问作者mohammad amin rahimi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.01 01:22:39