Skip to content

Structured Logging Handler should be project aware #544

Closed as not planned
Closed as not planned
@averikitsch

Description

@averikitsch

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    2023-triageapi: loggingIssues related to the googleapis/python-logging API.priority: p2Moderately-important priority. Fix may not be included in next release.type: feature request‘Nice-to-have’ improvement, new feature or different behavior or design.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions