Best way to log a Python exception
Question: I'm printing my exceptions to a log file currently with:

    
    
    try:
        # coode in here
    except Exception, e:
        logging.error(e)
    

Could I be printing more information about the exception and the code that
generated it than just the exception string? Things like line numbers or stack
traces would be great.


Answer: [`logger.exception`](http://docs.python.org/library/logging.html#logging.Logger.exception)
does that.

For example:

    
    
    import logging
    try:
        1/0
    except Exception as e:
        logging.exception("message")
    

Output:

    
    
    ERROR:root:message
    Traceback (most recent call last):
      File "<stdin>", line 2, in <module>
    ZeroDivisionError: integer division or modulo by zero
    

[@Paulo Cheque](http://stackoverflow.com/users/1163081/paulo-cheque) notes,
"be aware that in Python 3 you must call the `logging.exception` method just
inside the `except` part. If you call this method in an arbitrary place you
may get a bizarre exception. The docs alert about that."

