记录显示错误的行号

时间:2014-11-22 23:56:48

标签: python-2.7

我正在尝试学习python 2.7s日志记录模块,并已复制此剪辑:

def _logging():     #设置程序日志记录     全球记录器

# valid logging levels:
# =====================
# logger.debug('debug message')
# logger.info('info message')
# logger.warn('warn message')
# logger.error('error message')
# logger.critical('critical message')
#
# to show traceback:
# logger.critical(str(e), exc_info = True)   # if 'error' & 'True' - halts script 

logger = logging.getLogger()
logger.setLevel(logging.DEBUG)

# create file handler which logs INFO & above messages
fh = logging.FileHandler('c:/temp/pgmLog.log','w')
fh.setLevel(logging.INFO)

# create console handler that logs all messages
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)

# create formatter and add it to the handlers
formatter = logging.Formatter('%(asctime)s   %(lineno)-5d %(levelname)-12s %(message)s', datefmt='%m-%d-%Y %H:%M:%S')
ch.setFormatter(formatter)
fh.setFormatter(formatter)

# add the handlers to logger
logger.addHandler(ch)       # console logger
logger.addHandler(fh)       # file logger

当我运行以下作品时:



logger.info('Program start')
try:
    print 3/0
except Exception, err:
    logger.error(err)




记录器正在向我显示:



11-22-2014 15:48:32   46    INFO         Program start
11-22-2014 15:48:32   1415  ERROR        integer division or modulo by zero




有人可以告诉我为什么错误显示为第1415行,而不是错误的真实位置?

非常感谢

0 个答案:

没有答案