Stackdriver Logging Client#

Client for interacting with the Google Stackdriver Logging API.

class google.cloud.logging.client.Client(project=None, credentials=None, http=None, use_gax=None)[source]#

Bases: google.cloud.client.ClientWithProject

Client to bundle configuration needed for API requests.

Parameters:
  • project (str) – the project which the client acts on behalf of. If not passed, falls back to the default inferred from the environment.
  • credentials (Credentials) – (Optional) The OAuth2 Credentials to use for this client. If not passed (and if no http object is passed), falls back to the default inferred from the environment.
  • http (Http) – (Optional) HTTP object to make requests. Can be any object that defines request() with the same interface as request(). If not passed, an http object is created that is bound to the credentials for the current object.
  • use_gax (bool) – (Optional) Explicitly specifies whether to use the gRPC transport (via GAX) or HTTP. If unset, falls back to the GOOGLE_CLOUD_DISABLE_GRPC environment variable
SCOPE = ('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()[source]#

Return the default logging handler based on the local environment.

Return type:logging.Handler
Returns:The default log handler based on the environment
list_entries(projects=None, filter_=None, order_by=None, page_size=None, page_token=None)[source]#

Return a page of log entries.

See: https://cloud.google.com/logging/docs/reference/v2/rest/v2/entries/list

Parameters:
  • projects (list of strings) – project IDs to include. If not passed, defaults to the project bound to the client.
  • filter (str) – a filter expression. See: https://cloud.google.com/logging/docs/view/advanced_filters
  • order_by (str) – One of ASCENDING or DESCENDING.
  • page_size (int) – maximum number of entries to return, If not passed, defaults to a value set by the API.
  • page_token (str) – opaque marker for the next “page” of entries. If not passed, the API will return the first page of entries.
Return type:

Iterator

Returns:

Iterator of _BaseEntry accessible to the current client.

list_metrics(page_size=None, page_token=None)[source]#

List metrics for the project associated with this client.

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

Parameters:
  • page_size (int) – maximum number of metrics to return, If not passed, defaults to a value set by the API.
  • page_token (str) – opaque marker for the next “page” of metrics. If not passed, the API will return the first page of metrics.
Return type:

Iterator

Returns:

Iterator of Metric accessible to the current client.

list_sinks(page_size=None, page_token=None)[source]#

List sinks for the project associated with this client.

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

Parameters:
  • page_size (int) – maximum number of sinks to return, If not passed, defaults to a value set by the API.
  • page_token (str) – opaque marker for the next “page” of sinks. If not passed, the API will return the first page of sinks.
Return type:

Iterator

Returns:

Iterator of Sink accessible to the current client.

logger(name)[source]#

Creates a logger bound to the current client.

Parameters:name (str) – the name of the logger to be constructed.
Return type:google.cloud.logging.logger.Logger
Returns:Logger created with the current client.
logging_api#

Helper for logging-related API calls.

See: https://cloud.google.com/logging/docs/reference/v2/rest/v2/entries https://cloud.google.com/logging/docs/reference/v2/rest/v2/projects.logs

metric(name, filter_=None, description='')[source]#

Creates a metric bound to the current client.

Parameters:
  • name (str) – the name of the metric to be constructed.
  • filter (str) – the advanced logs filter expression defining the entries tracked by the metric. If not passed, the instance should already exist, to be refreshed via Metric.reload().
  • description (str) – the description of the metric to be constructed. If not passed, the instance should already exist, to be refreshed via Metric.reload().
Return type:

google.cloud.logging.metric.Metric

Returns:

Metric created with the current client.

metrics_api#

Helper for log metric-related API calls.

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

setup_logging(log_level=20, excluded_loggers=('google.cloud', 'oauth2client'))[source]#

Attach default Stackdriver logging handler to the root logger.

This method uses the default log handler, obtained by get_default_handler(), and attaches it to the root Python logger, so that a call such as logging.warn, as well as all child loggers, will report to Stackdriver logging.

Parameters:
  • log_level (int) – (Optional) Python logging log level. Defaults to logging.INFO.
  • excluded_loggers (tuple) – (Optional) The loggers to not attach the handler to. This will always include the loggers in the path of the logging client itself.
sink(name, filter_=None, destination=None)[source]#

Creates a sink bound to the current client.

Parameters:
  • name (str) – the name of the sink to be constructed.
  • filter (str) – (optional) the advanced logs filter expression defining the entries exported by the sink. If not passed, the instance should already exist, to be refreshed via Sink.reload().
  • destination (str) – destination URI for the entries exported by the sink. If not passed, the instance should already exist, to be refreshed via Sink.reload().
Return type:

google.cloud.logging.sink.Sink

Returns:

Sink created with the current client.

sinks_api#

Helper for log sink-related API calls.

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