-
-
Save migurski/e73faf00d7e1c5e44bd6 to your computer and use it in GitHub Desktop.
Alternative logging arrangement for OpenAddresses machine
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def _run_process_one(lock, source_queue, destination, source_extras, results): | |
''' Single queue worker for source files to conform(). | |
Keep going until source_queue is empty. | |
''' | |
while True: | |
with lock: | |
if not source_queue: | |
return | |
path = source_queue.pop(0) | |
extras = source_extras.get(path, dict()) | |
try: | |
if not isdir(destination): | |
try: | |
mkdir(destination) | |
except OSError: | |
pass | |
_L.info(path) | |
# | |
# Filter class to match only records in the current thread. | |
# | |
class ThreadFilter: | |
def __init__(self, id): | |
self.id = id | |
def filter(self, record): | |
return record.thread == self.id | |
# | |
# LoggingHandler that writes to a stream. | |
# | |
from io import BytesIO | |
stream = BytesIO() | |
new_handler = logging.StreamHandler(stream) | |
new_handler.setFormatter(logging.Formatter('%(asctime)s %(levelname)06s: %(message)s')) | |
new_handler.setLevel(logging.DEBUG) | |
new_handler.addFilter(ThreadFilter(thread.get_ident())) | |
logging.getLogger('openaddr').addHandler(new_handler) | |
# | |
# Do the work | |
# | |
result = process_one.process(path, destination, extras) | |
# | |
# Remove the handler and peek into the log contents. | |
# | |
logging.getLogger('openaddr').removeHandler(new_handler) | |
_L.info(repr(stream.getvalue())) | |
except: | |
_L.error('Error while running process_one.process', exc_info=True) | |
result = None | |
with lock: | |
results[path] = result |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment