0
0
GCPcloud~5 mins

Cloud Logging overview in GCP - Commands & Configuration

Choose your learning style9 modes available
Introduction
Cloud Logging helps you collect and view logs from your applications and services in one place. It solves the problem of finding and understanding what happened in your cloud environment by organizing logs clearly.
When you want to see error messages from your app running on Google Cloud.
When you need to track user activity or system events for security checks.
When you want to monitor performance issues by reviewing logs over time.
When you want to create alerts based on specific log messages.
When you need to export logs to storage or analysis tools for deeper insights.
Commands
This command writes a test log entry with INFO severity to a log named 'my-log'. It helps verify that logging is working.
Terminal
gcloud logging write my-log "This is a test log entry" --severity=INFO
Expected OutputExpected
Created log entry.
--severity - Sets the importance level of the log entry.
This command reads the last 5 entries from the 'my-log' log in descending order to see recent log messages.
Terminal
gcloud logging read "logName=projects/my-project/logs/my-log" --limit=5 --order=desc
Expected OutputExpected
[ { "textPayload": "This is a test log entry", "severity": "INFO", "logName": "projects/my-project/logs/my-log" } ]
--limit - Limits the number of log entries returned.
--order - Orders the log entries by newest first.
This command creates a sink named 'my-sink' that exports all ERROR and higher severity logs to a Cloud Storage bucket named 'my-bucket'.
Terminal
gcloud logging sinks create my-sink storage.googleapis.com/my-bucket --log-filter="severity>=ERROR"
Expected OutputExpected
Created sink [my-sink].
--log-filter - Filters logs to export only those with severity ERROR or higher.
This command lists all log names available in the project so you can see what logs exist.
Terminal
gcloud logging logs list
Expected OutputExpected
my-log cloudaudit.googleapis.com%2Factivity compute.googleapis.com%2Finstance
Key Concept

If you remember nothing else from this pattern, remember: Cloud Logging collects and organizes logs so you can easily find and understand what happened in your cloud services.

Common Mistakes
Not specifying the correct project when running gcloud logging commands.
Logs belong to specific projects, so commands run in the wrong project won't show or write the expected logs.
Use 'gcloud config set project my-project' to set the right project before running logging commands.
Using incorrect log names or filters when reading or exporting logs.
If the log name or filter is wrong, you won't see the logs you expect or export the right data.
Double-check log names and use simple filters first to confirm logs exist before applying complex filters.
Summary
Use 'gcloud logging write' to add log entries manually for testing or custom logs.
Use 'gcloud logging read' to view recent logs with filters and limits.
Create sinks to export logs to storage or other services for analysis.
List logs to discover what logs are available in your project.