How to Troubleshoot WSO2 Products — 2. How to log Message Payload in Request and Response with WSO2 API Manager

Maneesha Wijesekara
2 min readJul 9, 2021

Hi Everyone,

As the 2nd story on troubleshooting WSO2 products, I have thought to give some tips on message mediation with WSO2 API Manager.

As the heading suggests, logging request and response message payloads would definitely a plus point when troubleshooting the message mediation flow.

The proposed solution consists of a couple of custom mediation extensions for inflow and outflow with a logger mediator which prints the message body.

One benefit from this would be, you can enable this ‘per API’ or make it as a global configuration available for all the APIs easily.

Enable it as ‘Per API’

  • By following the documentation [1], create a couple of sequences as below,

Sequence 1

<sequence xmlns="http://ws.apache.org/ns/synapse" name="log_in_message">
<log level="full">
<property name="RequestBody" value="IN_MESSAGE"/>
</log>
</sequence>

Sequence 2

<sequence xmlns="http://ws.apache.org/ns/synapse" name="log_out_message">
<log level="full">
<property name="ResponseBody" value="OUT_MESSAGE"/>
</log>
</sequence>
  • Save the first sequence as inflow.xml and second one as outflow.xml.
  • Then edit the API and follow [2] and upload ‘inflow.xml’ for ‘In Flow’ and ‘outflow.xml’ for ‘Out Flow’ under ‘Message Mediation Policies’ and publish the API.
  • When you invoke the respective API, you can see the message body for request and response in the log.

Enable it as ‘Globally’

If you want to make this globally without configuring each and every API in your environment, you can use this solution by exposing as a global extension.

  • As per [2], we can create the sequence with the name as ‘WSO2AM — Ext — In’ and ‘WSO2AM — Ext — Out’.

Sequence 1

<sequence xmlns="http://ws.apache.org/ns/synapse"  name="WSO2AM--Ext--In">     
<log level="full">
<property name="RequestBody" value="IN_MESSAGE"/>
</log>
</sequence>

Sequence 2

<sequence xmlns="http://ws.apache.org/ns/synapse"  name="WSO2AM--Ext--Out">     
<log level="full">
<property name="ResponseBody" value="OUT_MESSAGE"/>
</log>
</sequence>
  • Save the first sequence as inflow.xml and second one as outflow.xml and added to <API_Gateway>/repository/deployment/server/synapse-configs/default/sequences directory.
  • Then restart the server.

After that for each API, you can see the response and request body as below,

[2021-04-21 21:32:42,642]  INFO - LogMediator To: /manee/1.0.0/order, MessageID: urn:uuid:b46528b3-cc65-4f73-b030-2be8ba4b1a5e, Direction: request, RequestBody = IN_MESSAGE, Envelope: <?xml version='1.0' encoding='utf-8'?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><soapenv:Body><jsonObject><customerName>string</customerName><delivered>true</delivered><address>string</address><pizzaType>string</pizzaType><creditCardNumber>string</creditCardNumber><quantity>0</quantity><orderId>string</orderId></jsonObject></soapenv:Body></soapenv:Envelope>[2021-04-21 21:32:42,666]  INFO - LogMediator To: http://www.w3.org/2005/08/addressing/anonymous, WSAction: , SOAPAction: , MessageID: urn:uuid:4838e13e-ca7a-4686-8d73-208136d09494, Direction: response, ResponseBody = OUT_MESSAGE, Envelope: <?xml version='1.0' encoding='utf-8'?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><soapenv:Body><jsonObject><orderId>7ed9b351-4518-4002-8bc8-6ef37f2ec7c6</orderId><pizzaType>string</pizzaType><quantity>0</quantity><customerName>string</customerName><creditCardNumber>string</creditCardNumber><address>string</address><delivered>true</delivered></jsonObject></soapenv:Body></soapenv:Envelope>

As you can see you can filter the request and response with a log analyzed such as EFK with the strings we have added in extensions as ‘RequestBody’ and ‘ResponseBody’. These strings can be changed as per your wish by updating the sequences.

Hope this will help you to troubleshoot issues which requires to examine the request and response bodies.

Adiós until I come back with another interesting way of troubleshooting WSO2 products.

[1] https://docs.wso2.com/display/AM260/Adding+Mediation+Extensions
[2] https://docs.wso2.com/display/AM260/Adding+Mediation+Extensions#AddingMediationExtensions-CreatinganduploadingmanuallyintheAPIPublisher

--

--

Maneesha Wijesekara

Certified WSO2 Engineer, BSc in ICT, CTFL in QA, CIM Base for Marketing and HEQ in BCS