With the standard component HTTP-Request, Mule ESB offers an effective way to consume REST APIs within Mule applications. Concepts such as API-led Connectivity and the growing number of external APIs promote the networking of applications and thus increase the number of communication endpoints. In this network of applications, business processes, and interfaces, the need to log the communication between systems and partners arises quickly.
Mule HTTP Request and Response Logger
Mule is based on the Java logging framework Apache Log4j2 and enables the logging of HTTP requests and responses by setting the log level DEBUG for the loggers HttpMessageLogger and com.ning.http. In addition to the Log4j2 configuration file log4j2.xml, the Anypoint Platform lets you configure loggers through the web interface.
<Loggers>
<AsyncLogger name="org.mule.module.http.internal.HttpMessageLogger" level="DEBUG" />
<AsyncLogger name="com.ning.http" level="DEBUG" />
</Loggers>The loggers included in Mule ESB then log all HTTP requests and responses. In our case, the query of the REST API resource GET https://api.dataliquid.com/system/api/users/1 is logged at the log level DEBUG.
The Grizzly HTTP Client used in the Mule HTTP module logs the HTTP request to be executed with the logger com.ning.http.client.providers.grizzly.AsyncHttpClientFilter.
REQUEST: HttpRequestPacket (
method=GET
url=/system/api/users/1
query=null
protocol=HTTP/1.1
content-length=-1
headers=[
Host=api.dataliquid.com:443
User-Agent=AHC/1.0
Connection=keep-alive
Accept=*/*]
)Mule HTTP Request
The Mule HTTP message logger org.mule.module.http.internal.HttpMessageLogger records the outgoing HTTP Request.
GET /system/api/users/1 HTTP/1.1
Host: api.dataliquid.com:443
User-Agent: AHC/1.0
Connection: keep-alive
Accept: */*
Content-Type: application/jsonMule HTTP Response
The incoming HTTP Response is likewise logged by the logger org.mule.module.http.internal.HttpMessageLogger.
HTTP/1.1 200 OK
Date: Thu, 13 Sep 2018 17:34:39 GMT
Content-Type: application/json; charset=utf-8
Content-Length: 83
Connection: keep-alive
Vary: Origin, Accept-Encoding
Access-Control-Allow-Credentials: true
Cache-Control: public, max-age=14400
Pragma: no-cache
Expires: Thu, 13 Sep 2018 19:34:39 GMT
{
"id": 1,
"user_id": 1,
"username": "jdoe"
}Conclusion
With the loggers presented here, the Mule framework makes it possible to log the exchanged HTTP communication in the log file. Because of the increased log volume, however, the storage requirement can quickly exceed the storage capacity provided. In addition, a log management system is recommended so that the log messages can still be evaluated clearly when the logs are written to at a high rate.
As shown, the log level DEBUG logs all information of the HTTP communication. This can include security-relevant information (such as client ID, secrets, or credentials) or sensitive data (such as customer details or credit card information). The log levels should therefore be activated only when needed for analysis or in the event of an error.