@@ -115,6 +115,68 @@ Woohoo, it works!!!
115
115
116
116
Well, sort of. Onwards to adding some real functionality!
117
117
118
+ .. _debugging_asgi_applications :
119
+
120
+ Debugging ASGI Applications
121
+ ---------------------------
122
+
123
+ While developing and testing a Falcon ASGI application along the lines of this
124
+ tutorial, you may encounter unexpected issues or behaviors, be it a copy-paste
125
+ mistake, an idea that didn't work out, or unusual input where validation falls
126
+ outside of the scope of this tutorial.
127
+
128
+ Unlike WSGI, the ASGI specification has no standard mechanism for logging
129
+ errors back to the application server, so Falcon falls back to the stdlib's
130
+ :mod: `logging ` (using the ``falcon `` :class: `logger <logging.Logger> `).
131
+
132
+ As a well-behaved library, Falcon does not configure any loggers since that
133
+ might interfere with the user's logging setup.
134
+ Here's how you can set up basic logging in your ASGI Falcon application via
135
+ :func: `logging.basicConfig `:
136
+
137
+ .. code :: python
138
+
139
+ import logging
140
+
141
+ import falcon
142
+
143
+ logging.basicConfig(level = logging.INFO )
144
+
145
+
146
+ class ErrorResource :
147
+ def on_get (self , req , resp ):
148
+ raise Exception (' Something went wrong!' )
149
+
150
+
151
+ app = falcon.App()
152
+ app.add_route(' /error' , ErrorResource())
153
+
154
+ When the above route is accessed, Falcon will catch the unhandled exception and
155
+ automatically log an error message. Below is an example of what the log output
156
+ might look like:
157
+
158
+ .. code-block :: none
159
+
160
+ ERROR:falcon.asgi.app:Unhandled exception in ASGI application
161
+ Traceback (most recent call last):
162
+ File "/path/to/your/app.py", line 123, in __call__
163
+ resp = resource.on_get(req, resp)
164
+ File "/path/to/your/app.py", line 7, in on_get
165
+ raise Exception("Something went wrong!")
166
+ Exception: Something went wrong!
167
+
168
+ .. note ::
169
+ While logging is helpful for development and debugging, be mindful of
170
+ logging sensitive information. Ensure that log files are stored securely
171
+ and are not accessible to unauthorized users.
172
+
173
+ .. note ::
174
+ Unhandled errors are only logged automatically by Falcon's default error
175
+ handler for :class: `Exception `. If you
176
+ :meth: `replace this handler <falcon.asgi.App.add_error_handler> ` with your
177
+ own generic :class: `Exception ` handler, you are responsible for logging or
178
+ reporting these errors yourself.
179
+
118
180
.. _asgi_tutorial_config :
119
181
120
182
Configuration
@@ -963,54 +1025,6 @@ adding ``--cov-fail-under=100`` (or any other percent threshold) to our
963
1025
tests in multiple environments would most probably involve running
964
1026
``coverage `` directly, and combining results.
965
1027
966
- .. _debugging-asgi-applications :
967
-
968
- Debugging ASGI Applications
969
- ---------------------------
970
- (This section also applies to WSGI applications)
971
-
972
- While developing and testing ASGI applications, understanding how to configure
973
- and utilize logging can be helpful, especially when you encounter unexpected
974
- issues or behaviors.
975
-
976
- By default, Falcon does not set up logging for you,
977
- but Python's built-in :mod: `logging ` module provides a flexible framework for
978
- emitting and capturing log messages. Here's how you can set up basic logging in
979
- your ASGI Falcon application:
980
-
981
- .. code :: python
982
-
983
- import logging
984
- import falcon
985
-
986
- logging.basicConfig(level = logging.INFO )
987
-
988
- class ErrorResource :
989
- def on_get (self , req , resp ):
990
- raise Exception (' Something went wrong!' )
991
-
992
- app = falcon.App()
993
- app.add_route(' /error' , ErrorResource())
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
- .. note ::
1010
- While logging is helpful for development and debugging, be mindful of logging
1011
- sensitive information. Ensure that log files are stored securely and are not
1012
- accessible to unauthorized users.
1013
-
1014
1028
What Now?
1015
1029
---------
1016
1030
0 commit comments