Skip to main content

A Python library adding a structured log formatter for Google Cloud

Project description

Structured Logger

Formatter for structured logging in Google Cloud.

Installing

pip install google-cloud-structured-logger

Usage

import logging
from gcstructuredlogger import structuredlogger

logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)

log_handler = logging.StreamHandler()
formatter = structuredlogger.StructuredJsonFormatter()
log_handler.setFormatter(formatter)
log_handler.setLevel(logging.DEBUG)
logger.addHandler(log_handler)

Dict config example

import logging.config

from gcstructuredlogger import structuredlogger

LOGGING_CONFIG = {
    "version": 1,
    "disable_existing_loggers": False,
    "formatters": {
        "structured_json": {
            "()": structuredlogger.StructuredJsonFormatter
        }
    },
    "handlers": {
        "default": {
            "formatter": "structured_json",
            "level": "DEBUG",
            "class": "logging.StreamHandler",
            "stream": "ext://sys.stdout",
        }
    },
    "root": {
        "level": "DEBUG",
        "handlers": ["default"]
    },
}

logging.config.dictConfig(LOGGING_CONFIG)

Features

  • severity is set to the same level as the log
  • Send log to Error Reporting if log level is ERROR, CRITICAL, or FATAL
  • If exc_info is True, the stack trace is set to stack_trace

Log example

main.py:

logger.debug("debug")
logger.info("info", extra={"foo": "bar"})
logger.error("error")
logger.critical("critical")
logger.fatal("fatal")

try:
    1 / 0
except Exception as e:
    logger.error(e, exc_info=True)

Output:

{"message": "debug", "timestamp": "2022-08-18T12:55:52.064302", "severity": "DEBUG"}
{"message": "info", "foo": "bar", "timestamp": "2022-08-18T12:55:52.064358", "severity": "INFO"}
{"message": "error", "timestamp": "2022-08-18T12:55:52.064381", "severity": "ERROR", "@type": "type.googleapis.com/google.devtools.clouderrorreporting.v1beta1.ReportedErrorEvent"}
{"message": "critical", "timestamp": "2022-08-18T12:55:52.064399", "severity": "CRITICAL", "@type": "type.googleapis.com/google.devtools.clouderrorreporting.v1beta1.ReportedErrorEvent"}
{"message": "fatal", "timestamp": "2022-08-18T12:55:52.064414", "severity": "CRITICAL", "@type": "type.googleapis.com/google.devtools.clouderrorreporting.v1beta1.ReportedErrorEvent"}
{"message": "division by zero", "stack_trace": "Traceback (most recent call last):\n  File \"/path/to/main.py\", line 16, in main\n    1 / 0\nZeroDivisionError: division by zero", "timestamp": "2022-08-18T12:55:52.064520", "severity": "ERROR", "@type": "type.googleapis.com/google.devtools.clouderrorreporting.v1beta1.ReportedErrorEvent"}

Logs displayed in Google Cloud

Cloud Function: cloud_function_logs

Cloud Logging: cloud_logging_logs

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

google-cloud-structured-logger-0.1.1.tar.gz (3.7 kB view hashes)

Uploaded Source

Built Distribution

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