Skip to main content

Unified Logging Module - Dual-Mode (Local + GCP Cloud Logging)

Automatically detects environment and configures appropriate logging backend:

  • LOCAL: File-based logging with RollingLineFileHandler
  • GCP: Cloud Logging with structured logs, correlation IDs, resource labels

Design principles:

  1. Zero code changes between local and GCP environments
  2. Structured logging with JSON format
  3. Correlation IDs for distributed tracing
  4. Resource labels for GKE pod metadata
  5. Log-based metrics for monitoring
  6. Automatic environment detection

Author: AZ1.AI INC (Hal Casteel) Framework: CODITECT License: MIT

File: unified_logger.py

Classes

StructuredLogEntry

Structured log entry for consistent logging format.

UnifiedLogger

Dual-mode logger that works in both local and GCP environments.

Functions

detect_environment()

Detect execution environment.

get_gcp_resource_labels()

Extract GCP resource labels from environment.

setup_unified_logger(component, log_file, max_lines, console_level, file_level, workflow_id)

Setup unified logger with automatic environment detection.

to_dict()

Convert to dictionary, filtering None values.

to_json()

Convert to JSON string.

debug(message, operation, metadata)

Log DEBUG level message.

info(message, operation, step, metadata)

Log INFO level message.

warning(message, operation, metadata)

Log WARNING level message.

error(message, operation, metadata, exc_info)

Log ERROR level message.

critical(message, operation, metadata)

Log CRITICAL level message.

log_step_start(step, step_name)

Log the start of a workflow step.

log_step_success(step, step_name, start_time)

Log successful completion of a step.

log_step_error(step, step_name, error)

Log step failure with error details.

log_checkpoint(message)

Log a checkpoint (verification point) in the workflow.

log_verification_success(what, details)

Log successful verification.

Usage

python unified_logger.py