On January 1, 2020 this library will no longer support Python 2 on the latest released version. Previously released library versions will continue to be available. For more information please visit Python 2 support on Google Cloud.

Logger

Define API Loggers.

class google.cloud.logging.logger.Batch(logger, client, resource=None)[source]

Bases: object

Context manager: collect entries to log via a single API call.

Helper returned by Logger.batch()

Parameters
  • logger (google.cloud.logging.logger.Logger) – the logger to which entries will be logged.

  • client (google.cloud.logging.client.Client) – The client to use.

  • resource (Resource) – (Optional) Monitored resource of the batch, defaults to None, which requires that every entry should have a resource specified. Since the methods used to write entries default the entry’s resource to the global resource type, this parameter is only required if explicitly set to None. If no entries’ resource are set to None, this parameter will be ignored on the server.

commit(client=None)[source]

Send saved log entries as a single API call.

Parameters

client (Client or NoneType) – the client to use. If not passed, falls back to the client stored on the current batch.

log_empty(**kw)[source]

Add a entry without payload to be logged during commit().

Parameters

kw (dict) – (optional) additional keyword arguments for the entry. See LogEntry.

log_proto(message, **kw)[source]

Add a protobuf entry to be logged during commit().

Parameters
  • message (protobuf message) – the protobuf entry

  • kw (dict) – (optional) additional keyword arguments for the entry. See LogEntry.

log_struct(info, **kw)[source]

Add a struct entry to be logged during commit().

Parameters
  • info (dict) – the struct entry

  • kw (dict) – (optional) additional keyword arguments for the entry. See LogEntry.

log_text(text, **kw)[source]

Add a text entry to be logged during commit().

Parameters
  • text (str) – the text entry

  • kw (dict) – (optional) additional keyword arguments for the entry. See LogEntry.

class google.cloud.logging.logger.Logger(name, client, labels=None)[source]

Bases: object

Loggers represent named targets for log entries.

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

Parameters
  • name (str) – the name of the logger

  • client (google.cloud.logging.client.Client) – A client which holds credentials and project configuration for the logger (which requires a project).

  • labels (dict) – (optional) mapping of default labels for entries written via this logger.

batch(client=None)[source]

Return a batch to use as a context manager.

Parameters

client (Client or NoneType) – the client to use. If not passed, falls back to the client stored on the current topic.

Return type

Batch

Returns

A batch to use as a context manager.

property client

Clent bound to the logger.

delete(client=None)[source]

API call: delete all entries in a logger via a DELETE request

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

Parameters

client (Client or NoneType) – the client to use. If not passed, falls back to the client stored on the current logger.

property full_name

Fully-qualified name used in logging APIs

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) – Optional. The maximum number of entries in each page of results from this request. Non-positive values are ignored. Defaults to a sensible value set by the API.

  • page_token (str) – Optional. If present, return the next batch of entries, using the value, which must correspond to the nextPageToken value returned in the previous response. Deprecated: use the pages property of the returned iterator instead of manually passing the token.

Return type

Iterator

Returns

Iterator of log entries accessible to the current logger. See LogEntry.

log_empty(client=None, **kw)[source]

API call: log an empty message via a POST request

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

Parameters
  • client (Client or NoneType) – the client to use. If not passed, falls back to the client stored on the current logger.

  • kw (dict) – (optional) additional keyword arguments for the entry. See LogEntry.

log_proto(message, client=None, **kw)[source]

API call: log a protobuf message via a POST request

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

Parameters
  • message (Message) – The protobuf message to be logged.

  • client (Client or NoneType) – the client to use. If not passed, falls back to the client stored on the current logger.

  • kw (dict) – (optional) additional keyword arguments for the entry. See LogEntry.

log_struct(info, client=None, **kw)[source]

API call: log a structured message via a POST request

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

Parameters
  • info (dict) – the log entry information

  • client (Client or NoneType) – the client to use. If not passed, falls back to the client stored on the current logger.

  • kw (dict) – (optional) additional keyword arguments for the entry. See LogEntry.

log_text(text, client=None, **kw)[source]

API call: log a text message via a POST request

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

Parameters
  • text (str) – the log message.

  • client (Client or NoneType) – the client to use. If not passed, falls back to the client stored on the current logger.

  • kw (dict) – (optional) additional keyword arguments for the entry. See LogEntry.

property path

URI path for use in logging APIs

property project

Project bound to the logger.