gRPC Low-Level Client for Google Cloud Logging API v2
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
- breaking This library is marked with an "Inactive" development status (7 - Inactive) on PyPI, and its last release was in September 2016. It is no longer actively maintained.
- gotcha Application developers should almost always use the `google-cloud-logging` Python library (`pip install google-cloud-logging`) for interacting with Google Cloud Logging, not `grpc-google-logging-v2`. `google-cloud-logging` provides a user-friendly, idiomatic Python interface that integrates seamlessly with the standard `logging` module and handles the underlying gRPC communication.
- breaking The last release of `grpc-google-logging-v2` officially supported Python 2.7, 3.4, and 3.5. Using it directly with modern Python versions (3.6+) may lead to compatibility issues or unexpected behavior due to lack of maintenance.
- gotcha `grpc-google-logging-v2` is a low-level, generated gRPC client. Direct usage requires deep understanding of gRPC, Protocol Buffers (especially `google.logging.v2.logging_pb2` and `logging_pb2_grpc`), and manual client stub management, which is significantly more complex than using `google-cloud-logging`.
- gotcha When not using `google-cloud-logging`'s `setup_logging()` for integration, standard Python `logging.info()` calls might appear with incorrect (e.g., `ERROR`) severity in Cloud Logging, as the default behavior for raw stdout/stderr ingestion might not correctly map Python logging levels.
Install
-
pip install grpc-google-logging-v2
Imports
- Client
from google.cloud import logging
- LoggingServiceV2Client
from google.logging.v2 import logging_pb2_grpc
- LogEntry
from google.logging.v2 import logging_pb2
Quickstart
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.