Python Structured Logging on Google Stackdriver as jsonPayload

I want to log as jsonPayload in GCF logging but now it was logged as textPayload. I already tried to use python-json-logger and structlog. They gave the same results. I think becuase both of them use json.dumps to make it as string. That’s why it was logged as textPayload.

GCF logging image here

Another method I tried is on this link https://medium.com/google-cloud/python-and-stackdriver-logging-2ade460c90e3. But I got some errors. it was trying to concat
log dict with string when logging.

Here is my code

import logging
import os
import structlog

from google.cloud import logging_v2 as cloud_logging
from structlog.contextvars import bind_contextvars, merge_contextvars


from module_a.test import Test


def rename_event_key(_, __, ed):
    ed["message"] = ed.pop("event")
    return ed


structlog.configure(
    processors=[
        merge_contextvars,
        rename_event_key,
        structlog.stdlib.add_logger_name,
        structlog.stdlib.add_log_level,
        structlog.stdlib.PositionalArgumentsFormatter(),
        structlog.processors.format_exc_info,
        structlog.processors.StackInfoRenderer(),
        structlog.processors.JSONRenderer(),
    ],
    context_class=dict,
    logger_factory=structlog.stdlib.LoggerFactory(),
    wrapper_class=structlog.stdlib.BoundLogger,
    cache_logger_on_first_use=True,
)

root_logger = logging.getLogger()
root_logger.setLevel(logging.INFO)

if os.environ.get("app_env", "develop") != "local":
    logging_client = cloud_logging.Client()
    handler = logging_client.get_default_handler()
else:
    handler = logging.StreamHandler()

root_logger.addHandler(handler)

logger = structlog.getLogger(__name__)


def main(request):
    item_id = request.args.get("item_id")

    bind_contextvars(item_id=item_id)

    logger.debug("debug message", random_id=123)
    logger.info("info message", random_id_info=1233423)
    logger.warning("warning message")
    logger.error("error message")

    test = Test()

    return "OK"

and this is some log output from my local console

{"random_id_info": 1233423, "item_id": "123", "message": "info message", "logger": "main", "level": "info"}
{"item_id": "123", "message": "warning message", "logger": "main", "level": "warning"}
{"item_id": "123", "message": "error message", "logger": "main", "level": "error"}
{"item_id": "123", "message": "Warning message in test module", "logger": "module_a.test", "level": "warning"}
{"item_id": "123", "message": "Cannot devided", "logger": "module_a.test", "level": "error", "exception": "Traceback (most recent call last):n  File "/Users/melodymale/Documents/test-cloud-logging/module_a/test.py", line 11, in __init__n    s = 1 / 0nZeroDivisionError: division by zero"}

Source: Python Questions

LEAVE A COMMENT