Skip to content

Commit

Permalink
Fixed task logging with greenlets
Browse files Browse the repository at this point in the history
  • Loading branch information
Joel Collins committed Apr 3, 2020
1 parent c981306 commit 640e446
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions labthings/core/tasks/thread.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from gevent import Greenlet, GreenletExit
from gevent.thread import get_ident
import datetime
import logging
import traceback
Expand Down Expand Up @@ -54,7 +55,7 @@ def id(self):
@property
def ident(self):
"""Compatibility with threading interface. A small, unique non-negative integer that identifies this object."""
return self.minimal_ident
return get_ident(self)

@property
def state(self):
Expand Down Expand Up @@ -124,7 +125,7 @@ def terminate(self):


class ThreadLogHandler(logging.Handler):
def __init__(self, thread=None, dest=None):
def __init__(self, thread=None, dest=None, level=logging.WARNING):
"""Set up a log handler that appends messages to a list.
This log handler will first filter by ``thread``, if one is
Expand All @@ -141,17 +142,17 @@ def __init__(self, thread=None, dest=None):
lot of log messages, you may run into memory problems.
"""
logging.Handler.__init__(self)
self.setLevel(level)
self.thread = thread
self.dest = dest if dest else []
self.dest = dest if dest is not None else []
self.addFilter(self.check_thread)

def check_thread(self, record):
"""Determine if a thread matches the desired record"""
if self.thread is None:
return 1

thread_ident = getattr(record.thread, "ident", None)
if record.thread == self.thread.ident:
if get_ident() == get_ident(self.thread):
return 1
return 0

Expand All @@ -162,6 +163,7 @@ def emit(self, record):
for k in ["created", "levelname", "levelno", "lineno", "filename"]:
record_dict[k] = getattr(record, k)
self.dest.append(record_dict)
print(self.thread.log)
# FIXME: make sure this doesn't become a memory disaster!
# We probably need to check the size of the list...
# TODO: think about whether any of the keys are security flaws
Expand Down

0 comments on commit 640e446

Please sign in to comment.