@@ -10,7 +10,7 @@ When you run a WebSocket client, your code calls coroutines provided by
10
10
websockets.
11
11
12
12
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
14
14
closes the connection.
15
15
16
16
When you run a WebSocket server, websockets accepts connections, performs the
@@ -38,12 +38,15 @@ closed`_.
38
38
.. _connection is established : https://www.rfc-editor.org/rfc/rfc6455.html#section-4
39
39
.. _connection is closed : https://www.rfc-editor.org/rfc/rfc6455.html#section-7.1.4
40
40
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.
44
44
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.
47
50
48
51
Configure logging
49
52
-----------------
@@ -94,13 +97,14 @@ However, this technique runs into two problems:
94
97
* Even with :meth: `str.format ` style, you're restricted to attribute and index
95
98
lookups, which isn't enough to implement some fairly simple requirements.
96
99
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.
100
103
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
102
106
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.
104
108
105
109
Here's how to include them in logs, assuming they're in the
106
110
``X-Forwarded-For `` header::
@@ -111,6 +115,7 @@ Here's how to include them in logs, assuming they're in the
111
115
)
112
116
113
117
class LoggerAdapter(logging.LoggerAdapter):
118
+ """Add connection ID and client IP address to websockets logs."""
114
119
def process(self, msg, kwargs):
115
120
try:
116
121
websocket = kwargs["extra"]["websocket"]
@@ -148,6 +153,7 @@ Finally, we populate the ``event_data`` custom attribute in log records with
148
153
a :class: `~logging.LoggerAdapter `::
149
154
150
155
class LoggerAdapter(logging.LoggerAdapter):
156
+ """Add connection ID and client IP address to websockets logs."""
151
157
def process(self, msg, kwargs):
152
158
try:
153
159
websocket = kwargs["extra"]["websocket"]
@@ -169,9 +175,9 @@ Disable logging
169
175
---------------
170
176
171
177
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.
175
181
176
182
If you want to disable this behavior for websockets, you can add
177
183
a :class: `~logging.NullHandler `::
@@ -183,8 +189,8 @@ propagation to the root logger, or else its handlers could output logs::
183
189
184
190
logging.getLogger("websockets").propagate = False
185
191
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 ` `::
188
194
189
195
logging.getLogger("websockets").setLevel(logging.CRITICAL)
190
196
@@ -199,21 +205,21 @@ Log levels
199
205
200
206
Here's what websockets logs at each level.
201
207
202
- :attr: ` ~logging. ERROR `
203
- ......................
208
+ `` ERROR ` `
209
+ .........
204
210
205
211
* Exceptions raised by connection handler coroutines in servers
206
212
* Exceptions resulting from bugs in websockets
207
213
208
- :attr: ` ~logging. INFO `
209
- .....................
214
+ `` INFO ` `
215
+ ........
210
216
211
217
* Server starting and stopping
212
218
* Server establishing and closing connections
213
219
* Client reconnecting automatically
214
220
215
- :attr: ` ~logging. DEBUG `
216
- ......................
221
+ `` DEBUG ` `
222
+ .........
217
223
218
224
* Changes to the state of connections
219
225
* Handshake requests and responses
0 commit comments