mirror of
https://git.anonymousland.org/anonymousland/synapse.git
synced 2025-05-04 06:34:56 -04:00
Change the way we do logging contexts so that they survive divergences
This commit is contained in:
parent
db0dca2f6f
commit
476899295f
13 changed files with 128 additions and 97 deletions
|
@ -12,6 +12,8 @@
|
|||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
from twisted.internet import defer
|
||||
|
||||
import threading
|
||||
import logging
|
||||
|
||||
|
@ -129,3 +131,32 @@ class PreserveLoggingContext(object):
|
|||
def __exit__(self, type, value, traceback):
|
||||
"""Restores the current logging context"""
|
||||
LoggingContext.thread_local.current_context = self.current_context
|
||||
|
||||
|
||||
def preserve_context_over_fn(fn, *args, **kwargs):
|
||||
with PreserveLoggingContext():
|
||||
deferred = fn(*args, **kwargs)
|
||||
|
||||
return preserve_context_over_deferred(deferred)
|
||||
|
||||
|
||||
def preserve_context_over_deferred(deferred):
|
||||
d = defer.Deferred()
|
||||
|
||||
current_context = LoggingContext.current_context()
|
||||
|
||||
def cb(res):
|
||||
with PreserveLoggingContext():
|
||||
LoggingContext.thread_local.current_context = current_context
|
||||
res = d.callback(res)
|
||||
return res
|
||||
|
||||
def eb(failure):
|
||||
with PreserveLoggingContext():
|
||||
LoggingContext.thread_local.current_context = current_context
|
||||
res = d.errback(failure)
|
||||
return res
|
||||
|
||||
deferred.addCallbacks(cb, eb)
|
||||
|
||||
return d
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue