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)
The structlog_gcp.build_processors()
function constructs structlog processors to:
- Output logs as Google Cloud Logging format using the default Python JSON serializer.
- Carry context variables across loggers (see structlog: Context Variables)
For more advanced usage, see [Advanced Configuration][#advanced-configuration]
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)
Advanced Configuration
If you need to have more control over the processors configured by the library, you can use the structlog_gcp.build_gcp_processors()
builder function.
This function only configures the Google Cloud Logging-specific processors and omits all the rest.
In particular, you can use this function:
- If you want to have more control over the processors to be configured in structlog. You can prepend or append other processors around the Google-specific ones.
- If you want to serialize using another JSON serializer or with specific options.
For instance:
import orjson
import structlog
from structlog.processors import JSONRenderer
import structlog_gcp
def add_open_telemetry_spans(...):
# Cf. https://www.structlog.org/en/stable/frameworks.html#opentelemetry
...
gcp_processors = structlog_gcp.build_gcp_processors()
# Fine-tune processors
processors = [add_open_telemetry_spans]
processors.extend(gcp_processors)
processors.append(JSONRenderer(serializer=orjson.dumps))
structlog.configure(processors=processors)
[!IMPORTANT]
structlog_gcp.build_gcp_processors()
doesn't configure a renderer and you must supply a JSON renderer of your choice for the library to work correctly.
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.3.0.tar.gz
.
File metadata
- Download URL: structlog_gcp-0.3.0.tar.gz
- Upload date:
- Size: 113.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ab88b215386ff0209b80ef2f73bd60d9ce56c5a5fc07e5da9f1947cbc40a0c04 |
|
MD5 | d85c54e0457748631ad4cd2e3774abf5 |
|
BLAKE2b-256 | a902d295e618debade6ad54e5a6edabd719436f6f28c8e1089f05b54af2a3f70 |
File details
Details for the file structlog_gcp-0.3.0-py3-none-any.whl
.
File metadata
- Download URL: structlog_gcp-0.3.0-py3-none-any.whl
- Upload date:
- Size: 9.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 | 767c9e7a545104e73230913ac9c92f78baffaa5b850c7780fdee5c8955fbc253 |
|
MD5 | 6318cbfa01fe212c232355a67edc7f8d |
|
BLAKE2b-256 | 2a229a2e089c03f666ba94fe2be9863b020f8c3b9e71997ff4de55ba750a2c03 |