Skip to main content

Lightweight, production-ready logging package for Google Cloud Platform applications

Project description

GCLog

Tests codecov Python 3.9+ PyPI version

A lightweight, production-ready logging package for Google Cloud Platform applications. Built on top of loguru, it automatically detects GCP environments and provides structured JSON logging for cloud services while maintaining human-readable logs for local development.

Why?

I found that my logs in a fastapi app were using the Google Cloud Platforms' default log level irrespective of the actual log level that was being emitted by the app itself. This meant my GCP logs were reporting things incorrectly, decreasing visibility and making it harder to debug issues. This issue came up time and again, and I didn't want to have to write a log formatter for every service, so I wrote this package to fix this problem once and for all.

Features

  • 🚀 Auto-detection of all major GCP services (Cloud Run, Cloud Functions, App Engine, GKE, Compute Engine)
  • 📊 Structured JSON logging for GCP with proper severity levels
  • 🎨 Beautiful colored logs for local development
  • Thread-safe singleton pattern for consistent logging across your application
  • 🛡️ Zero external dependencies beyond loguru and requests
  • 🧪 Fully tested with comprehensive test coverage

Supported GCP Services

  • Cloud Run - Detected via K_REVISION environment variable
  • Cloud Functions - Detected via FUNCTION_NAME environment variable
  • App Engine - Detected via GAE_APPLICATION environment variable
  • Cloud Run Jobs - Detected via CLOUD_RUN_JOB environment variable
  • Google Kubernetes Engine (GKE) - Detected via KUBERNETES_SERVICE_HOST environment variable
  • Compute Engine - Detected via metadata server API

Installation

pip install gclog

Quick Start

from gclog import get_logger

# Get a configured logger instance
logger = get_logger()

# Use it like any loguru logger
logger.info("Application started")
logger.warning("This is a warning", extra={"user_id": "12345"})
logger.error("Something went wrong", extra={"error_code": "E001"})

# Exception logging with full traceback
try:
    result = 1 / 0
except Exception:
    logger.exception("Division by zero error")

Log Output

Local Development

2025-01-15 10:30:45.123 | INFO     | myapp:main:15 - Application started | {}
2025-01-15 10:30:45.124 | WARNING  | myapp:main:16 - This is a warning | {'user_id': '12345'}
2025-01-15 10:30:45.125 | ERROR    | myapp:main:17 - Something went wrong | {'error_code': 'E001'}

GCP Cloud Environment

{
  "severity": "INFO",
  "message": "Application started",
  "time": "2025-01-15T10:30:45.123000",
  "extra": {},
  "file": {"name": "main.py", "path": "/app/main.py"},
  "function": "main",
  "line": 15,
  "module": "myapp",
  "process": {"id": 1, "name": "python"},
  "thread": {"id": 140567890, "name": "MainThread"},
  "elapsed": 0.001
}

Configuration

Log Level

Set the log level using the LOG_LEVEL environment variable:

export LOG_LEVEL=INFO

Or pass it directly:

from gclog import GCPLogger

logger = GCPLogger(level="DEBUG")

Custom Labels

Add custom context to your logs:

logger.info("User action", extra={
    "user_id": "user123",
    "action": "login",
    "ip_address": "192.168.1.1"
})

Advanced Usage

Manual Environment Detection

from gclog import is_running_on_cloud

if is_running_on_cloud():
    print("Running on GCP!")
else:
    print("Running locally")

Direct Logger Configuration

from gclog import GCPLogger

# This returns the global loguru logger instance
logger = GCPLogger(level="WARNING")

# All subsequent calls return the same configured instance
logger2 = GCPLogger()  # Same as logger

Local Development

When not running on GCP, the logger:

  • Uses colored output for better readability
  • Sends INFO and below to stdout
  • Sends WARNING and above to stderr
  • Includes full diagnostic information

Error Handling

The logger includes robust error handling:

  • Graceful fallback if GCP detection fails
  • Automatic fallback to basic logging if configuration fails
  • Thread-safe initialization prevents race conditions

Requirements

  • Python 3.9+
  • loguru
  • requests (for GCP metadata server detection)

Testing

Run the test suite:

pytest tests/ -v

Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Add tests for new functionality
  4. Ensure all tests pass
  5. Submit a pull request

License

MIT License - see LICENSE file for details.

Changelog

v0.1.1

  • feat: Add conditional extra data formatting for cleaner logs
  • improvement: Extra data section only appears when data is present
  • docs: Update examples and documentation

v0.1.0

  • Initial release
  • Support for all major GCP services
  • Structured JSON logging for cloud environments
  • Beautiful local development logs
  • Thread-safe singleton pattern
  • Comprehensive test coverage

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

gclog-0.2.0.tar.gz (67.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

gclog-0.2.0-py3-none-any.whl (7.0 kB view details)

Uploaded Python 3

File details

Details for the file gclog-0.2.0.tar.gz.

File metadata

  • Download URL: gclog-0.2.0.tar.gz
  • Upload date:
  • Size: 67.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.11

File hashes

Hashes for gclog-0.2.0.tar.gz
Algorithm Hash digest
SHA256 a1bcca1594d43d496d0a9da2dc5c5997adab144c14ad19e38bb3301c821293d9
MD5 373a0cb6362cb8143eda96a9270ef93c
BLAKE2b-256 7bfe7dd85008724d8be75dd3bedd01c282566ddbe2b8577e572d4d497e691084

See more details on using hashes here.

File details

Details for the file gclog-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: gclog-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 7.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.11

File hashes

Hashes for gclog-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b2530ad2a7fd601135c9a37a04fb767242d3a7fcb53c94e9ff45cc0cc4aff5f4
MD5 beb4be2c9f940524ed0fa4edde5c6fc0
BLAKE2b-256 c7c31d229579ebc2195af7dc978f09cfa8d43f865b6fe6ea0918cbe373f63f6a

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page