Closed as not planned
Description
I want to use StructuredLogHandler directly to ensure printing to stdout regardless of location, especially locally. I want my project Id to be automatically inferred so the trace ID includes my project ID ("logging.googleapis.com/trace": "projects/PROJECT_ID/traces/abcdef",
).
Neither locally or remotely, will the project id be set automatically:
logging_client = Client()
from google.cloud.logging_v2.handlers import StructuredLogHandler, setup_logging
handler = StructuredLogHandler()
setup_logging(handler)
import logging
msg = "Hello, world!"
metadata = {"component": "arbitrary-property", "severity":"error"}
logging.info(msg, extra={"json_fields": metadata})
The solution is to set the project ID:
handler = StructuredLogHandler(project_id="PROJECT_ID")
setup_logging(handler)
Or use CloudLoggingHandler (note this doesn't print to stdout unless env vars are set):
logging_client = Client()
logging_client.setup_logging()