如何用Python调试打印?

时间:2016-01-20 18:56:50

标签: python debugging python-3.x logging

我有这样的功能......

def validate_phone(raw_number, debug=False):

我希望调试标志控制它是否输出日志语句。例如:

if (debug):
  print('Before splitting numbers', file=sys.stderr)

split_version = raw_number.split('-')

if (debug):
  print('After splitting numbers', file=sys.stderr)

然而,该代码非常重复。什么是最干净(DRYest?)方式来处理这种if-flag-then-log逻辑?

4 个答案:

答案 0 :(得分:0)

您应该使用日志记录模块:

import logging
import sys
# Get the "root" level logger.
root = logging.getLogger()
# Set the log level to debug.
root.setLevel(logging.DEBUG)
# Add a handler to output log messages as a stream (to a file/handle)
# in this case, sys.stderr
ch = logging.StreamHandler(sys.stderr)
# This handler can log debug messages - multiple handlers could log
# different "levels" of log messages.
ch.setLevel(logging.DEBUG)
# Format the output to include the time, etc.
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
ch.setFormatter(formatter)
# Add the formatter to the root handler...
root.addHandler(ch)
# Get a logger object - this is what we use to omit log messages.
logger=logging.getLogger(__name__)

# Generate a bunch of log messages as a demonstration
for i in xrange(100):
   logger.debug('The value of i is: %s', i)

# Demonstrate a useful example of logging full exception tracebacks
# if the logger will output debug, but a warning in other modes.
try:
   a='a'+12
except Exception as e:
   # Only log exceptions if debug is enabled..
   if logger.isEnabledFor(logging.DEBUG):
       # Log the traceback w/ the exception message.
       logger.exception('Something went wrong: %s', e)
   else:
       logger.warning('Something went wrong: %s', e)

在此处详细了解:https://docs.python.org/2/library/logging.html

要禁用日志记录,只需将级别(logging.DEBUG)设置为其他内容(例如logging.INFO)。请注意,将消息重定向到其他地方(如文件)或在某些地方发送一些消息(调试)以及向其他人发送其他消息(警告)也很容易。

答案 1 :(得分:0)

import logging
logging.basicConfig(filename='example.log',level=logging.DEBUG)
logging.debug('This message should go to the log file')

您可以将日志记录级别更改为INFO/DEBUG/WARNING/ERROR/CRITICAL,并且logging模块也可以为您记录时间戳,也可以配置。

检查链接python3 logging HowTo

答案 2 :(得分:0)

我会使用日志记录模块。它是为它而制造的。

> cat a.py       
import logging

log = logging.getLogger(__name__)


def main():
    log.debug('This is debug')
    log.info('This is info')
    log.warn('This is warn')
    log.fatal('This is fatal')
    try:
        raise Exception("this is exception")
    except Exception:
        log.warn('Failed with exception', exc_info=True)
        raise

if __name__ == '__main__':
    import argparse
    parser = argparse.ArgumentParser(description='something')
    parser.add_argument(
        '-v', '--verbose', action='count', default=0, dest='verbosity')
    args = parser.parse_args()

    logging.basicConfig()
    logging.getLogger().setLevel(logging.WARN - 10 * args.verbosity)

    main()
> python a.py 
WARNING:__main__:This is warn
CRITICAL:__main__:This is fatal
WARNING:__main__:Failed with exception
Traceback (most recent call last):
  File "a.py", line 12, in main
    raise Exception("this is exception")
Exception: this is exception
Traceback (most recent call last):
  File "a.py", line 27, in <module>
    main()
  File "a.py", line 12, in main
    raise Exception("this is exception")
Exception: this is exception
> python a.py -v
INFO:__main__:This is info
WARNING:__main__:This is warn
CRITICAL:__main__:This is fatal
WARNING:__main__:Failed with exception
Traceback (most recent call last):
  File "a.py", line 12, in main
    raise Exception("this is exception")
Exception: this is exception
Traceback (most recent call last):
  File "a.py", line 27, in <module>
    main()
  File "a.py", line 12, in main
    raise Exception("this is exception")
Exception: this is exception
> python a.py -vv
DEBUG:__main__:This is debug
INFO:__main__:This is info
WARNING:__main__:This is warn
CRITICAL:__main__:This is fatal
WARNING:__main__:Failed with exception
Traceback (most recent call last):
  File "a.py", line 12, in main
    raise Exception("this is exception")
Exception: this is exception
Traceback (most recent call last):
  File "a.py", line 27, in <module>
    main()
  File "a.py", line 12, in main
    raise Exception("this is exception")
Exception: this is exception

答案 3 :(得分:0)

我同意使用日志记录是在运行python脚本时打印调试信息的最佳解决方案。我编写了一个DebugPrint模块,有助于更轻松地使用记录器:

#DebugPrint.py
import logging
import os
import time

DEBUGMODE=True

logging.basicConfig(level=logging.DEBUG)
log=logging.getLogger('=>')    


#DebugPrint.py
#DbgPrint=logging.debug 
def DbgPrint(*args, **kwargs):
    if DEBUGMODE:
        #get module, class, function, linenumber information
        import inspect
        className = None
        try:
            className = inspect.stack()[2][0].f_locals['self'].__class__.__name__
        except:
            pass
        modName=None
        try:
            modName = os.path.basename(inspect.stack()[2][1])
        except:
            pass
        lineNo=inspect.stack()[2][2]
        fnName=None
        try:
            fnName = inspect.stack()[2][3]
        except:
            pass
        DbgText="line#{}:{}->{}->{}()".format(lineNo, modName,className, fnName)
        argCnt=len(args)
        kwargCnt=len(kwargs)
        #print("argCnt:{} kwargCnt:{}".format(argCnt,kwargCnt))
        fmt=""
        fmt1=DbgText+":"+time.strftime("%H:%M:%S")+"->"
        if argCnt > 0:
            fmt1+=(argCnt-1)*"%s,"
            fmt1+="%s"
            fmt+=fmt1

        if kwargCnt>0:
            fmt2="%s"
            args+=("{}".format(kwargs),)
            if len(fmt)>0:
                fmt+=","+fmt2
            else:
                fmt+=fmt2


        #print("fmt:{}".format(fmt))
        log.debug(fmt,*args)


if __name__=="__main__":
    def myTest():
        print("Running myTest()")
        DbgPrint("Hello","World")

myTest()

如果DEBUGMODE变量为false,则不会打印任何内容 如果是,则上面的示例代码打印出来:

DEBUG:=>:16:24:14:line#78:DebugPrint.py->None->myTest():->Hello,World

现在,我将使用定义类的模块测试DebugPrint。

#testDebugPrint.py
from DebugPrint import DbgPrint


class myTestClass(object):
    def __init__(self):
        DbgPrint("Initializing the class")

    def doSomething(self, arg):
        DbgPrint("I'm doing something with {}".format(arg))

if __name__=='__main__':
    test=myTestClass()
    test.doSomething("a friend!")

运行此脚本时,输出如下:

DEBUG:=>:16:25:02:line#7:testDebugPrint.py->myTestClass->__init__():->Initializing the class
DEBUG:=>:16:25:02:line#10:testDebugPrint.py->myTestClass->doSomething():->I'm doing something with a friend!

请注意,控制台上打印的模块名称,类名称,功能名称和行​​号以及语句的打印时间是正确的。

我希望您会发现此实用程序很有用。