如何在WSO2 API Manager的API网关HTTP访问日志中记录应用名称(App Name)
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:
Option A: Use a Custom Handler (Recommended for Persistence)
- Create a simple Java class that implements
org.apache.synapse.rest.AbstractHandlerto 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; } } - Compile this class using WSO2's core dependencies (synapse-core, slf4j-api) and package it into a JAR file.
- Place the JAR in
<APIM_HOME>/repository/components/lib/. - 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:
- Open
<APIM_HOME>/repository/deployment/server/synapse-configs/default/sequences/main.xmland 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> - Add a cleanup script to the
fault-sequenceandout-sequenceto 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:
- Open
<APIM_HOME>/repository/conf/log4j2.properties. - Locate the
appender.ACCESS_LOG.layout.patternproperty. This defines your access log format. - Insert
%X{api.ut.application.name}wherever you want the App Name to appear. For example:
Theappender.ACCESS_LOG.layout.pattern = %d{ISO8601} [%X{api.ut.application.name}] %X{remoteAddr} %X{host} "%m" %n%X{key}syntax pulls the value from MDC.
Step 3: Restart and Test
- Restart your WSO2 APIM server.
- Send an authenticated request to one of your APIs (using an app's key/token).
- 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.nameis 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

