Download CloudHub Application Log Files Using CloudHub Public API

Mazhar Ansari
5 min readSep 14, 2020

Note: While copy/paste the code snippet from blog please covert quotes and double quotes.

CloudHub stores up to 100 MB of log data per application per worker, or up to 30 days, whichever limit is reached first. This is not applicable for clients who have Titanium subscription. For Titanium Subscription, you will get 200 GB per production core.

Objective of this blog to discuss a generic approach which can help clients to download application log files if they are not using any centralized logging server with Mulesoft.

MuleSoft Options to Overcome such issue?

  • Manually download log files
  • Anypoint CLI
  • Anypoint CloudHub Public API

In this blog we will discuss option # 3 and try to create scripted solutions for the same.

Anypoint CloudHub Public API:

  • The CloudHub Public API enables you to access application management services for applications deployed to CloudHub.
  • The Cloudhub Public API allows you to deploy and manage applications in CloudHub.
  • You can deploy your application, manage schedules and queues within an application, and view logs pertaining to an application.
  • You can also view memory and cpu usage and get statistics about the Mule messages sent using the application.

The CloudHub Public API can be accessed in 2 ways.

  • Simple REST Calls
  • CloudHub Public API Connector

CloudHub Public API Connector Installation:

  • Go to Anypoint Public Exchange Asset List Page
  • Search “CloudHub API
Anypoint Asset List
  • Click On Cloudhub API
  • Click On Download -> As Mule 4 Connector
  • Go to Anypoint Studio
  • Right Click on Project
  • Manager Dependencies -> Manager Modules
Manage Maven Dependency
  • Click on Add button and Select From Maven
Add Maven Dependency
  • Click on Install (Install a local dependency)
Install local repository
  • Browse the recently downloaded jar files for cloudhub public api
  • Click on Install
  • CloudHub Public API will be available in current project
Anypoint Studio Palette

Mule Project Overview:

Mule Flow
  • Get Accounts will get the Information about all environments under particular business organisation for given env
  • Get All applications will fetched all deployed and running applications for a particular environment
  • Payload will prepare the list of Domain Name (Application Name) and ID (Internal Instance ID of worker)
  • Download File call the Anypoint CloudHub REST API call to download the log file
  • If it’s successful we write it to local file system

logs-scheduler.xml:

This file has all main processing logic.

