Hello folks ! I hope you all are doing well. So, today I am here with a new and interesting topic for you all. In this blog we’ll see how we can get the Gateway Logs using Logs Explorer in apigee. Apigee API Gateway logs can provide valuable insights into the behaviour and performance of your APIs.
So, let’s get started !!!
Introduction
As part of the Next Gen API gateway platform migration project, we need to get Apigee API Gateway call logs Using the Logs Explorer for better observability. Apigee API Gateway logs can provide valuable insights into the behaviour and performance of your APIs. This guide will walk you through the process of enabling message logging in shared flows and flow hooks and retrieving those logs using Logs Explorer.
Prerequisites
Before you begin, make sure you have the following:
- Access to the Apigee Management UI or API
- Permissions to create or modify shared flows and flow hooks
- Access to the Cloud Console for the project associated with your Apigee organisation
- Service Account Access for the role – roles/logging.logWriter for the service account we are using to add the message logging policy..
- Familiarity with Logs Explorer and querying logs in the Cloud Logging interface
Enabling Message Logging in Shared Flows
Shared Flow:Shared flow is a reusable component which can be created at one place and used by multiple proxies during their execution flow as long as the proxies and shared flow lie in the same environment.

To create the Message-Logging Policy(Sample-Message-Logging-Policy). Go to Apigee Management UI and navigate to the Shared Flows section as above.
- Under Develop in Apigee, select the Shared Flows tab.
- Click Create New, enter the name (Sample-Message-Logging-Policy), and click create.
- Under the Develop tab, click + under policies.
- Scroll down, and select Message Logging under the Extension group.
- Set the name and display name to ML-CloudLogging Policy.
- From the Script File option, select Import new script.
- Select Choose File and select the Sample-Message-Logging-Policy.xml file from the decompressed policy package.
- When complete, select Add (no extra configuration is needed for this policy)
- In the shared flow, under Shared Flows click default.
- Click + to add a policy step and choose the existing policy ML-CloudLogging.
- Once finished, click Deploy to deploy the request shared flow and also save the changes to the shared flow.
Add the following policy to the policy or step configuration
![Global Integration Homepage > [WIP] Apigee API Gateway Logs using Logs Explorer > Screenshot from 2023-05-17 15-44-16.png](https://i0.wp.com/i.postimg.cc/HxjcVrdr/Screenshot-from-2023-05-17-15-44-16.png?w=1300&ssl=1)
Message Logging Policy file
Replace the below xml file with the sample cloudlogging.xml in order to the the apigee API gateway call logs.
<? version="1.0" encoding="UTF-8" standalone="yes"?>
<MessageLogging continueOnError="false" enabled="true" name="ML-CloudLogging">
<DisplayName>ML-CloudLogging</DisplayName>
<CloudLogging>
<LogName>projects/{organization.name}/logs/apigee</LogName>
<Message contentType="application/json">{
"organization": "{organization.name}",
"environment": "{environment.name}",
"proxy": "{apiproxy.name}",
"verb": "{request.verb}",
"response.code":"{response.status.code}",
"response.reason":"{message.reason.phrase}",
"response.content":"{escapeJSON(message.content)}",
"literal_value":"Hi! I am a literal value",
"OrgName": "{organization.name}",
"Proxy_Name": "{apiproxy.name}",
"Environment_Name": "{environment.name}",
"User_Agent": "{request.header.User-Agent.values}",
"Host": "{request.header.Host}",
"Proxy_URL": "{proxy.url}",
"API_Proxy_Revision": "{apiproxy.revision}",
"Target_IP": "{target.ip}",
"Target_Host": "{target.host}",
"Target_URL": "{target.url}",
"Target_Endpoint_Name": " {route.target}",
"Proxy_Base_Path": "{proxy.basepath}",
"Proxy_IP": "{proxy.client.ip}",
}</Message>
</CloudLogging>
</MessageLogging>
you can add the variables you want to see the logs for Click Here for the variables list.
Adding Shared Flows to Flow Hooks
Flow Hooks: “Flow Hooks” are pre-defined points within the request/response flow of an API proxy where you can inject custom policies or logic. These hooks allow you to extend the functionality of the API proxy and perform additional processing or modifications.
![Global Integration Homepage > [WIP] Apigee API Gateway Logs using Logs Explorer > Screenshot from 2023-05-17 16-07-33.png](https://i0.wp.com/i.postimg.cc/SsjVLLWq/Screenshot-from-2023-05-17-16-07-33.png?w=1300&ssl=1)
- In the Apigee Management UI, under Environment, select the Flow Hooks.
- choose “PreFlow” or “PostFlow” depending on where you want to add the shared flow.
- Click on the “Edit” button to add a Shared Flow in which your policy is attached.
- Select “Shared Flow” from the drop-down menu and choose the shared flow you modified in the previous section.
- Save the changes to the API Proxy.
![Global Integration Homepage > [WIP] Apigee API Gateway Logs using Logs Explorer > Screenshot from 2023-05-17 16-10-34.png](https://i0.wp.com/i.postimg.cc/QCQ7cVnh/Screenshot-from-2023-05-17-16-10-34.png?w=1300&ssl=1)
Retrieving Apigee API Gateway Logs using Logs Explorer
- Go to the Cloud Console for the project associated with your Apigee organisation.
- Navigate to the Logs Explorer page.
- Use the following filter in the query editor to retrieve the Apigee API Gateway logs:
logName="projects/[PROJECT_ID]/logs/apigee"
Replace [PROJECT_ID]
with your actual Cloud project ID.
- Optionally, you can further refine the query using additional filters like time range, severity level, or specific APIs.
- Click on the “Run Query” button to execute the query and retrieve the logs.
You will see the output as below:
![Global Integration Homepage > [WIP] Apigee API Gateway Logs using Logs Explorer > Screenshot from 2023-05-17 16-29-12.png](https://i0.wp.com/i.postimg.cc/vZgQKCb6/Screenshot-from-2023-05-17-16-28-48.png?w=1300&ssl=1)
![Global Integration Homepage > [WIP] Apigee API Gateway Logs using Logs Explorer > Screenshot from 2023-05-17 16-28-48.png](https://i0.wp.com/i.postimg.cc/hPhgJNJ3/Screenshot-from-2023-05-17-16-29-12.png?w=1300&ssl=1)
So this is how you can get the Apigee API Gateway Logs using the Logs Explorer.
Conclusion
So, in this blog you have seen How to get Apigee API Gateway Logs using Logs Explorer. By following the steps outlined in this guide, you can enable message logging in shared flows and flow hooks in Apigee API Gateway and retrieve the logs using Logs Explorer. Analysing these logs can help you gain insights into the behaviour and performance of your APIs, enabling you to identify and address any issues effectively. For more information feel free to contact me.
Keep Learning !!!
Keep Growing !!!
Thank You 🙂
Reference
https://github.com/GoogleCloudPlatform/apigee-samples/tree/main/cloud-logging