Metadata-Version: 2.3
Name: structlog-gcp
Version: 0.2.5
Summary: A structlog set of processors to output as Google Cloud Logging format
Project-URL: Documentation, https://github.com/multani/structlog-gcp#readme
Project-URL: Issues, https://github.com/multani/structlog-gcp/issues
Project-URL: Source, https://github.com/multani/structlog-gcp
Author-email: Jonathan Ballet <jon@multani.info>
License: The MIT License (MIT)
        
        Copyright (c) Jonathan Ballet and contributors
        
        Permission is hereby granted, free of charge, to any person obtaining a copy
        of this software and associated documentation files (the "Software"), to deal
        in the Software without restriction, including without limitation the rights
        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
        copies of the Software, and to permit persons to whom the Software is
        furnished to do so, subject to the following conditions:
        
        The above copyright notice and this permission notice shall be included in all
        copies or substantial portions of the Software.
        
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
        SOFTWARE.
License-File: LICENSE
Classifier: Development Status :: 4 - Beta
Classifier: Programming Language :: Python
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: Implementation :: CPython
Classifier: Programming Language :: Python :: Implementation :: PyPy
Requires-Python: >=3.10
Requires-Dist: structlog
Description-Content-Type: text/markdown

# Google Cloud Logging formatter for `structlog`

This is an opiniated package that configures [structlog](https://structlog.org/)
to output log compatible with the [Google Cloud Logging log
format](https://cloud.google.com/logging/docs/structured-logging).

The intention of this package is to be used for applications that run in [Google
Kubernetes Engine (GKE)](https://cloud.google.com/kubernetes-engine/) or [Google
Cloud Function](https://cloud.google.com/functions/), 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](https://cloud.google.com/logging/docs/structured-logging)
* The [Python standard library's `logging`](https://docs.python.org/3/library/logging.html)
  log levels are available and translated to their GCP equivalents.
* Exceptions and `CRITICAL` log messages will be reported into [Google Error Reporting dashboard](https://cloud.google.com/error-reporting/)
* 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:

```sh
pip install structlog-gcp
```

Then, configure `structlog` as usual, using the Structlog processors the package
provides:

```python
import structlog
import structlog_gcp

processors = structlog_gcp.build_processors()
structlog.configure(processors=processors)
```

Then, you can use `structlog` as usual:

```python
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](https://cloud.google.com/error-reporting/).

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](https://cloud.google.com/functions/docs/configuring/env-var#runtime_environment_variables_set_automatically).
* You can also pass the service name and revision at configuration time with:

  ```python
  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](https://github.com/multani/structlog-gcp/tree/main/examples) to see how it can be used.

* How it should appear in the Google Cloud Logging log explorer:
  ![](https://raw.githubusercontent.com/multani/structlog-gcp/main/docs/logs.png)

* How it should appear in the Google Cloud Error Reporting dashboard:
  ![](https://raw.githubusercontent.com/multani/structlog-gcp/main/docs/errors.png)


## Reference

* https://cloud.google.com/logging/docs/structured-logging
* https://cloud.google.com/error-reporting/docs/formatting-error-messages
* https://cloud.google.com/logging/docs/reference/v2/rest/v2/LogEntry
