structlog-gcp is a Python library that provides a set of structlog processors designed to output logs in the Google Cloud Logging format. It is intended for applications running in Google Cloud environments like GKE or Cloud Functions, facilitating easy integration with Google Cloud Logging and Error Reporting. The library, currently at version 0.5.0, has an active development cycle with regular releases addressing bug fixes and new features.
pip install structlog-gcpVerified import paths — ran on the pinned version, not inferred.
Configures structlog to use the Google Cloud Logging compatible processors. Logs are printed to stdout as JSON, suitable for ingestion by Google Cloud Logging. Exceptions passed with `exception=e` are formatted for Google Error Reporting.
Replace `gcp_logs = structlog_gcp.StructlogGCP(); structlog.configure(processors=gcp_logs.build_processors())` with `processors = structlog_gcp.build_processors(); structlog.configure(processors=processors)`.
Understand that `structlog-gcp` is a formatter, not a sender. Ensure your deployment environment is configured to collect and forward stdout logs to Google Cloud Logging.
Review exception logging logic to ensure desired log levels are explicitly set or understood. If `CRITICAL` is always needed for exceptions, set the level explicitly.
If using `build_gcp_processors()`, explicitly add a JSON renderer (e.g., `structlog.processors.JSONRenderer()`) and any other necessary processors to your chain. For default behavior, continue using `structlog_gcp.build_processors()`.
Always pass the actual exception object using the `exception` keyword argument or use `logger.exception()` for automatic Error Reporting integration.