0
0
GcpHow-ToBeginner · 4 min read

How to View Logs in Cloud Logging in GCP Quickly

To view logs in Cloud Logging on GCP, open the Google Cloud Console, navigate to Logging > Logs Explorer, and use filters to find your logs. Alternatively, use the gcloud logging read command in the terminal to fetch logs programmatically.
📐

Syntax

The main ways to view logs in GCP Cloud Logging are:

  • Google Cloud Console: Navigate to Logging > Logs Explorer to search and filter logs visually.
  • gcloud CLI command: Use gcloud logging read with filters to fetch logs from the terminal.

Example syntax for CLI:

gcloud logging read "resource.type=RESOURCE_TYPE" --limit=NUMBER --order=desc

Where:

  • resource.type specifies the resource generating logs (e.g., gce_instance for VM logs).
  • --limit limits the number of log entries returned.
  • --order sorts logs by time, desc for newest first.
bash
gcloud logging read "resource.type=RESOURCE_TYPE" --limit=10 --order=desc
💻

Example

This example shows how to view the last 5 logs from a Compute Engine VM instance using the gcloud CLI.

bash
gcloud logging read "resource.type=gce_instance" --limit=5 --order=desc
Output
2024-06-01T12:34:56.789Z gce_instance: Instance started 2024-06-01T12:33:45.123Z gce_instance: SSH session opened 2024-06-01T12:32:10.456Z gce_instance: System update completed 2024-06-01T12:30:00.000Z gce_instance: Instance stopped 2024-06-01T12:29:00.000Z gce_instance: Instance started
⚠️

Common Pitfalls

Common mistakes when viewing logs in GCP Cloud Logging include:

  • Not specifying the correct resource.type filter, resulting in no logs found.
  • Using overly broad or no filters, which can return too many logs and make it hard to find relevant entries.
  • Not setting the time range properly in the Logs Explorer, missing recent logs.
  • Confusing gcloud logging read syntax by omitting quotes or using incorrect filter expressions.

Example of a wrong CLI command and the correct version:

bash
Wrong:
gcloud logging read resource.type=gce_instance --limit=5

Right:
gcloud logging read "resource.type=gce_instance" --limit=5
📊

Quick Reference

ActionCommand / LocationNotes
View logs in ConsoleGoogle Cloud Console > Logging > Logs ExplorerUse filters and time range to find logs visually
View logs via CLIgcloud logging read "resource.type=TYPE" --limit=N --order=descReplace TYPE with resource type, N with number of logs
Filter logs by severityAdd filter: severity=ERRORTo see only error logs
Set time rangeUse Logs Explorer time pickerSelect last hour, day, or custom range

Key Takeaways

Use Google Cloud Console Logs Explorer for easy visual log searching and filtering.
Use the gcloud CLI command with proper quotes and filters to fetch logs from terminal.
Always specify the correct resource type to find relevant logs.
Set appropriate time ranges to avoid missing recent logs.
Avoid overly broad filters to keep log results manageable.