# Logger

3.16.0 (latest) 3.15.0 3.13.0 3.12.1 3.11.3 3.10.0 3.9.0 3.8.0 3.7.0 3.6.0 3.5.0 3.4.0 3.3.1 3.2.5 3.1.2 3.0.0 2.7.2 2.6.0 2.5.0 2.4.0 2.3.1 2.2.0 2.1.1 2.0.2 1.15.3 1.14.0 1.13.0 1.12.1 Define API Loggers.

### *class* google.cloud.logging_v2.logger.Batch(logger, client, \*, resource=None)

Bases: [`object`](https://docs.python.org/3/library/functions.html#object)

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

Helper returned by `Logger.batch()`

- **Parameters**

  - **logger** (*logging_v2.logger.Logger*) -- the logger to which entries will be logged.

  - **client** (*Client*) -- The client to use.

  - **resource** (*Optional* *\[* [*Resource*](https://docs.cloud.google.com/python/docs/reference/logging/latest/resource#google.cloud.logging_v2.resource.Resource)*\]*) -- 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, partial_success=True)

Send saved log entries as a single API call.

- **Parameters**

  - **client** (*Optional* *\[* [*Client*](https://docs.cloud.google.com/python/docs/reference/logging/latest/client#google.cloud.logging_v2.client.Client)*\]* ) -- The client to use. If not passed, falls back to the
    `client` stored on the current batch.

  - **partial_success** (*Optional* *\[* [*bool*](https://docs.python.org/3/library/functions.html#bool)*\]*) -- Whether a batch's valid entries should be written even
    if some other entry failed due to a permanent error such
    as INVALID_ARGUMENT or PERMISSION_DENIED.

- **Raises**

  [**ValueError**](https://docs.python.org/3/library/exceptions.html#ValueError) -- if one of the messages in the batch cannot be successfully parsed.

#### log(message=None, \*\*kw)

Add an arbitrary message to be logged during `commit()`.
Type will be inferred based on the input message.

- **Parameters**

  - **message** (*Optional* *\[* [*str*](https://docs.python.org/3/library/stdtypes.html#str)\* or *\[* dict*\](<https://docs.python.org/3/library/stdtypes.html#dict>)* or **google.protobuf.Message**\]\*) -- The message. to log

  - **kw** (*Optional* *\[* [*dict*](https://docs.python.org/3/library/stdtypes.html#dict)*\]* ) -- Additional keyword arguments for the entry.
    See `LogEntry`.

#### log_empty(\*\*kw)

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

- **Parameters**

  **kw** (*Optional* *\[* [*dict*](https://docs.python.org/3/library/stdtypes.html#dict)*\]* ) -- Additional keyword arguments for the entry.
  See `LogEntry`.

#### log_proto(message, \*\*kw)

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

- **Parameters**

  - **message** (*google.protobuf.Message*) -- The protobuf entry.

  - **kw** (*Optional* *\[* [*dict*](https://docs.python.org/3/library/stdtypes.html#dict)*\]* ) -- Additional keyword arguments for the entry.
    See `LogEntry`.

#### log_struct(info, \*\*kw)

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

The message must be able to be serializable to a Protobuf Struct.
It must be a dictionary of strings to one of the following:
> - [`str`](https://docs.python.org/3/library/stdtypes.html#str)
>
> - [`int`](https://docs.python.org/3/library/functions.html#int)
>
> - [`float`](https://docs.python.org/3/library/functions.html#float)
>
> - [`bool`](https://docs.python.org/3/library/functions.html#bool)
>
> - `list[str|float|int|bool|list|dict|None]`
>
> - `dict[str, str|float|int|bool|list|dict|None]`

For more details on Protobuf structs, see <https://protobuf.dev/reference/protobuf/google.protobuf/#value>.
If the provided dictionary cannot be serialized into a Protobuf struct,
it will not be logged, and a [`ValueError`](https://docs.python.org/3/library/exceptions.html#ValueError) will be raised during `commit()`.

- **Parameters**

  - **info** ([*dict*](https://docs.python.org/3/library/stdtypes.html#dict)*\[* [*str*](https://docs.python.org/3/library/stdtypes.html#str)*, \*\[* str*\](<https://docs.python.org/3/library/stdtypes.html#str>)* \|*\[* float*\](<https://docs.python.org/3/library/functions.html#float>)* \|*\[* int*\](<https://docs.python.org/3/library/functions.html#int>)* \|*\[* bool*\](<https://docs.python.org/3/library/functions.html#bool>)* \|*\[* list*\](<https://docs.python.org/3/library/stdtypes.html#list>)* \|*\[* dict*\](<https://docs.python.org/3/library/stdtypes.html#dict>)* \|**None**\]\*) -- The struct entry,

  - **kw** (*Optional* *\[* [*dict*](https://docs.python.org/3/library/stdtypes.html#dict)*\]* ) -- Additional keyword arguments for the entry.
    See `LogEntry`.

#### log_text(text, \*\*kw)

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

- **Parameters**

  - **text** ([*str*](https://docs.python.org/3/library/stdtypes.html#str)) -- the text entry

  - **kw** (*Optional* *\[* [*dict*](https://docs.python.org/3/library/stdtypes.html#dict)*\]* ) -- Additional keyword arguments for the entry.
    See `LogEntry`.

### *class* google.cloud.logging_v2.logger.Logger(name, client, \*, labels=None, resource=None)

Bases: [`object`](https://docs.python.org/3/library/functions.html#object)

Loggers represent named targets for log entries.

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

- **Parameters**

  - **name** ([*str*](https://docs.python.org/3/library/stdtypes.html#str)) -- The name of the logger.

  - **client** ([*Client*](https://docs.cloud.google.com/python/docs/reference/logging/latest/client#google.cloud.logging_v2.client.Client)) -- A client which holds credentials and project configuration
    for the logger (which requires a project).

  - **resource** (*Optional* *\[* *Resource* *\]*) -- a monitored resource object
    representing the resource the code was run on. If not given, will
    be inferred from the environment.

  - **labels** (*Optional* *\[* [*dict*](https://docs.python.org/3/library/stdtypes.html#dict)*\]*) -- Mapping of default labels for entries written
    via this logger.

#### batch(\*, client=None)

Return a batch to use as a context manager.

- **Parameters**

  **client** (*Union* *\[* *None* *, \*\[* Client*\](client.md#google.cloud.logging_v2.client.Client)* \]\*) -- The client to use. If not passed, falls back to the
  `client` stored on the current sink.
- **Returns**

  A batch to use as a context manager.
- **Return type**

  Batch

#### *property* client()

Clent bound to the logger.

#### delete(logger_name=None, \*, client=None)

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**

  - **logger_name** (*Optional* *\[* [*str*](https://docs.python.org/3/library/stdtypes.html#str)*\]*) -- The resource name of the log to delete:

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

    `[LOG_ID]` must be URL-encoded. For example,
    `"projects/my-project-id/logs/syslog"`,
    `"organizations/1234567890/logs/cloudresourcemanager.googleapis.com%2Factivity"`.
    If not passed, defaults to the project bound to the client.
  - **client** (*Optional* *\[* [*Client*](https://docs.cloud.google.com/python/docs/reference/logging/latest/client#google.cloud.logging_v2.client.Client)*\]* ) -- 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(\*, resource_names=None, filter_=None, order_by=None, max_results=None, page_size=None, page_token=None)

Return a generator of log entry resources.

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

- **Parameters**

  - **resource_names** (*Optional* *\[* *Sequence* *\[* [*str*](https://docs.python.org/3/library/stdtypes.html#str)*\]* *\]*) -- Names of one or more parent resources
    from 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 client.
  - **filter** (*Optional* *\[* [*str*](https://docs.python.org/3/library/stdtypes.html#str)*\]* ) -- a filter expression. See
    <https://cloud.google.com/logging/docs/view/advanced_filters>
    By default, a 24 hour filter is applied.

  - **order_by** (*Optional* *\[* [*str*](https://docs.python.org/3/library/stdtypes.html#str)*\]* ) -- One of `ASCENDING`
    or `DESCENDING`.

  - **max_results** (*Optional* *\[* [*int*](https://docs.python.org/3/library/functions.html#int)*\]*) -- Optional. The maximum number of entries to return.
    Non-positive values are treated as 0. If None, uses API defaults.

  - **page_size** ([*int*](https://docs.python.org/3/library/functions.html#int)) -- number of entries to fetch in each API call. Although
    requests are paged internally, logs are returned by the generator
    one at a time. If not passed, defaults to a value set by the API.

  - **page_token** ([*str*](https://docs.python.org/3/library/stdtypes.html#str)) -- opaque marker for the starting "page" of entries. If not
    passed, the API will return the first page of entries.

- **Returns**

  Generator\[\~logging_v2.LogEntry\]

#### log(message=None, \*, client=None, \*\*kw)

Log an arbitrary message. Type will be inferred based on the input.

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

- **Parameters**

  - **message** (*Optional* *\[* [*str*](https://docs.python.org/3/library/stdtypes.html#str)\* or *\[* dict*\](<https://docs.python.org/3/library/stdtypes.html#dict>)* or **google.protobuf.Message**\]\*) -- The message. to log

  - **client** (*Optional* *\[* [*Client*](https://docs.cloud.google.com/python/docs/reference/logging/latest/client#google.cloud.logging_v2.client.Client)*\]* ) -- The client to use. If not passed, falls back to the
    `client` stored on the current sink.

  - **kw** (*Optional* *\[* [*dict*](https://docs.python.org/3/library/stdtypes.html#dict)*\]* ) -- additional keyword arguments for the entry.
    See `LogEntry`.

#### log_empty(\*, client=None, \*\*kw)

Log an empty message

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

- **Parameters**

  - **client** (*Optional* *\[* [*Client*](https://docs.cloud.google.com/python/docs/reference/logging/latest/client#google.cloud.logging_v2.client.Client)*\]* ) -- The client to use. If not passed, falls back to the
    `client` stored on the current sink.

  - **kw** (*Optional* *\[* [*dict*](https://docs.python.org/3/library/stdtypes.html#dict)*\]* ) -- additional keyword arguments for the entry.
    See `LogEntry`.

#### log_proto(message, \*, client=None, \*\*kw)

Log a protobuf message

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

- **Parameters**

  - **message** ([*google.protobuf.message.Message*](https://googleapis.dev/python/protobuf/latest/google/protobuf/message.html#google.protobuf.message.Message)) -- The protobuf message to be logged.

  - **client** (*Optional* *\[* [*Client*](https://docs.cloud.google.com/python/docs/reference/logging/latest/client#google.cloud.logging_v2.client.Client)*\]* ) -- The client to use. If not passed, falls back to the
    `client` stored on the current sink.

  - **kw** (*Optional* *\[* [*dict*](https://docs.python.org/3/library/stdtypes.html#dict)*\]* ) -- additional keyword arguments for the entry.
    See `LogEntry`.

#### log_struct(info, \*, client=None, \*\*kw)

Logs a dictionary message.

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

The message must be able to be serializable to a Protobuf Struct.
It must be a dictionary of strings to one of the following:
> - [`str`](https://docs.python.org/3/library/stdtypes.html#str)
>
> - [`int`](https://docs.python.org/3/library/functions.html#int)
>
> - [`float`](https://docs.python.org/3/library/functions.html#float)
>
> - [`bool`](https://docs.python.org/3/library/functions.html#bool)
>
> - `list[str|float|int|bool|list|dict|None]`
>
> - `dict[str, str|float|int|bool|list|dict|None]`

For more details on Protobuf structs, see <https://protobuf.dev/reference/protobuf/google.protobuf/#value>.
If the provided dictionary cannot be serialized into a Protobuf struct,
it will not be logged, and a [`ValueError`](https://docs.python.org/3/library/exceptions.html#ValueError) will be raised.

- **Parameters**

  - **info** ([*dict*](https://docs.python.org/3/library/stdtypes.html#dict)*\[* [*str*](https://docs.python.org/3/library/stdtypes.html#str)*, \*\[* str*\](<https://docs.python.org/3/library/stdtypes.html#str>)* \|*\[* float*\](<https://docs.python.org/3/library/functions.html#float>)* \|*\[* int*\](<https://docs.python.org/3/library/functions.html#int>)* \|*\[* bool*\](<https://docs.python.org/3/library/functions.html#bool>)* \|*\[* list*\](<https://docs.python.org/3/library/stdtypes.html#list>)* \|*\[* dict*\](<https://docs.python.org/3/library/stdtypes.html#dict>)* \|**None**\]\*) -- the log entry information.

  - **client** (*Optional* *\[* [*Client*](https://docs.cloud.google.com/python/docs/reference/logging/latest/client#google.cloud.logging_v2.client.Client)*\]* ) -- The client to use. If not passed, falls back to the
    `client` stored on the current sink.

  - **kw** (*Optional* *\[* [*dict*](https://docs.python.org/3/library/stdtypes.html#dict)*\]* ) -- additional keyword arguments for the entry.
    See `LogEntry`.

- **Raises**

  [**ValueError**](https://docs.python.org/3/library/exceptions.html#ValueError) -- if the dictionary message provided cannot be serialized into a Protobuf
  struct.

#### log_text(text, \*, client=None, \*\*kw)

Log a text message

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

- **Parameters**

  - **text** ([*str*](https://docs.python.org/3/library/stdtypes.html#str)) -- the log message

  - **client** (*Optional* *\[* [*Client*](https://docs.cloud.google.com/python/docs/reference/logging/latest/client#google.cloud.logging_v2.client.Client)*\]* ) -- The client to use. If not passed, falls back to the
    `client` stored on the current sink.

  - **kw** (*Optional* *\[* [*dict*](https://docs.python.org/3/library/stdtypes.html#dict)*\]* ) -- additional keyword arguments for the entry.
    See `LogEntry`.

#### *property* path()

URI path for use in logging APIs

#### *property* project()

Project bound to the logger.