Post stack traces as pre-formatted text, not images, see Wiki - how to post code on this forum. Just copy the stack trace as a whole and put it in the pre-formatted text block.
Neither of the scripts you gave us have 265 lines present, so its likely the source is elsewhere (maybe your message handler?). Looking at the error, it appears you forgot to close a pair of parentheses somewhere.
Additionally, you should NOT be importing anything within a function definition. You should never have to import system
, its always available in all scripting contexts.
Also, the logger methods also accept a trace object as an additional argument. Don't stringify the traceback you get from your exception clauses. Example:
except Exception as dt_err:
logger.error("Date/time parse failed: {} - datetime_str = '{}'".format(str(dt_err), datetime_str))
Should be
except Exception as dt_err:
logger.errorf("Date/time parse failed for datetime_str '%s'", datetime_str, dt_err)