stacklogging

Python structured logging with Google Cloud Stackdriver API integration


Keywords
stackdriver, logging, google-cloud
License
MIT
Install
pip install stacklogging==0.1.5

Documentation

Stacklogging

License PyPI version

This small library generates log entries that are understood by Stackdriver Logging. You won't need to authenticate in GCE. The logs emmited will be in JSON schema understood by Stackdriver API and Stackdriver Logging.

Installation

You can use pip to install stacklogging:

pip install stacklogging

Usage

import stacklogging

logger = stacklogging.getLogger()

def sum(a, b):
    logger.info(f"Adding {a} and {b}")
    return a + b

r = sum(2, 4)

This will output a JSON like this one:

{
    "message": "Adding 2 and 4",
    "timestamp": {
        "seconds": 1548239928,
        "nanos": 380779027
    },
    "thread": 140619343472128,
    "severity": "INFO",
    "sourceLocation": {
        "file": "test.py",
        "function": "sum",
        "line": 6
    }
}

It is also possible to log extra fields at runtime using the extra parameter.

logger.info("message", extra={"from": "Alice", "to": "Bob"})

Stacklogging will add the extra keys to the final JSON:

{
    "message": "Email sent",
    "timestamp": {
        "seconds": 1548260191,
        "nanos": 256482124
    },
    "thread": 140166127678976,
    "severity": "INFO",
    "sourceLocation": {
        "file": "test.py",
        "function": "sum",
        "line": 9
    },
    "from": "Alice",
    "to": "Bob"
}

License

MIT © Buffer