gcp-log 1.0.1

Creator: bradpython12

Last updated:

Add to Cart

Description:

gcplog 1.0.1

gcp-log
Python library to format logs as GCP-compatible JSON.
Features:

Works with standard logging.
Supports extra for structured logging.
100% compatible with the official spec.
Non-invasive, doesn't start any threads or processes, doesn't make network or system calls.
Zero configuration.
Fast.
Pure Python.
Type-safe.
Zero dependency.
Optional integration with orjson for better performance.

Installation
python3 -m pip install gcp-log

Optional: If you also install orjson, gcp-log will automatically use it instead of stdlib json for faster serialization:
python3 -m pip install orjson

Usage
To use it, simply set gcp_log.Formatter as the formatter for the logger:
import logging
import gcp_log

logger = logging.getLogger()
handler = logging.StreamHandler()
formatter = gcp_log.Formatter()
handler.setFormatter(formatter)
logger.addHandler(handler)

And then you can use the logger:
logger.info('aragorn', extra=dict(father='arathorn'))

The output:
{"message":"aragorn","severity":"INFO","timestamp":"2022-03-17T10:09:58.393124+00:00Z","logging.googleapis.com/sourceLocation":{"file":"/full/path/to/example.py.py","line":24,"function":"test_gcp_formatter"},"father":"arathorn"}

A human-readable version:
{
"message": "aragorn",
"severity": "INFO",
"timestamp": "2022-03-17T10:09:58.393124+00:00Z",
"logging.googleapis.com/sourceLocation": {
"file": "/full/path/to/example.py.py",
"line": 24,
"function": "test_gcp_formatter",
},
"father": "arathorn",
}

License

For personal and professional use. You cannot resell or redistribute these repositories in their original state.

Customer Reviews

There are no reviews.