Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
a5c36d7
added empty v3.0.0 section to UPGRADING.md
daniel-sanche Dec 7, 2021
0ff7048
reorganized table of contents
daniel-sanche Dec 7, 2021
68e7f9d
marked old handlers as deprecated
daniel-sanche Dec 7, 2021
ff046f5
removed duplicated stdlib doc
daniel-sanche Dec 8, 2021
2e5fd18
made some progress on standard library integration doc
daniel-sanche Dec 8, 2021
51d2a9a
added more stdlib documentation
daniel-sanche Dec 8, 2021
fcc239b
finished up stdlib docs
daniel-sanche Dec 9, 2021
5826ce3
added page for direct API usage
daniel-sanche Dec 9, 2021
205d915
copied old usage guide info
daniel-sanche Dec 9, 2021
98a0a8c
working on setup section
daniel-sanche Dec 10, 2021
117e619
added log writing docs
daniel-sanche Dec 10, 2021
913a08c
removed references to POST requests
daniel-sanche Dec 10, 2021
29ea7d4
added batching info
daniel-sanche Dec 10, 2021
e822bd0
added transport docs
daniel-sanche Dec 10, 2021
8bce609
added grpc vs http docs
daniel-sanche Dec 10, 2021
eac5e2d
added upgrading notes
daniel-sanche Dec 14, 2021
01a0e33
improved manual handler section
daniel-sanche Jan 14, 2022
0526955
fixed lint issues
daniel-sanche Jan 15, 2022
a91b5f4
fixed snippet issue
daniel-sanche Jan 24, 2022
0ad37c8
addressed Drew's comments
daniel-sanche Jan 24, 2022
fd314b9
incorporated Pamela's feedback to migration guide
daniel-sanche Jan 25, 2022
95e5890
updated formatting
daniel-sanche Jan 26, 2022
51a7cd5
updated wording
daniel-sanche Jan 26, 2022
69209b1
changed wording
daniel-sanche Jan 26, 2022
741dfac
addressed Pamela's comments on the direct library usage document
daniel-sanche Jan 26, 2022
49dc3c3
incorporated Pamela's feedback in std lib integration doc
daniel-sanche Jan 26, 2022
ef947d3
removed unneeded delete lines
daniel-sanche Jan 26, 2022
1ff01aa
switched client for tests
daniel-sanche Jan 26, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
added more stdlib documentation
  • Loading branch information
daniel-sanche committed Dec 8, 2021
commit 51d2a9a9bef9eddd2ec7a5d233c08cc83e98aa55
53 changes: 47 additions & 6 deletions docs/std-lib-integration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ standard :mod:`logging` module as normal:

For more information on the library, see the `Google Cloud Logging documentation <https://cloud.google.com/logging/docs/setup/python>`_.
For more information on the Python :mod:`logging` standard library, see the `logging documentation <https://docs.python.org/3/howto/logging.html#a-simple-example>`_

Manual Handler Configuration
-----------------------------

Expand All @@ -50,19 +51,59 @@ There are two supported handler classes to choose from:
to standard out, to be read and parsed by a GCP logging agent
- This is the default handler on Kubernetes Engine, Cloud Functions and Cloud Run

logging JSON poayloads
.. _JSON:

Logging JSON Payloads
----------------------

Although the Python :mod:`logging` standard library `expects all logs to be strings <https://docs.python.org/3/library/logging.html#logging.Logger.debug>`_
Although the Python :mod:`logging` standard library `expects all logs to be strings <https://docs.python.org/3/library/logging.html#logging.Logger.debug>`_,
Google Cloud Logging allows `JSON payload data <https://cloud.google.com/logging/docs/structured-logging>`_.
You can write JSON logs using the standard library integration in one of the following ways:

1. Using the `json_fields` `extra` argument:

.. literalinclude:: ../samples/snippets/usage_guide.py
:start-after: [START logging_extra_json_fields]
:end-before: [END logging_extra_json_fields]
:dedent: 4

2. Logging a JSON-parsable string:

.. literalinclude:: ../samples/snippets/usage_guide.py
:start-after: [START logging_json_dumps]
:end-before: [END logging_json_dumps]
:dedent: 4

Using the `extra` Argument
--------------------------

The Python :mod:`logging` standard library accepts `an "extra" argument <https://docs.python.org/3/library/logging.html#logging.Logger.debug>`_ when
writing logs, which can be used to populate LogRecord objects with user-defined
key-value pairs. Google Cloud Logging uses the `extra` field as a way to pass in
metadata to populate `LogEntry fields <https://cloud.google.com/logging/docs/reference/v2/rest/v2/LogEntry>`_.

.. literalinclude:: ../samples/snippets/usage_guide.py
:start-after: [START logging_extras]
:end-before: [END logging_extras]
:dedent: 4


The following `LogEntry fields <https://cloud.google.com/logging/docs/reference/v2/rest/v2/LogEntry>`_
can be set through the `extra` argument:

- *logging dictionaries*
- labels
- trace
- span_id
- trace_sampled
- http_request
- source_location
- resource
- :ref:`json_fields<JSON>`


:mod:`google-cloud-logging` will also attempt to parse stringified JSON objects logged using the library.
Metadata sent explicitly through the `extra` argument will override any :ref:`automatically detected<Autodetection>` fields.

Using `extras`
--------------
.. _Autodetection:

Automatic Metadata Detection
----------------------------
Expand Down
31 changes: 31 additions & 0 deletions samples/snippets/usage_guide.py
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,37 @@ def logging_handler(client):
handler = CloudLoggingHandler(client, name="mycustomlog")
# [END create_named_handler]

@snippet
def logging_json(client):
# [START logging_json_dumps]
import logging
import json

data_dict = {"hello": "world"}
logging.info(json.dumps(data_dict))
# [END logging_json_dumps]

# [START logging_extra_json_fields]
import logging

data_dict = {"hello": "world"}
logging.info("message field", extra={"json_fields": data_dict})
# [END logging_extra_json_fields]

@snippet
def using_extras(client):
# [START logging_extras]
my_labels = {"foo": "bar"}
my_http = {"requestUrl": "localhost"}
my_trace = "01234"

logging.info("hello", extra={
"labels": my_labels,
"http_request": my_http,
"trace": my_trace
})
# [END logging_extras]


@snippet
def setup_logging(client):
Expand Down