python exception message capturing

前端 未结 11 1690
醉梦人生
醉梦人生 2020-12-12 09:06
import ftplib
import urllib2
import os
import logging
logger = logging.getLogger(\'ftpuploader\')
hdlr = logging.FileHandler(\'ftplog.log\')
formatter = logging.Form         


        
11条回答
  •  一向
    一向 (楼主)
    2020-12-12 09:42

    Using str(e) or repr(e) to represent the exception, you won't get the actual stack trace, so it is not helpful to find where the exception is.

    After reading other answers and the logging package doc, the following two ways works great to print the actual stack trace for easier debugging:

    use logger.debug() with parameter exc_info

    try:
        # my code
    except SomeError as e:
        logger.debug(e, exc_info=True)
    

    use logger.exception()

    or we can directly use logger.exception() to print the exception.

    try:
        # my code
    except SomeError as e:
        logger.exception(e)
    

提交回复
热议问题