Skip to content

Commit f1d6345

Browse files
committed
Review logging doc.
1 parent 73ae74c commit f1d6345

File tree

1 file changed

+28
-22
lines changed

1 file changed

+28
-22
lines changed

docs/topics/logging.rst

+28-22
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ When you run a WebSocket client, your code calls coroutines provided by
1010
websockets.
1111

1212
If an error occurs, websockets tells you by raising an exception. For example,
13-
it raises a :exc:`~exception.ConnectionClosed` exception if the other side
13+
it raises a :exc:`~exceptions.ConnectionClosed` exception if the other side
1414
closes the connection.
1515

1616
When you run a WebSocket server, websockets accepts connections, performs the
@@ -38,12 +38,15 @@ closed`_.
3838
.. _connection is established: https://www.rfc-editor.org/rfc/rfc6455.html#section-4
3939
.. _connection is closed: https://www.rfc-editor.org/rfc/rfc6455.html#section-7.1.4
4040

41-
websockets doesn't log an event for every message because that would be
42-
excessive for many applications exchanging small messages at a fast rate.
43-
However, you could add this level of logging in your own code if necessary.
41+
By default, websockets doesn't log an event for every message. That would be
42+
excessive for many applications exchanging small messages at a fast rate. If
43+
you need this level of detail, you could add logging in your own code.
4444

45-
See :ref:`log levels <log-levels>` below for details of events logged by
46-
websockets at each level.
45+
Finally, you can enable debug logs to get details about everything websockets
46+
is doing. This can be useful when developing clients as well as servers.
47+
48+
See :ref:`log levels <log-levels>` below for a list of events logged by
49+
websockets logs at each log level.
4750

4851
Configure logging
4952
-----------------
@@ -94,13 +97,14 @@ However, this technique runs into two problems:
9497
* Even with :meth:`str.format` style, you're restricted to attribute and index
9598
lookups, which isn't enough to implement some fairly simple requirements.
9699

97-
There's a better way. :func:`~server.serve` accepts a ``logger`` argument to
98-
override the default :class:`~logging.Logger`. You can set ``logger`` to
99-
a :class:`~logging.LoggerAdapter` that enriches logs.
100+
There's a better way. :func:`~client.connect` and :func:`~server.serve` accept
101+
a ``logger`` argument to override the default :class:`~logging.Logger`. You
102+
can set ``logger`` to a :class:`~logging.LoggerAdapter` that enriches logs.
100103

101-
For example, if the server is behind a reverse proxy, ``remote_address`` gives
104+
For example, if the server is behind a reverse
105+
proxy, :attr:`~legacy.protocol.WebSocketCommonProtocol.remote_address` gives
102106
the IP address of the proxy, which isn't useful. IP addresses of clients are
103-
generally available in a HTTP header set by the proxy.
107+
provided in a HTTP header set by the proxy.
104108

105109
Here's how to include them in logs, assuming they're in the
106110
``X-Forwarded-For`` header::
@@ -111,6 +115,7 @@ Here's how to include them in logs, assuming they're in the
111115
)
112116

113117
class LoggerAdapter(logging.LoggerAdapter):
118+
"""Add connection ID and client IP address to websockets logs."""
114119
def process(self, msg, kwargs):
115120
try:
116121
websocket = kwargs["extra"]["websocket"]
@@ -148,6 +153,7 @@ Finally, we populate the ``event_data`` custom attribute in log records with
148153
a :class:`~logging.LoggerAdapter`::
149154

150155
class LoggerAdapter(logging.LoggerAdapter):
156+
"""Add connection ID and client IP address to websockets logs."""
151157
def process(self, msg, kwargs):
152158
try:
153159
websocket = kwargs["extra"]["websocket"]
@@ -169,9 +175,9 @@ Disable logging
169175
---------------
170176

171177
If your application doesn't configure :mod:`logging`, Python outputs messages
172-
of severity :data:`~logging.WARNING` and higher to :data:`~sys.stderr`. As a
173-
consequence, you will see a message and a stack trace if a connection handler
174-
coroutine crashes or if you hit a bug in websockets.
178+
of severity ``WARNING`` and higher to :data:`~sys.stderr`. As a consequence,
179+
you will see a message and a stack trace if a connection handler coroutine
180+
crashes or if you hit a bug in websockets.
175181

176182
If you want to disable this behavior for websockets, you can add
177183
a :class:`~logging.NullHandler`::
@@ -183,8 +189,8 @@ propagation to the root logger, or else its handlers could output logs::
183189

184190
logging.getLogger("websockets").propagate = False
185191

186-
Alternatively, you could set the log level to :data:`~logging.CRITICAL` for
187-
websockets, as the highest level currently used is :data:`~logging.ERROR`::
192+
Alternatively, you could set the log level to ``CRITICAL`` for the
193+
``"websockets"`` logger, as the highest level currently used is ``ERROR``::
188194

189195
logging.getLogger("websockets").setLevel(logging.CRITICAL)
190196

@@ -199,21 +205,21 @@ Log levels
199205

200206
Here's what websockets logs at each level.
201207

202-
:attr:`~logging.ERROR`
203-
......................
208+
``ERROR``
209+
.........
204210

205211
* Exceptions raised by connection handler coroutines in servers
206212
* Exceptions resulting from bugs in websockets
207213

208-
:attr:`~logging.INFO`
209-
.....................
214+
``INFO``
215+
........
210216

211217
* Server starting and stopping
212218
* Server establishing and closing connections
213219
* Client reconnecting automatically
214220

215-
:attr:`~logging.DEBUG`
216-
......................
221+
``DEBUG``
222+
.........
217223

218224
* Changes to the state of connections
219225
* Handshake requests and responses

0 commit comments

Comments
 (0)