<?xml version=”1.0" encoding=”UTF-8"?><mule xmlns:file=”http://www.mulesoft.org/schema/mule/file"
xmlns:ee=”http://www.mulesoft.org/schema/mule/ee/core"
xmlns:cloudhub-public-api=”http://www.mulesoft.org/schema/mule/cloudhub-public-api"
xmlns:http=”http://www.mulesoft.org/schema/mule/http"
xmlns=”http://www.mulesoft.org/schema/mule/core"
xmlns:doc=”http://www.mulesoft.org/schema/mule/documentation"
xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=”http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/cloudhub-public-api http://www.mulesoft.org/schema/mule/cloudhub-public-api/current/mule-cloudhub-public-api.xsd
http://www.mulesoft.org/schema/mule/ee/core http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd
http://www.mulesoft.org/schema/mule/file http://www.mulesoft.org/schema/mule/file/current/mule-file.xsd">
<flow name=”logs-schedulerFlow”
doc:id=”1740d6d3–5963–40b4–8106-dbf7b3bc80e1">
<http:listener doc:name=”Listener”
doc:id=”96c228f9–6d6d-48ab-b959–5b96fdd0773b”
config-ref=”HTTP_Listener_config” path=”/logger” />
<cloudhub-public-api:get-accounts
doc:name=”Get accounts” doc:id=”eb0926d3–7f6f-4030-a792–182f4f9fa4f9"
config-ref=”Cloudhub_Public_API_Config”
x-anypnt-env-id=”#[p(‘anypoint.cloudhub.X-ANYPNT-ENV-ID’)]”
request-streaming-mode=”ALWAYS” />
<cloudhub-public-api:get-applications_2 doc:name=”Get All applications” doc:id=”eaff26b7–75c7–4819–89e7–19022e141610" config-ref=”Cloudhub_Public_API_Config” x-anypnt-env-id=”#[p(‘anypoint.cloudhub.X-ANYPNT-ENV-ID’)]” retrieve-statistics=”false”/>
<ee:transform doc:name=”payload”
doc:id=”6002d607-bfe7–41f0–8d4b-6e3c1a667c57">
<ee:message>
<ee:set-payload ><![CDATA[%dw 2.0
output application/json
— -
payload map (item, index) -> {
id: item.id,
domain: item.domain,
}]]></ee:set-payload>
</ee:message>
<ee:variables>
</ee:variables>
</ee:transform>
<async doc:name=”Async” doc:id=”a2351e8f-ebee-4800–88c6-ced1b7887412" >
<foreach doc:name=”For Each” doc:id=”d2067cf0–61cc-480d-b798–08aaaff142f7">
<logger level=”INFO” doc:name=”Logger” doc:id=”a9259611-d5e8–4486–8df6-c7c2a31bb706" message=”#[payload]”/>
<http:request method=”GET” doc:name=”downloadFile” doc:id=”92f9c48e-f836–4269-b839-b006251b20dd” config-ref=”HTTP_Request_configuration” path=”/{domain}/instances/{instanceId}/log-file” target=”downloadFileOutput”>
<http:headers><![CDATA[#[output application/java
— -
{
“X-ANYPNT-ENV-ID” : p(‘anypoint.cloudhub.X-ANYPNT-ENV-ID’)
}]]]></http:headers>
<http:uri-params><![CDATA[#[output application/java
— -
{
“domain” : payload.domain,
“instanceId” : payload.id
}]]]></http:uri-params>
</http:request>
<try doc:name=”Try” doc:id=”b6d6beb9–356c-4880-bb06–6826aef9fb44">
<file:write doc:name=”Write Log File to Local” doc:id=”7255a0dd-5a56–42f9–90b9–545641e30f13" config-ref=”File_Config” mode=”CREATE_NEW” path=”#[p(“anypoint.cloudhub.fileDir”) ++ “/” ++ payload.domain as String ++ ‘.’ ++ (now() as String {format: “yyyy_MM_dd_HH_mm_ss_SSS”}) ++ ‘.txt’]” >
<file:content ><![CDATA[#[vars.downloadFileOutput]]]></file:content>
</file:write>
<error-handler>
<on-error-continue enableNotifications=”true” logException=”true” doc:name=”On Error Continue” doc:id=”3b534f13–66e3–4c65–9ebd-ed5953746baa”>
<logger level=”INFO” doc:name=”Logger” doc:id=”8980c60c-aa42–4035-a224–4a92f77e0a4f” message=’#[“error in file”]’ />
</on-error-continue>
</error-handler>
</try>
</foreach>
</async>
</flow>
</mule>

Config.xml:

This file has all the configurations.

<?xml version=”1.0" encoding=”UTF-8"?><mule
xmlns:cloudhub-public-api=”http://www.mulesoft.org/schema/mule/cloudhub-public-api"
xmlns:http=”http://www.mulesoft.org/schema/mule/http"
xmlns:file=”http://www.mulesoft.org/schema/mule/file"
xmlns=”http://www.mulesoft.org/schema/mule/core"
xmlns:doc=”http://www.mulesoft.org/schema/mule/documentation"
xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=”http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/file http://www.mulesoft.org/schema/mule/file/current/mule-file.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/cloudhub-public-api http://www.mulesoft.org/schema/mule/cloudhub-public-api/current/mule-cloudhub-public-api.xsd">
<configuration-properties
doc:name=”Configuration properties”
doc:id=”69d47f7d-5100–4085-af16–6c65784d20f2"
file=”config.yaml” />
<http:listener-config name=”HTTP_Listener_config” doc:name=”HTTP Listener config” doc:id=”98dca629–08cc-4a2f-a0e2–9c46d9f9eae0" >
<http:listener-connection host=”0.0.0.0" port=”8081" />
</http:listener-config>
<cloudhub-public-api:config name=”Cloudhub_Public_API_Config” doc:name=”Cloudhub Public API Config” doc:id=”250e4aa0–3c5f-41ba-83f4–0eec7812182c” property_username=”${anypoint.cloudhub.username}” property_password=”${anypoint.cloudhub.password}” property_host=”${anypoint.cloudhub.host}” property_port=”${anypoint.cloudhub.port}” property_protocol=”${anypoint.cloudhub.protocol}” property_basePath=”${anypoint.cloudhub.basePath}”/>
<http:request-config name=”HTTP_Request_configuration” doc:name=”HTTP Request configuration” doc:id=”4a6d1f34–5c62–4a14–9bfc-8c5ef6098163" basePath=”/cloudhub/api/v2/applications” >
<http:request-connection protocol=”HTTPS” host=”${anypoint.cloudhub.host}” port=”${anypoint.cloudhub.port}” >
<http:authentication >
<http:basic-authentication username=”${anypoint.cloudhub.username}” password=”${anypoint.cloudhub.password}” />
</http:authentication>
</http:request-connection>
</http:request-config>
<file:config name=”File_Config” doc:name=”File Config” doc:id=”96efbbc4–266d-42da-8d39–3fe363438338" >
<file:connection workingDir=”#[${anypoint.cloudhub.fileDir}]” />
</file:config>
</mule>

config.yaml:

This property file has all configuration details.

anypoint:
cloudhub:
username: "mohdapi03"
password: "Q1w2e3r4"
host: "anypoint.mulesoft.com"
port: "443"
basePath: "/cloudhub/api"
protocol: "HTTPS"
X-ANYPNT-ENV-ID: "8844e555-eff7-415d-bb41-6050c3fb8510"
X-ANYPNT-ORG-ID: "f20be9f3-8ad3-4477-8943-d08e133a867a"
anypointDomain: "DEV"
fileDir: "E:\\LogFiles"

Execution:

Postman
  • Console output will look like this
Console Output
  • Folder will have following files
Output Folder

Note: This project can be modified and used as a scheduler based project.

--

--

Mazhar Ansari

I am seasoned Integration Architect with around 18+ yrs of exp. I have extensively worked on TIBCO and Mulesoft. Mainly in EAI, ESB, SOA, API and BPM projects.