summary refs log tree commit diff
path: root/synapse/util
diff options
context:
space:
mode:
authorMark Haines <mark.haines@matrix.org>2014-10-30 01:21:33 +0000
committerMark Haines <mark.haines@matrix.org>2014-10-30 01:21:33 +0000
commitb29517bd013b82302b1a73072da8bfc39564dc1a (patch)
tree363ac928a18f2432b6d53c27dde2f2efb34b16a8 /synapse/util
parentfix mobile CSS layout (diff)
downloadsynapse-b29517bd013b82302b1a73072da8bfc39564dc1a.tar.xz
Add a request-id to each log line
Diffstat (limited to 'synapse/util')
-rw-r--r--synapse/util/async.py5
-rw-r--r--synapse/util/logcontext.py85
-rw-r--r--synapse/util/logutils.py1
3 files changed, 90 insertions, 1 deletions
diff --git a/synapse/util/async.py b/synapse/util/async.py
index 647ea6142c..3d3fbe182c 100644
--- a/synapse/util/async.py
+++ b/synapse/util/async.py
@@ -16,8 +16,11 @@
 
 from twisted.internet import defer, reactor
 
+from .logcontext import PreserveLoggingContext
 
+@defer.inlineCallbacks
 def sleep(seconds):
     d = defer.Deferred()
     reactor.callLater(seconds, d.callback, seconds)
-    return d
+    with PreserveLoggingContext():
+        yield d
diff --git a/synapse/util/logcontext.py b/synapse/util/logcontext.py
new file mode 100644
index 0000000000..46a2855a15
--- /dev/null
+++ b/synapse/util/logcontext.py
@@ -0,0 +1,85 @@
+from functools import wraps
+
+import threading
+import logging
+
+class LoggingContext(object):
+    __slots__ = ["parent_context", "name", "__dict__"]
+
+    thread_local = threading.local()
+
+    class Sentinel(object):
+        __slots__ = []
+        def copy_to(self, record):
+            pass
+
+    sentinel = Sentinel()
+
+    def __init__(self, name=None):
+        self.parent_context = None
+        self.name = name
+
+    def __str__(self):
+        return "%s@%x" % (self.name, id(self)) 
+
+    @classmethod
+    def current_context(cls):
+        return getattr(cls.thread_local, "current_context", cls.sentinel)
+
+    def __enter__(self):
+        if self.parent_context is not None:
+            raise Exception("Attempt to enter logging context multiple times")
+        self.parent_context = self.current_context()
+        self.thread_local.current_context = self
+        return self
+
+    def __exit__(self, type, value, traceback):
+        if self.thread_local.current_context is not self:
+            logging.error(
+                "Current logging context %s is not the expected context %s",
+                self.thread_local.current_context,
+                self
+            )
+        self.thread_local.current_context = self.parent_context
+        self.parent_context = None
+
+    def __getattr__(self, name):
+        return getattr(self.parent_context, name)
+
+    def copy_to(self, record):
+        if self.parent_context is not None:
+            self.parent_context.copy_to(record)
+        for key, value in self.__dict__.items():
+            setattr(record, key, value)
+
+    @classmethod
+    def wrap_callback(cls, callback):
+        context = cls.current_context()
+        @wraps(callback)
+        def wrapped(*args, **kargs):
+            cls.thread_local.current_context = context
+            return callback(*args, **kargs)
+        return wrapped
+
+
+class LoggingContextFilter(logging.Filter):
+    def __init__(self, **defaults):
+        self.defaults = defaults
+
+    def filter(self, record):
+        context = LoggingContext.current_context()
+        for key, value in self.defaults.items():
+            setattr(record, key, value)
+        context.copy_to(record)
+        return True
+
+
+class PreserveLoggingContext(object):
+    __slots__ = ["current_context"]
+    def __enter__(self):
+        self.current_context = LoggingContext.current_context()
+
+    def __exit__(self, type, value, traceback):
+        LoggingContext.thread_local.current_context = self.current_context
+
+
diff --git a/synapse/util/logutils.py b/synapse/util/logutils.py
index fadf0bd510..903a6cf1b3 100644
--- a/synapse/util/logutils.py
+++ b/synapse/util/logutils.py
@@ -75,6 +75,7 @@ def trace_function(f):
     linenum = f.func_code.co_firstlineno
     pathname = f.func_code.co_filename
 
+    @wraps(f)
     def wrapped(*args, **kwargs):
         name = f.__module__
         logger = logging.getLogger(name)