📖 Scenario: You are managing a Google Cloud project and want to keep track of important events and errors happening in your cloud resources. Cloud Logging helps you collect and view these logs in one place.
🎯 Goal: Learn how to create a log entry, set a filter to find specific logs, and display the filtered logs using Google Cloud Logging commands.
📋 What You'll Learn
Create a log entry with a specific name and message
Set a filter string to find logs with a certain severity
Use the gcloud command to list logs matching the filter
Display the filtered log entries
💡 Why This Matters
🌍 Real World
Cloud Logging helps cloud engineers and developers monitor their applications and infrastructure by collecting logs from various cloud services in one place.
💼 Career
Knowing how to create, filter, and view logs is essential for troubleshooting, auditing, and maintaining cloud systems in roles like Cloud Engineer or DevOps Specialist.
Progress0 / 4 steps
1
Create a log entry
Use the gcloud logging write command to create a log entry named my-log with the message "Hello Cloud Logging" and severity INFO.
GCP
Hint
Use gcloud logging write LOG_NAME MESSAGE --severity=SEVERITY format.
2
Set a filter for severity
Create a variable called filter and set it to the string "severity=INFO" to filter logs with INFO severity.
GCP
Hint
Assign the string exactly as "severity=INFO" to the variable filter.
3
List logs using the filter
Use the gcloud logging read command with the filter variable filter to list log entries that have severity INFO.
GCP
Hint
Use gcloud logging read "$filter" to apply the filter variable.
4
Display the filtered logs
Run the command to display the filtered logs and verify that the log entry with message "Hello Cloud Logging" appears in the output.
GCP
Hint
Check the output for the message Hello Cloud Logging.
Practice
(1/5)
1. What is the main purpose of Cloud Logging in Google Cloud Platform?
easy
A. To store large files in the cloud
B. To create virtual machines automatically
C. To manage user permissions and roles
D. To collect and display logs from cloud resources in one place
Solution
Step 1: Understand Cloud Logging's role
Cloud Logging collects logs from various cloud resources and shows them centrally.
Step 2: Compare options with this role
Only To collect and display logs from cloud resources in one place describes collecting and displaying logs; others describe unrelated services.
Final Answer:
To collect and display logs from cloud resources in one place -> Option D
Quick Check:
Cloud Logging = centralized log collection [OK]
Hint: Cloud Logging is about logs, not VMs or storage [OK]
Common Mistakes:
Confusing logging with VM creation
Thinking it manages user permissions
Mixing it up with storage services
2. Which of the following is the correct gcloud command to read logs from Cloud Logging?
easy
A. gcloud logging read "resource.type=gae_app"
B. gcloud compute instances list
C. gcloud storage buckets create my-bucket
D. gcloud iam roles describe admin
Solution
Step 1: Identify the command for reading logs
The command to read logs uses 'gcloud logging read' followed by a filter.
Step 2: Check each option
gcloud logging read "resource.type=gae_app" uses 'gcloud logging read' correctly; others relate to compute, storage, or IAM.
Final Answer:
gcloud logging read "resource.type=gae_app" -> Option A
Quick Check:
Log reading command starts with 'gcloud logging read' [OK]
Hint: Look for 'logging read' in the command to find logs [OK]
Common Mistakes:
Using compute or storage commands instead of logging
Missing the 'read' keyword after 'logging'
Confusing IAM commands with logging commands
3. What will be the output of this command? gcloud logging read "severity=ERROR" --limit 2 --format json
medium
A. A JSON list of the 2 most recent ERROR severity log entries
B. A list of all logs regardless of severity
C. An error message about invalid command syntax
D. A list of 2 logs with severity INFO
Solution
Step 1: Understand the command parameters
The command reads logs filtered by severity=ERROR, limits output to 2 entries, and formats output as JSON.
Step 2: Analyze expected output
It returns a JSON list of the 2 most recent logs with ERROR severity, not all logs or INFO logs.
Final Answer:
A JSON list of the 2 most recent ERROR severity log entries -> Option A
Hint: Filter by severity and limit controls output count [OK]
Common Mistakes:
Ignoring the severity filter
Expecting all logs instead of limited ones
Confusing severity levels
4. You run this command but get no output: gcloud logging read "severity=CRITICAL" --limit 5 What is the most likely reason?
medium
A. The command syntax is incorrect
B. There are no logs with severity CRITICAL in the selected time range
C. The gcloud tool is not installed
D. The limit flag cannot be used with severity filters
Solution
Step 1: Check command syntax
The command syntax is correct; 'gcloud logging read' supports severity filters and limit.
Step 2: Consider reasons for no output
No output usually means no matching logs exist for severity CRITICAL in the default time range.
Final Answer:
There are no logs with severity CRITICAL in the selected time range -> Option B
Quick Check:
No output often means no matching logs [OK]
Hint: No output? Check if logs exist for that severity [OK]
Common Mistakes:
Assuming syntax error without checking
Thinking limit flag is invalid with filters
Ignoring that no logs might exist
5. You want to monitor your app's logs for errors and send alerts only when ERROR or higher severity logs appear. Which Cloud Logging feature should you use?
hard
A. Create a VM instance to process logs locally
B. Use Cloud Storage to save logs and check manually
C. Create a log-based metric with a filter for severity >= ERROR and set up an alerting policy
D. Run gcloud logging read manually every hour
Solution
Step 1: Identify monitoring and alerting needs
You want automatic alerts on ERROR or higher severity logs.
Step 2: Match features to needs
Log-based metrics with filters and alerting policies automate monitoring and alerts.
Step 3: Evaluate other options
Manual checks or storing logs without alerts do not meet the requirement.
Final Answer:
Create a log-based metric with a filter for severity >= ERROR and set up an alerting policy -> Option C