Skip to content

Commit

Permalink
allow thread specific log storage to be reentrant
Browse files Browse the repository at this point in the history
  • Loading branch information
buzztroll committed Jun 29, 2012
1 parent 1379b83 commit d26b0e8
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions epu/domain_log.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,28 +29,34 @@ class DomainLogFilter(logging.Filter):
it simply changes the contents of the log record based on thread specific data.
"""
def filter(self, record):
record.domain_info = ""
if not hasattr(g_threadlocal, 'epu_info'):
return True
if not g_threadlocal.epu_info:
return True

# set any value needed
domain_info = ""
for f in g_context_log_fields:
if hasattr(g_threadlocal, f):
v = getattr(g_threadlocal, f)
domain_info = "%s %s=%s" % (domain_info, f, v)
for f in g_context_log_fields:
kw = g_threadlocal.epu_info[-1]
if f in kw:
domain_info = "%s %s=%s" % (domain_info, f, kw[f])
record.domain_info = domain_info.strip()
return True


class EpuLoggerThreadSpecific():
def __init__(self, **kw):
self.kw = kw.copy()
if not hasattr(g_threadlocal, 'epu_info'):
g_threadlocal.epu_info = []

def __enter__(self):
for key in self.kw:
setattr(g_threadlocal, key, self.kw[key])
g_threadlocal.epu_info.append(self.kw)
return None

def __exit__(self, type, value, traceback):
for key in self.kw:
delattr(g_threadlocal, key)
g_threadlocal.epu_info.pop()
return None


Expand Down

0 comments on commit d26b0e8

Please sign in to comment.