Google Cloud Logging and Monitoring


Cloud Logging (formerly Stackdriver Logging) is a fully managed log management service that lets you store, search, analyze, and export logs from your applications and GCP services.
Key Features:
Ingest logs from Compute Engine, Cloud Run, GKE, App Engine, and custom apps.
Powerful log filtering and querying with Log Explorer.
Export logs to Cloud Storage, BigQuery, or Pub/Sub.
Set up log-based alerts.
Cloud Monitoring (formerly Stackdriver Monitoring) lets you visualize metrics, build dashboards, and receive alerts based on the performance of your systems.
Key Features:
Collect metrics from GCP services, AWS, and on-prem systems.
Prebuilt and custom dashboards.
Uptime checks and alerting policies.
Integrates with Cloud Logging, Error Reporting, Trace, and Profiler.
Hands-On: Monitor a VM Instance
Let’s walk through a basic example of using Logging and Monitoring with a Compute Engine VM.
Step 1: Enable APIs
Make sure these APIs are enabled in your project:
gcloud services enable monitoring.googleapis.com logging.googleapis.com
Step 2: Create a VM with Logging Enabled
gcloud compute instances create monitored-vm \
--zone=us-central1-a \
--metadata=startup-script='#!/bin/bash
sudo apt-get update
sudo apt-get install -y apache2
systemctl start apache2' \
--scopes=https://www.googleapis.com/auth/logging.write,https://www.googleapis.com/auth/monitoring.write
This startup script installs and runs Apache — and sends system and service logs to Cloud Logging automatically.
Step 3: View Logs in Log Explorer
Go to Cloud Console > Logging > Logs Explorer
Use this query to see VM logs:
resource.type="gce_instance"
resource.labels.instance_id="YOUR_INSTANCE_ID"
Step 4: Create a Custom Dashboard
Go to Cloud Console > Monitoring > Dashboards
Click Create Dashboard
Add a line chart for the metric
compute.googleapis.com/instance/cpu/utilization
Apply filters to your instance
Step 5: Set an Alerting Policy
Go to Monitoring > Alerting > Create Policy
Add a condition:
Metric: CPU Utilization
Trigger when CPU > 80% for 5 minutes
Add a notification channel (email, SMS, Slack, etc.)
Save the policy
Exporting Logs (Optional)
To archive or process logs:
gcloud logging sinks create my-logs-to-storage \
storage.googleapis.com/YOUR_BUCKET_NAME \
--log-filter='resource.type="gce_instance"'
Best Practices
Use log-based metrics to turn log entries into monitoring data.
Tag logs with custom labels to improve searchability.
Keep alert rules meaningful — avoid alert fatigue.
Use uptime checks for external endpoint monitoring.
Subscribe to my newsletter
Read articles from Rohit directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Rohit
Rohit
I'm a results-driven professional skilled in both DevOps and Web Development. Here's a snapshot of what I bring to the table: 💻 DevOps Expertise: AWS Certified Solutions Architect Associate: Proficient in deploying and managing applications in the cloud. Automation Enthusiast: Leveraging Python for task automation, enhancing development workflows. 🔧 Tools & Technologies: Ansible, Terraform, Docker, Prometheus, Kubernetes, Linux, Git, Github Actions, EC2, S3, VPC, R53 and other AWS services. 🌐 Web Development: Proficient in HTML, CSS, JavaScript, React, Redux-toolkit, Node.js, Express.js and Tailwind CSS. Specialized in building high-performance websites with Gatsby.js. Let's connect to discuss how my DevOps skills and frontend expertise can contribute to your projects or team. Open to collaboration and always eager to learn! Aside from my work, I've also contributed to open-source projects, like adding a feature for Focalboard Mattermost.