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 details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

File details

Details for the file google-cloud-structured-logger-0.1.1.tar.gz.

File metadata

File hashes

Hashes for google-cloud-structured-logger-0.1.1.tar.gz
Algorithm Hash digest
SHA256 6826ecc29cc575ea3f4bac1b642ef2fc1254ac03ad08b99d8abc7c890f8a34da
MD5 744ff003a6e3da612edbe492c19a3020
BLAKE2b-256 9d8eadb5ef54369803bf982c323ff86870e109e8626aa1b5972db420656d2972

See more details on using hashes here.

File details

Details for the file google_cloud_structured_logger-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for google_cloud_structured_logger-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 60944c3a4aefa655c94b976c192fd57a1db5ffc60a228d4f332b0af0462a00ef
MD5 cc06ebc65ac925a43b0ceba517f6d833
BLAKE2b-256 1080b3a446289438983af805baca77aa05d389049ee5f93e81c838f6ca3cf5e5

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page