Introduction
In the previous blog we had looked at the details of Cloud Logging in GCP along with details of Access control with IAM, Audit logs, and details of Google Cloud Logs. This blog explains the details of Cloud Logging in GCP along with detailed steps for writing query log entries with the gcloud CLI, using python script and Cloud Logging for Compute Engine VMs .
Without further ado , let's get started.
Write and query log entries with the gcloud CLI
Let's get started with writing the log entries with the gcloud CLI.
Prerequisites
To finish this quickstart, you need a Google Cloud project that has billing enabled. If you don't have a cloud project or your cloud project doesn't have billing enabled, take the following actions:
-
Create an account if you're new to Google Cloud to see how well our products work in practical situations. Additionally, new users receive $300 in complimentary credits to run, test, and deploy workloads.
-
Choose or create a Google Cloud project from the project selector page in the Google Cloud dashboard.
-
Make sure your Cloud project's billing is enabled. Find out how to determine whether billing is enabled for a project.
Getting started
A set of commands called gcloud logging in the gcloud CLI offers a command-line interface to the Cloud Logging API.In this quickstart, the gcloud CLI commands can be executed using the Cloud Shell environment. In the Cloud Shell environment, the gcloud CLI is already installed.
Make sure the correct Cloud project is being used by the gcloud CLI and follow the following steps:
-
Click Activate Cloud Shell in the Google Cloud console.
-
The Cloud Shell launches in a window and shows a greeting.
- Run the following command, substituting PROJECT ID for the project ID of the Cloud project you want to use in place of the one listed in the welcome message:
gcloud config set project PROJECT_ID
Write log entries by using the gcloud CLI
Logging enables both organised and unstructured data in log entries. JSON data structures make up structured data, such as "weather": "Sunny." A string of text, such as "The first entry," is an example of unstructured data. The gcloud CLI is used in the following steps to create log entries with unstructured data and structured data:
- Create a log entry in the my-test-log containing unstructured data as follows:
gcloud logging write my-test-log "The first entry."
You see the following message when the command has finished: Created log entry
- Add a structured log entry to the log called "my-test-log" like follows:
gcloud logging write --payload-type=json my-test-log '{ "message": The second entry", "weather": "Sunny"}'
Structured data log entries must contain the —payload-type=json flag. Logging treats the payload as unstructured data if this field is left blank.
Let's look at the procedure to list long entries by using the gcloud CLI.
List log entries by using the gcloud CLI
Using the gcloud CLI, you can get log entries from Logging and display them. Run the following command, for instance, to retrieve and display log entries with a resource type of global:
gcloud logging read "resource.type=global"
Let's look at the procedure to list long entries by using APIs Explorer.
List log entries by using APIs Explorer
Refer to Using the APIs Explorer to execute Logging API calls without creating any code. Do the following to read a list of log items from Logging:
-
Visit the entries.list API method reference page here.
- Set up and execute the API command:
The text after PROJECT ID should be changed:
"resourceNames": [
"projects/PROJECT_ID"
],
"filter": "resource.type=global",
"orderBy": "timestamp desc"
-
Copy the revised content from the previous step, and then paste it into APIs Explorer's Request body area.
-
Choose Execute.
Let's look at the procedure to view long entries.
View log entries in the Logs Explorer
You can use the Logs Explorer to view log entries in the Google Cloud console. The majority of Cloud projects keep a lot of logs; you can choose certain log entries by writing a query.
Follow these steps to view the log entries you made using the Logs Explorer:
-
Logs Explorer may be found in the Google Cloud console.
-
In the Google Cloud navigation bar, make sure your Cloud project is selected. Select your Cloud project using the drop-down menu, if necessary.
-
Go to the Resource menu and choose Global.
-
Wait a few minutes, then refresh the website if you don't see the Global menu item or your log entries. The time it takes for Logging to receive log entries varies.
-
Click the Global Menu right of a log entry to access its information.
The information for the initial log entry is kept in textPayload. Structured data that is saved in jsonPayload is found in the second log entry. The weather and the key message are both included in the structured payload.
Let's look at the procedure to query long entries.
Query log entries in the Logs Explorer
Using the query editor and, with structured logs, by the key and value, you can query log entries. For instance, carry out the following actions to show all log entries that include the word simple:
-
Go to the Logs Explorer in the Google Cloud console.
-
Go to the Resource menu and choose Global.
-
Enter the string simple in quotation marks in the query editor. Only the log entry is displayed in the logs list : A simple entry.
-
Remove the query string you added and click Run query after you have seen your log. Both log entries are displayed again.
Do the following to display all structured data log entries with weather as the key and partly in the value field:
- The line resource is present in the query editor. type="global". Input the command as follows:
jsonPayload.weather:partly
-
Click Run search.
The outcome is the single log entry : The Second Entry.
Lets dive into the details of write and query log entries using python script.