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
logginglog levels are available and translated to their GCP equivalents. - Exceptions and
CRITICALlog messages will be reported into Google Error Reporting dashboard - Additional logger bound arguments will be reported into the
jsonPayloadevent.
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)
try:
1 / 0
except ZeroDivisionError as exc:
logger.info("This was known to happen! {exc}")
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
Errors
Errors are automatically reported to the Google Error Reporting service, most of the time.
Using logger.exception
Using:
try:
1 / 0
except:
logger.exception("oh no")
Will give you:
- The current exception can automatically added into the log event
- The log level will be
ERROR - The exception will be reported in Error Reporting
Using logger.$LEVEL(..., exception=exc)
Using:
try:
1 / 0
except Exception as exc
logger.info("oh no", exception=exc)
Will give you:
- The specified exception will be part of the log event
- The log level will be
INFO, or whichever log level you used - The exception will be reported in Error Reporting
Using logger.$LEVEL(...)
Not passing any exception argument to the logger, as in:
try:
1 / 0
except Exception as exc
logger.warning(f"oh no: {exc}")
Will give you:
- The exception will not be part of the log event.
- The log level will be
WARNING(or whichever log level you used) - AND the exception will not be reported in Error Reporting
Configuration
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 Run environment variables configured, in particular the
K_SERVICEandK_REVISIONvariables. -
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file structlog_gcp-0.5.0.tar.gz.
File metadata
- Download URL: structlog_gcp-0.5.0.tar.gz
- Upload date:
- Size: 117.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
361da96886023e6fb8fa49f60f5278aff666820c8987534ca61e091b32047592
|
|
| MD5 |
83813c0aa42b88660822159aee9eb3ca
|
|
| BLAKE2b-256 |
07f9f305098c1bb4f4f3aeb017accf6ca6f60ce24d33b54b3ef19d4971f39118
|
Provenance
The following attestation bundles were made for structlog_gcp-0.5.0.tar.gz:
Publisher:
build-test-release.yml on multani/structlog-gcp
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
structlog_gcp-0.5.0.tar.gz -
Subject digest:
361da96886023e6fb8fa49f60f5278aff666820c8987534ca61e091b32047592 - Sigstore transparency entry: 272991685
- Sigstore integration time:
-
Permalink:
multani/structlog-gcp@1210c2c84d014079f202ea9c48dfdaaa13c66a93 -
Branch / Tag:
refs/tags/v0.5.0 - Owner: https://github.com/multani
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
build-test-release.yml@1210c2c84d014079f202ea9c48dfdaaa13c66a93 -
Trigger Event:
release
-
Statement type:
File details
Details for the file structlog_gcp-0.5.0-py3-none-any.whl.
File metadata
- Download URL: structlog_gcp-0.5.0-py3-none-any.whl
- Upload date:
- Size: 9.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
95d4611e2ae8e8c1cc6b18b580c3060240bb47dc1c6f62ae8619c7b823949677
|
|
| MD5 |
9a2f3c4658df85b4292475efc6fb2757
|
|
| BLAKE2b-256 |
2f2da63e143137574f524446731eb9e9358cda09af5232fe9f42ab6831be1194
|
Provenance
The following attestation bundles were made for structlog_gcp-0.5.0-py3-none-any.whl:
Publisher:
build-test-release.yml on multani/structlog-gcp
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
structlog_gcp-0.5.0-py3-none-any.whl -
Subject digest:
95d4611e2ae8e8c1cc6b18b580c3060240bb47dc1c6f62ae8619c7b823949677 - Sigstore transparency entry: 272991686
- Sigstore integration time:
-
Permalink:
multani/structlog-gcp@1210c2c84d014079f202ea9c48dfdaaa13c66a93 -
Branch / Tag:
refs/tags/v0.5.0 - Owner: https://github.com/multani
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
build-test-release.yml@1210c2c84d014079f202ea9c48dfdaaa13c66a93 -
Trigger Event:
release
-
Statement type: