A structlog set of processors to output as Google Cloud Logging format
Project description
Google Cloud Logging formatter for structlog
This is an opiniated package that configures structlog to output log compatible with the Google Cloud Logging log format.
The intention of this package is to be used for applications that run in Google Kubernetes Engine (GKE) or Google Cloud Function, or any other systems that know how to send logs to Google Cloud.
As such, the package is only concerned about formatting logs, where logs are expected to be written on the standard output. Sending the logs to the actual Google Logging API is supposed to be done by an external agent.
In particular, this package provides the following configuration by default:
- Logs are formatted as JSON using the Google Cloud Logging log format
- The Python standard library's
logging
log levels are available and translated to their GCP equivalents. - Exceptions and
CRITICAL
log messages will be reported into Google Error Reporting dashboard - Additional logger bound arguments will be reported into the
jsonPayload
event.
How to use?
Install the package with pip
or your favorite Python package manager:
pip install structlog-gcp
Then, configure structlog
as usual, using the Structlog processors the package
provides:
import structlog
import structlog_gcp
processors = structlog_gcp.build_processors()
structlog.configure(processors=processors)
Then, you can use structlog
as usual:
logger = structlog.get_logger().bind(arg1="something")
logger.info("Hello world")
converted = False
try:
int("foobar")
converted = True
except:
logger.exception("Something bad happens")
if not converted:
logger.critical("This is not supposed to happen", converted=converted)
Errors
Errors are automatically reported to the Google Error Reporting service.
You can configure the service name and the version used during the report with 2 different ways:
-
By default, the library assumes to run with Cloud Function environment variables configured, in particular the
K_SERVICE
andK_REVISION
variables. -
You can also pass the service name and revision at configuration time with:
import structlog import structlog_gcp processors = structlog_gcp.build_processors( service="my-service", version="v1.2.3", ) structlog.configure(processors=processors)
Examples
Check out the examples
folder to see how it can be used.
-
How it should appear in the Google Cloud Logging log explorer:
-
How it should appear in the Google Cloud Error Reporting dashboard:
Reference
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file structlog_gcp-0.2.5.tar.gz
.
File metadata
- Download URL: structlog_gcp-0.2.5.tar.gz
- Upload date:
- Size: 144.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | cc00b144f81ea3de56a61556603e4aa8ea197246d64e968de547b208808a3112 |
|
MD5 | 6d8a9695a6e50f4d56510557241d8fb4 |
|
BLAKE2b-256 | 984cf587aee8d4dd514e4faad09feefa19c8bf1681b131537319b082a06ab258 |
File details
Details for the file structlog_gcp-0.2.5-py3-none-any.whl
.
File metadata
- Download URL: structlog_gcp-0.2.5-py3-none-any.whl
- Upload date:
- Size: 8.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f1e9bae9142d69b6782321af2255de1518eb6db752c0c4ba78f6b99c0a60f9d7 |
|
MD5 | 625f55cac50b36fe7e0eaa3f3459b90f |
|
BLAKE2b-256 | fb2677a29d21273e14023bb2e1b5f0dad0ffcca5e1d8bf31991f997c06c654b2 |