Skip to content

Commit c65ec92

Browse files
docs: Added documentation for Django/Flask integrations and dictConfig (#848)
* docs: Added documentation for Django/Flask integrations and dictConfig * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * Added product prefix to new snippet * Added client setup in sample + link to settings in documentation * Changed django links to point to `/stable/` links --------- Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
1 parent 1216cf6 commit c65ec92

File tree

6 files changed

+78
-5
lines changed

6 files changed

+78
-5
lines changed

README.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ Python >= 3.7
6161

6262
Unsupported Python Versions
6363
^^^^^^^^^^^^^^^^^^^^^^^^^^^
64-
Python == 2.7. The last version of the library compatible with Python 2.7 is `google-cloud-logging==1.15.1`.
65-
Python == 3.6. The last version of the library compatible with Python 3.6 is `google-cloud-logging==3.1.2`.
64+
| Python == 2.7. The last version of the library compatible with Python 2.7 is ``google-cloud-logging==1.15.1``.
65+
| Python == 3.6. The last version of the library compatible with Python 3.6 is ``google-cloud-logging==3.1.2``.
6666
6767

6868
Mac/Linux

docs/std-lib-integration.rst

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,16 @@ There are two supported handler classes to choose from:
4444
to standard out, to be read and parsed by a GCP logging agent
4545
- This is the default handler on Kubernetes Engine, Cloud Functions and Cloud Run
4646

47+
Handler classes can also be specified via `dictConfig <https://docs.python.org/3/library/logging.config.html#logging-config-dictschema>`_:
48+
49+
.. literalinclude:: ../samples/snippets/usage_guide.py
50+
:start-after: [START logging_dict_config]
51+
:end-before: [END logging_dict_config]
52+
:dedent: 4
53+
54+
Note that since :class:`~google.cloud.logging_v2.handlers.handlers.CloudLoggingHandler` requires an already initialized :class:`~google.cloud.logging_v2.client.Client`,
55+
you must initialize a client and include it in the dictConfig entry for a `CloudLoggingHandler`.
56+
4757
Standard Library
4858
---------------------------
4959

@@ -101,8 +111,7 @@ The following fields are currently supported:
101111
- :ref:`json_fields<JSON>`
102112

103113
.. note::
104-
Fields marked with "*" require a supported Python web framework. The Google Cloud Logging
105-
library currently supports `flask <https://flask.palletsprojects.com/>`_ and `django <https://www.djangoproject.com/>`_
114+
Fields marked with "*" require a :doc:`supported Python web framework </web-framework-integration>`.
106115

107116
Manual Metadata Using the `extra` Argument
108117
--------------------------------------------

docs/usage.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ Usage Guide
44
:maxdepth: 2
55

66
std-lib-integration
7+
web-framework-integration
78
direct-lib-usage
89
grpc-vs-http
910

docs/web-framework-integration.rst

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
Integration with Python Web Frameworks
2+
======================================
3+
4+
The Google Cloud Logging library can integrate with Python web frameworks
5+
`flask <https://flask.palletsprojects.com/>`_ and `django <https://www.djangoproject.com/>`_ to
6+
automatically populate `LogEntry fields <https://cloud.google.com/logging/docs/reference/v2/rest/v2/LogEntry>`_
7+
`trace`, `span_id`, `trace_sampled`, and `http_request`.
8+
9+
Django
10+
------
11+
12+
Django integration has been tested to work with each of the Django/Python versions listed `here <https://docs.djangoproject.com/en/stable/faq/install/#what-python-version-can-i-use-with-django>`_.
13+
To enable Django integration, add `google.cloud.logging_v2.handlers.middleware.RequestMiddleware` to the list of `MIDDLEWARE`
14+
in your `settings <https://docs.djangoproject.com/en/stable/topics/settings/>`_ file. Also be sure to :doc:`set up logging </std-lib-integration>` in your settings file.
15+
16+
Flask
17+
-----
18+
19+
Flask integration has been tested to work with the following versions of Flask:
20+
21+
=============== ==============
22+
Python version Flask versions
23+
=============== ==============
24+
3.7 >=1.0.0
25+
3.8 >=1.0.0
26+
3.9 >=1.0.0
27+
3.10 >=1.0.3
28+
3.11 >=1.0.3
29+
3.12 >=1.0.3
30+
=============== ==============
31+
32+
Be sure to :doc:`set up logging </std-lib-integration>` before declaring the Flask app.

google/cloud/logging_v2/handlers/_helpers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ def get_request_data_from_flask():
6666
Returns:
6767
Tuple[Optional[dict], Optional[str], Optional[str], bool]:
6868
Data related to the current http request, trace_id, span_id and trace_sampled
69-
for the request. All fields will be None if a django request isn't found.
69+
for the request. All fields will be None if a Flask request isn't found.
7070
"""
7171
if flask is None or not flask.request:
7272
return None, None, None, False

samples/snippets/usage_guide.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,37 @@ def setup_logging(client):
484484
# [END setup_logging_excludes]
485485

486486

487+
@snippet
488+
def logging_dict_config(client):
489+
import logging.config
490+
491+
# [START logging_dict_config]
492+
import google.cloud.logging
493+
494+
client = google.cloud.logging.Client()
495+
496+
LOGGING = {
497+
"version": 1,
498+
"handlers": {
499+
"cloud_logging": {
500+
"class": "google.cloud.logging.handlers.CloudLoggingHandler",
501+
"client": client,
502+
},
503+
"structured_log": {
504+
"class": "google.cloud.logging.handlers.StructuredLogHandler"
505+
},
506+
},
507+
"root": {"handlers": ["console"], "level": "WARNING"},
508+
"loggers": {
509+
"my_logger": {"handlers": ["cloud_logging"], "level": "INFO"},
510+
"my_other_logger": {"handlers": ["structured_log"], "level": "INFO"},
511+
},
512+
}
513+
# [END logging_dict_config]
514+
515+
logging.config.dictConfig(LOGGING)
516+
517+
487518
def _line_no(func):
488519
return func.__code__.co_firstlineno
489520

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy