# Buggy code
def log_trace(errmsg=None):
    """
    Log a traceback to the log. This should be called from within an
    exception.

    Args:
        errmsg (str, optional): Adds an extra line with added info
            at the end of the traceback in the log.

    """
    tracestring = format_exc()
    try:
        if tracestring:
            for line in tracestring.splitlines():
                log.msg("[::] %s" % line)
        if errmsg:
            try:
                errmsg = str(errmsg)
            except Exception as e:
                errmsg = str(e)
            for line in errmsg.splitlines():
                log.msg("[EE] %s" % line)
    except Exception:
        log.msg("[EE] %s" % errmsg)


# Traceback (exceptions.KeyError)
2017-04-14T10:08:52-0400 [AMPProtocol,0,127.0.0.1] Unhandled Error
Traceback (most recent call last):
File "/home/tehom/arx/local/lib/python2.7/site-packages/twisted/python/log.py", line 101, in callWithLogger
return callWithContext({"system": lp}, func, *args, **kw)
File "/home/tehom/arx/local/lib/python2.7/site-packages/twisted/python/log.py", line 84, in callWithContext
return context.call({ILogContext: newCtx}, func, *args, **kw)
File "/home/tehom/arx/local/lib/python2.7/site-packages/twisted/python/context.py", line 118, in callWithContext
return self.currentContext().callWithContext(ctx, func, *args, **kw)
File "/home/tehom/arx/local/lib/python2.7/site-packages/twisted/python/context.py", line 81, in callWithContext
return func(*args,**kw)
--- <exception caught here> ---
File "/home/tehom/arx/local/lib/python2.7/site-packages/twisted/internet/posixbase.py", line 597, in _doReadOrWrite
why = selectable.doRead()
File "/home/tehom/arx/local/lib/python2.7/site-packages/twisted/internet/tcp.py", line 208, in doRead
return self._dataReceived(data)
File "/home/tehom/arx/local/lib/python2.7/site-packages/twisted/internet/tcp.py", line 214, in _dataReceived
rval = self.protocol.dataReceived(data)
File "/home/tehom/arx/local/lib/python2.7/site-packages/twisted/protocols/amp.py", line 2411, in dataReceived
return Int16StringReceiver.dataReceived(self, data)
File "/home/tehom/arx/local/lib/python2.7/site-packages/twisted/protocols/basic.py", line 765, in dataReceived
self.stringReceived(packet)
File "/home/tehom/arx/local/lib/python2.7/site-packages/twisted/protocols/basic.py", line 877, in stringReceived
self.state = statehandler(string)
File "/home/tehom/arx/local/lib/python2.7/site-packages/twisted/protocols/amp.py", line 2464, in proto_key
self.boxReceiver.ampBoxReceived(self._currentBox)
File "/home/tehom/arx/local/lib/python2.7/site-packages/twisted/protocols/amp.py", line 1063, in ampBoxReceived
self._answerReceived(box)
File "/home/tehom/arx/local/lib/python2.7/site-packages/twisted/protocols/amp.py", line 989, in _answerReceived
question = self._outstandingRequests.pop(box[ANSWER])
exceptions.KeyError: '3433'

2017-04-14T10:08:53-0400 [evennia.server.amp.AmpServerFactory] Unhandled Error
Traceback (most recent call last):
File "/home/tehom/arx/local/lib/python2.7/site-packages/twisted/python/log.py", line 84, in callWithContext
return context.call({ILogContext: newCtx}, func, *args, **kw)
File "/home/tehom/arx/local/lib/python2.7/site-packages/twisted/python/context.py", line 118, in callWithContext
return self.currentContext().callWithContext(ctx, func, *args, **kw)
File "/home/tehom/arx/local/lib/python2.7/site-packages/twisted/python/context.py", line 81, in callWithContext
return func(*args,**kw)
File "/home/tehom/arx/local/lib/python2.7/site-packages/twisted/internet/posixbase.py", line 597, in _doReadOrWrite
why = selectable.doRead()
--- <exception caught here> ---
File "/home/tehom/arx/local/lib/python2.7/site-packages/twisted/internet/tcp.py", line 1072, in doRead
transport = self.transport(skt, protocol, addr, self, s, self.reactor)
File "/home/tehom/arx/local/lib/python2.7/site-packages/twisted/internet/tcp.py", line 789, in __init__
self.startReading()
File "/home/tehom/arx/local/lib/python2.7/site-packages/twisted/internet/abstract.py", line 434, in startReading
self.reactor.addReader(self)
File "/home/tehom/arx/local/lib/python2.7/site-packages/twisted/internet/epollreactor.py", line 271, in addReader
EPOLLIN, EPOLLOUT)
File "/home/tehom/arx/local/lib/python2.7/site-packages/twisted/internet/epollreactor.py", line 255, in _add
self._poller.modify(fd, flags)
exceptions.IOError: [Errno 2] No such file or directory

2017-04-14T10:08:54-0400 [evennia.server.amp.AmpServerFactory] Unhandled Error
Traceback (most recent call last):
File "/home/tehom/arx/local/lib/python2.7/site-packages/twisted/python/log.py", line 84, in callWithContext
return context.call({ILogContext: newCtx}, func, *args, **kw)
File "/home/tehom/arx/local/lib/python2.7/site-packages/twisted/python/context.py", line 118, in callWithContext
return self.currentContext().callWithContext(ctx, func, *args, **kw)
File "/home/tehom/arx/local/lib/python2.7/site-packages/twisted/python/context.py", line 81, in callWithContext
return func(*args,**kw)
File "/home/tehom/arx/local/lib/python2.7/site-packages/twisted/internet/posixbase.py", line 597, in _doReadOrWrite
why = selectable.doRead()
--- <exception caught here> ---
File "/home/tehom/arx/local/lib/python2.7/site-packages/twisted/internet/tcp.py", line 1072, in doRead
transport = self.transport(skt, protocol, addr, self, s, self.reactor)
File "/home/tehom/arx/local/lib/python2.7/site-packages/twisted/internet/tcp.py", line 789, in __init__
self.startReading()
File "/home/tehom/arx/local/lib/python2.7/site-packages/twisted/internet/abstract.py", line 434, in startReading
self.reactor.addReader(self)
File "/home/tehom/arx/local/lib/python2.7/site-packages/twisted/internet/epollreactor.py", line 271, in addReader
EPOLLIN, EPOLLOUT)
File "/home/tehom/arx/local/lib/python2.7/site-packages/twisted/internet/epollreactor.py", line 255, in _add
self._poller.modify(fd, flags)
exceptions.IOError: [Errno 2] No such file or directory

# Fixed code
def log_trace(errmsg=None):
    """
    Log a traceback to the log. This should be called from within an
    exception.

    Args:
        errmsg (str, optional): Adds an extra line with added info
            at the end of the traceback in the log.

    """
    tracestring = format_exc()
    try:
        if tracestring:
            for line in tracestring.splitlines():
                log.msg("[::] %s" % line)
        if errmsg:
            try:
                errmsg = str(errmsg)
            except Exception as e:
                errmsg = str(e)
            for line in errmsg.splitlines():
                log_msg("[EE] %s" % line)
    except Exception:
        log_msg("[EE] %s" % errmsg)