gRPC Low-Level Client for Google Cloud Logging API v2

0.11.1 · abandoned · verified Sat Apr 11

This library provides the low-level gRPC client for directly interacting with the Google Cloud Logging API v2. It is derived from the API's Interface Definition Language (IDL) and is generally *not* intended for direct use by application developers. For Python applications, the recommended way to interact with Google Cloud Logging is via the higher-level `google-cloud-logging` library, which offers a more idiomatic Python interface and integrates with the standard `logging` module. This `grpc-google-logging-v2` package is at version 0.11.1 and has been in an inactive development status since 2016.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use the recommended `google-cloud-logging` library to send logs to Google Cloud Logging, integrating with Python's standard `logging` module. The `setup_logging()` method automatically configures a handler, often routing structured JSON to stdout/stderr in Cloud environments for efficient ingestion. For direct interaction with the low-level `grpc-google-logging-v2` library, the process would be significantly more complex, involving manual gRPC channel management and protobuf message construction.

import logging
import os
from google.cloud import logging as cloud_logging

# Instantiates a client for the higher-level google-cloud-logging library
client = cloud_logging.Client()

# This method sets up a handler on the root logger, routing all logs at
# INFO level and higher to Cloud Logging.
# It automatically detects the environment (e.g., Cloud Run, GKE) to
# use the most efficient logging method (e.g., structured JSON to stdout).
client.setup_logging()

# Use the standard Python logging library
logging.info("Hello from a Python standard logger!")
logging.warning("This is a warning log message.")
logging.error("An error occurred here!")

# To send structured data, log a dictionary or JSON-parsable string
# The `extra` argument can also be used for structured fields
logging.info(
    "Structured log message",
    extra={
        "json_fields": {
            "component": "my-app",
            "requestId": os.environ.get('REQUEST_ID', 'N/A'),
            "status": "success",
            "latency_ms": 123
        }
    }
)

print("Logs sent to Google Cloud Logging via standard Python logging.")
# In local development, you'll see these logs in your console.
# In a Google Cloud environment (e.g., Cloud Run, GKE), they will be
# automatically ingested by Cloud Logging.

view raw JSON →