Skip to main content

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:

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 and K_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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

structlog_gcp-0.2.6.tar.gz (111.9 kB view details)

Uploaded Source

Built Distribution

structlog_gcp-0.2.6-py3-none-any.whl (8.3 kB view details)

Uploaded Python 3

File details

Details for the file structlog_gcp-0.2.6.tar.gz.

File metadata

  • Download URL: structlog_gcp-0.2.6.tar.gz
  • Upload date:
  • Size: 111.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.1 CPython/3.12.6

File hashes

Hashes for structlog_gcp-0.2.6.tar.gz
Algorithm Hash digest
SHA256 ee609d5bb7303a39ab0675be31fa9061e1c2eba6e661e7862e0c54cc366b510b
MD5 fe8aa1f13b6446483a117e7f9cf821f2
BLAKE2b-256 d7173d8e02322c8f64db979c105047d20e508bf24458c8a35cd0f81f40fa6abd

See more details on using hashes here.

File details

Details for the file structlog_gcp-0.2.6-py3-none-any.whl.

File metadata

File hashes

Hashes for structlog_gcp-0.2.6-py3-none-any.whl
Algorithm Hash digest
SHA256 024c38e9d8a33d58534b714de8ae2c520014d5689bd1194b7897aedf07466da4
MD5 53f57147b5b6c1082edabab8c5b0976d
BLAKE2b-256 82bf25331cb3fed4d657fedf1feae9c451c6b49f9e545accfebb0e77a2e1151a

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page