[Mpuls-commits] r5183 - in base/trunk: . mpulsweb/lib

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Wed Jul 20 16:18:15 CEST 2011


Author: bh
Date: 2011-07-20 16:18:14 +0200 (Wed, 20 Jul 2011)
New Revision: 5183

Modified:
   base/trunk/ChangeLog
   base/trunk/mpulsweb/lib/logfilter.py
Log:
* mpulsweb/lib/logfilter.py (DBNameContextFilter): Rework how the
database name is determined. Using getDbName is problematic, since
it may use the logging framework itself. Now, the
DBNameContextFilter class provides a way to set the database name
on a per thread basis. The database name can be set by the base
controller at the beginning of a request and reset after a request
has finished. The DBNameContextFilter can retrieve the name
without causing recursive calls to the logging system. See the
doc-string for details.
Also, the database name is now stored in the record in the
attribute mpuls_dbname to make potential name conflicts less
likely.


Modified: base/trunk/ChangeLog
===================================================================
--- base/trunk/ChangeLog	2011-07-20 08:15:48 UTC (rev 5182)
+++ base/trunk/ChangeLog	2011-07-20 14:18:14 UTC (rev 5183)
@@ -1,3 +1,18 @@
+2011-07-20  Bernhard Herzog  <bh at intevation.de>
+
+	* mpulsweb/lib/logfilter.py (DBNameContextFilter): Rework how the
+	database name is determined. Using getDbName is problematic, since
+	it may use the logging framework itself. Now, the
+	DBNameContextFilter class provides a way to set the database name
+	on a per thread basis. The database name can be set by the base
+	controller at the beginning of a request and reset after a request
+	has finished. The DBNameContextFilter can retrieve the name
+	without causing recursive calls to the logging system. See the
+	doc-string for details.
+	Also, the database name is now stored in the record in the
+	attribute mpuls_dbname to make potential name conflicts less
+	likely.
+
 2011-07-20  Ludwig Reiter <ludwig.reiter at intevation.de>
 
 	* mpulsweb/lib/logfilter.py:

Modified: base/trunk/mpulsweb/lib/logfilter.py
===================================================================
--- base/trunk/mpulsweb/lib/logfilter.py	2011-07-20 08:15:48 UTC (rev 5182)
+++ base/trunk/mpulsweb/lib/logfilter.py	2011-07-20 14:18:14 UTC (rev 5183)
@@ -2,33 +2,53 @@
 #
 # Authors:
 # Ludwig Reiter <ludwig.reiter at intevation.de>
+# Bernhard Herzog <bernhard.herzog at intevation.de>
 
+import thread
 import logging
-import mpulsweb.lib.security as security
 
-log = logging.getLogger(__name__)
 
 class DBNameContextFilter(logging.Filter):
+
+    """Logging filter that adds the databasename to log records.
+
+    The database name is different for each request and multiple
+    requests may be handled simultaneously in multiple threads, so
+    determining the database name needs to be done carefully. Simply
+    using the getDbName function from the security module doesn't work
+    properly, because, even though it would return the correct result,
+    it logs messages using the logging system, so the filter could be
+    called recursively.
+
+    To avoid such problems, the class provides a way to store and
+    retrieve thread-specific database names with the class methods
+    set_thread_dbname and get_thread_dbname. Some other code, e.g. the
+    base controller, calls set_thread_dbname with the appropriate
+    database name at the start of a request and sets it to None after
+    the request has been finished.
+
+    The filter always succeeds, i.e. it never suppresses any record, but
+    it modifies the records it filters. It adds the database name to the
+    record object as the attribute mpuls_dbname. With that name it can
+    be referenced in format string of a formatter. If no database name
+    has been set, the value of mpuls_dbname is None.
     """
-    This filter injects the dbname into a log message.
-    
-    It tries to get the dbname from the security module and
-    catches all exceptions from there.
-    """
 
-    def get_dbname(self):
-        dbname = None
-        try:
-            dbname = security.getDbName()
-        except:
-            log.debug("Cannot find db name")
-        return dbname 
+    dbnames = {}
 
+    @classmethod
+    def set_thread_dbname(cls, dbname):
+        cls.dbnames[thread.get_ident()] = dbname
+
+    @classmethod
+    def get_thread_dbname(cls):
+        return cls.dbnames.get(thread.get_ident())
+
     def filter(self, record):
-        record.dbname = self.get_dbname()
-
+        record.mpuls_dbname = self.get_thread_dbname()
         return True
 
+
 def filtered_handler(base_handler, *args):
     handler = base_handler(*args)
     handler.addFilter(DBNameContextFilter())



More information about the Mpuls-commits mailing list