Skip to content

Commit 9b27c71

Browse files
MRLab12vytas7
andauthored
docs(asgi-tutorial): include info on setting up logging for debugging (#2223)
* Update tutorial-asgi to include info on setting up logging for debugging an application * Add Python logging docs link/add note that debugging asgi also applies to wsgi * 80 char limit / changed logging example to show use with falcon / added intersphinx * docs: update tutorial-asgi.rst --------- Co-authored-by: Vytautas Liuolia <[email protected]>
1 parent c4a5f32 commit 9b27c71

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

docs/user/tutorial-asgi.rst

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -964,6 +964,54 @@ adding ``--cov-fail-under=100`` (or any other percent threshold) to our
964964
tests in multiple environments would most probably involve running
965965
``coverage`` directly, and combining results.
966966

967+
Debugging ASGI Applications
968+
---------------------------
969+
(This section also applies to WSGI applications)
970+
971+
While developing and testing ASGI applications, understanding how to configure
972+
and utilize logging can be helpful, especially when you encounter unexpected
973+
issues or behaviors.
974+
975+
By default, Falcon does not set up logging for you,
976+
but Python's built-in :mod:`logging` module provides a flexible framework for
977+
emitting and capturing log messages. Here's how you can set up basic logging in
978+
your ASGI Falcon application:
979+
980+
.. code:: python
981+
982+
import falcon
983+
import logging
984+
985+
logging.basicConfig(level=logging.INFO)
986+
987+
class ErrorResource:
988+
def on_get(self, req, resp):
989+
raise Exception('Something went wrong!')
990+
991+
app = falcon.App()
992+
app.add_route('/error', ErrorResource())
993+
994+
995+
When the above route is accessed, Falcon will catch the unhandled exception and
996+
automatically log an error message. Below is an example of what the log output
997+
might look like:
998+
999+
.. code-block:: none
1000+
1001+
ERROR:falcon.asgi.app:Unhandled exception in ASGI application
1002+
Traceback (most recent call last):
1003+
File "path/to/falcon/app.py", line 123, in __call__
1004+
resp = resource.on_get(req, resp)
1005+
File "/path/to/your/app.py", line 7, in on_get
1006+
raise Exception("Something went wrong!")
1007+
Exception: Something went wrong!
1008+
1009+
1010+
.. note::
1011+
While logging is helpful for development and debugging, be mindful of logging
1012+
sensitive information. Ensure that log files are stored securely and are not
1013+
accessible to unauthorized users.
1014+
9671015
What Now?
9681016
---------
9691017

0 commit comments

Comments
 (0)