Cloud Logging Client

Client for interacting with the Google Cloud Logging API.

class google.cloud.logging_v2.client.Client(*, project=None, credentials=None, _http=None, _use_grpc=None, client_info=None, client_options=None)

Bases:google.cloud.client.ClientWithProject

Client to bundle configuration needed for API requests.

SCOPE(: Optional[Tuple[str, ...] = ('https://www.googleapis.com/auth/logging.read', 'https://www.googleapis.com/auth/logging.write', 'https://www.googleapis.com/auth/logging.admin', 'https://www.googleapis.com/auth/cloud-platform' )

The scopes required for authenticating as a Logging consumer.

get_default_handler(**kw)

Return the default logging handler based on the local environment.

  • Parameters

    kw (dict) – keyword args passed to handler constructor

  • Returns

    The default log handler based on the environment

  • Return type

    logging.Handler

list_entries(*, resource_names=None, filter_=None, order_by=None, max_results=None, page_size=None, page_token=None)

Return a generator of log entry resources.

  • Parameters

    • resource_names (Sequence[str]) – Names of one or more parent resourcesfrom which to retrieve log entries:

      "projects/[PROJECT_ID]""organizations/[ORGANIZATION_ID]""billingAccounts/[BILLING_ACCOUNT_ID]""folders/[FOLDER_ID]"

      If not passed, defaults to the project bound to the API’s client.

    • filter (str) – a filter expression. Seehttps://cloud.google.com/logging/docs/view/advanced_filters

    • order_by (str) – orDESCENDING.

    • max_results (Optional[int]) – Optional. The maximum number of entries to return.Non-positive values are treated as 0. If None, uses API defaults.

    • page_size (int) – number of entries to fetch in each API call. Althoughrequests are paged internally, logs are returned by the generatorone at a time. If not passed, defaults to a value set by the API.

    • page_token (str) – opaque marker for the starting “page” of entries. If notpassed, the API will return the first page of entries.

  • Returns

    Generator[~logging_v2.LogEntry]

list_metrics(*, max_results=None, page_size=None, page_token=None)

List metrics for the project associated with this client.

Seehttps://cloud.google.com/logging/docs/reference/v2/rest/v2/projects.metrics/list

  • Parameters

    • max_results (Optional[int]) – Optional. The maximum number of entries to return.Non-positive values are treated as 0. If None, uses API defaults.

    • page_size (int) – number of entries to fetch in each API call. Althoughrequests are paged internally, logs are returned by the generatorone at a time. If not passed, defaults to a value set by the API.

    • page_token (str) – opaque marker for the starting “page” of entries. If notpassed, the API will return the first page of entries.

  • Returns

    Generator[logging_v2.Metric]

list_sinks(*, parent=None, max_results=None, page_size=None, page_token=None)

List sinks for the a parent resource.

Seehttps://cloud.google.com/logging/docs/reference/v2/rest/v2/projects.sinks/list

  • Parameters

    • parent (Optional[str]) – The parent resource whose sinks are to be listed:

      "projects/[PROJECT_ID]""organizations/[ORGANIZATION_ID]""billingAccounts/[BILLING_ACCOUNT_ID]""folders/[FOLDER_ID]".

      If not passed, defaults to the project bound to the API’s client.

    • max_results (Optional[int]) – Optional. The maximum number of entries to return.Non-positive values are treated as 0. If None, uses API defaults.

    • page_size (int) – number of entries to fetch in each API call. Althoughrequests are paged internally, logs are returned by the generatorone at a time. If not passed, defaults to a value set by the API.

    • page_token (str) – opaque marker for the starting “page” of entries. If notpassed, the API will return the first page of entries.

  • Returns

    Generator[~logging_v2.Sink]

logger(name, *, labels=None, resource=None)

Creates a logger bound to the current client.

  • Parameters

    • name (str) – The name of the logger to be constructed.

    • resource (Optional[Resource]) – a monitored resource objectrepresenting the resource the code was run on. If not given, willbe inferred from the environment.

    • labels (Optional[dict]) – Mapping of default labels for entries writtenvia this logger.

  • Returns

    Logger created with the current client.

  • Return type

    Logger

property logging_api()

Helper for logging-related API calls.

Seehttps://cloud.google.com/logging/docs/reference/v2/rest/v2/entrieshttps://cloud.google.com/logging/docs/reference/v2/rest/v2/projects.logs

metric(name, *, filter_=None, description='')

Creates a metric bound to the current client.

  • Parameters

    • name (str) – The name of the metric to be constructed.

    • filter (Optional[str]) – The advanced logs filter expression defining theentries tracked by the metric. If notpassed, the instance should already exist, to berefreshed viaMetric.reload().

    • description (Optional[str]) – The description of the metric to be constructed.If not passed, the instance should already exist,to be refreshed viaMetric.reload().

  • Returns

    Metric created with the current client.

  • Return type

    Metric

property metrics_api()

Helper for log metric-related API calls.

Seehttps://cloud.google.com/logging/docs/reference/v2/rest/v2/projects.metrics

setup_logging(*, log_level=20, excluded_loggers=('google.cloud', 'google.auth', 'google_auth_httplib2', 'google.api_core.bidi', 'werkzeug'), **kw)

Attach default Cloud Logging handler to the root logger.

This method uses the default log handler, obtained byget_default_handler(), and attaches it to the root Pythonlogger, so that a call such aslogging.warn, as well as all childloggers, will report to Cloud Logging.

  • Parameters

    • log_level (Optional[int]) – Python logging log level. Defaults tologging.INFO.

    • excluded_loggers (Optional[Tuple[str]]) – The loggers to not attach thehandler to. This will always include theloggers in the path of the logging clientitself.

  • Returns

    keyword args passed to handler constructor

  • Return type

    dict

sink(name, *, filter_=None, destination=None)

Creates a sink bound to the current client.

  • Parameters

    • name (str) – the name of the sink to be constructed.

    • filter (Optional[str]) – the advanced logs filter expressiondefining the entries exported by the sink. If notpassed, the instance should already exist, to berefreshed viaSink.reload().

    • destination (str) – destination URI for the entries exported bythe sink. If not passed, the instance shouldalready exist, to be refreshed viaSink.reload().

  • Returns

    Sink created with the current client.

  • Return type

    Sink

property sinks_api()

Helper for log sink-related API calls.

Seehttps://cloud.google.com/logging/docs/reference/v2/rest/v2/projects.sinks

Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2026-01-10 UTC.