1
- Log contexts
1
+ Log Contexts
2
2
============
3
3
4
4
.. contents ::
@@ -12,7 +12,7 @@ record.
12
12
Logcontexts are also used for CPU and database accounting, so that we can track
13
13
which requests were responsible for high CPU use or database activity.
14
14
15
- The ``synapse.util.logcontext `` module provides a facilities for managing the
15
+ The ``synapse.logging.context `` module provides a facilities for managing the
16
16
current log context (as well as providing the ``LoggingContextFilter `` class).
17
17
18
18
Deferreds make the whole thing complicated, so this document describes how it
@@ -27,19 +27,19 @@ found them:
27
27
28
28
.. code :: python
29
29
30
- from synapse.util import logcontext # omitted from future snippets
30
+ from synapse.logging import context # omitted from future snippets
31
31
32
32
def handle_request (request_id ):
33
- request_context = logcontext .LoggingContext()
33
+ request_context = context .LoggingContext()
34
34
35
- calling_context = logcontext .LoggingContext.current_context()
36
- logcontext .LoggingContext.set_current_context(request_context)
35
+ calling_context = context .LoggingContext.current_context()
36
+ context .LoggingContext.set_current_context(request_context)
37
37
try :
38
38
request_context.request = request_id
39
39
do_request_handling()
40
40
logger.debug(" finished" )
41
41
finally :
42
- logcontext .LoggingContext.set_current_context(calling_context)
42
+ context .LoggingContext.set_current_context(calling_context)
43
43
44
44
def do_request_handling ():
45
45
logger.debug(" phew" ) # this will be logged against request_id
@@ -51,7 +51,7 @@ written much more succinctly as:
51
51
.. code :: python
52
52
53
53
def handle_request (request_id ):
54
- with logcontext .LoggingContext() as request_context:
54
+ with context .LoggingContext() as request_context:
55
55
request_context.request = request_id
56
56
do_request_handling()
57
57
logger.debug(" finished" )
@@ -74,7 +74,7 @@ blocking operation, and returns a deferred:
74
74
75
75
@defer.inlineCallbacks
76
76
def handle_request (request_id ):
77
- with logcontext .LoggingContext() as request_context:
77
+ with context .LoggingContext() as request_context:
78
78
request_context.request = request_id
79
79
yield do_request_handling()
80
80
logger.debug(" finished" )
@@ -179,7 +179,7 @@ though, we need to make up a new Deferred, or we get a Deferred back from
179
179
external code. We need to make it follow our rules.
180
180
181
181
The easy way to do it is with a combination of ``defer.inlineCallbacks ``, and
182
- ``logcontext .PreserveLoggingContext ``. Suppose we want to implement ``sleep ``,
182
+ ``context .PreserveLoggingContext ``. Suppose we want to implement ``sleep ``,
183
183
which returns a deferred which will run its callbacks after a given number of
184
184
seconds. That might look like:
185
185
@@ -204,13 +204,13 @@ That doesn't follow the rules, but we can fix it by wrapping it with
204
204
This technique works equally for external functions which return deferreds,
205
205
or deferreds we have made ourselves.
206
206
207
- You can also use ``logcontext .make_deferred_yieldable ``, which just does the
207
+ You can also use ``context .make_deferred_yieldable ``, which just does the
208
208
boilerplate for you, so the above could be written:
209
209
210
210
.. code :: python
211
211
212
212
def sleep (seconds ):
213
- return logcontext .make_deferred_yieldable(get_sleep_deferred(seconds))
213
+ return context .make_deferred_yieldable(get_sleep_deferred(seconds))
214
214
215
215
216
216
Fire-and-forget
@@ -279,7 +279,7 @@ Obviously that option means that the operations done in
279
279
that might be fixed by setting a different logcontext via a ``with
280
280
LoggingContext(...) `` in ``background_operation ``).
281
281
282
- The second option is to use ``logcontext .run_in_background ``, which wraps a
282
+ The second option is to use ``context .run_in_background ``, which wraps a
283
283
function so that it doesn't reset the logcontext even when it returns an
284
284
incomplete deferred, and adds a callback to the returned deferred to reset the
285
285
logcontext. In other words, it turns a function that follows the Synapse rules
@@ -293,7 +293,7 @@ It can be used like this:
293
293
def do_request_handling ():
294
294
yield foreground_operation()
295
295
296
- logcontext .run_in_background(background_operation)
296
+ context .run_in_background(background_operation)
297
297
298
298
# this will now be logged against the request context
299
299
logger.debug(" Request handling complete" )
@@ -332,16 +332,16 @@ gathered:
332
332
result = yield defer.gatherResults([d1, d2])
333
333
334
334
In this case particularly, though, option two, of using
335
- ``logcontext .preserve_fn `` almost certainly makes more sense, so that
335
+ ``context .preserve_fn `` almost certainly makes more sense, so that
336
336
``operation1 `` and ``operation2 `` are both logged against the original
337
337
logcontext. This looks like:
338
338
339
339
.. code :: python
340
340
341
341
@defer.inlineCallbacks
342
342
def do_request_handling ():
343
- d1 = logcontext .preserve_fn(operation1)()
344
- d2 = logcontext .preserve_fn(operation2)()
343
+ d1 = context .preserve_fn(operation1)()
344
+ d2 = context .preserve_fn(operation2)()
345
345
346
346
with PreserveLoggingContext():
347
347
result = yield defer.gatherResults([d1, d2])
@@ -381,7 +381,7 @@ off the background process, and then leave the ``with`` block to wait for it:
381
381
.. code :: python
382
382
383
383
def handle_request (request_id ):
384
- with logcontext .LoggingContext() as request_context:
384
+ with context .LoggingContext() as request_context:
385
385
request_context.request = request_id
386
386
d = do_request_handling()
387
387
@@ -414,7 +414,7 @@ runs its callbacks in the original logcontext, all is happy.
414
414
415
415
The business of a Deferred which runs its callbacks in the original logcontext
416
416
isn't hard to achieve — we have it today, in the shape of
417
- ``logcontext ._PreservingContextDeferred ``:
417
+ ``context ._PreservingContextDeferred ``:
418
418
419
419
.. code :: python
420
420
0 commit comments