Python argparse文件错误
我正在尝试制作一个脚本,它将所有错误记录到日志文件中。我想在此文件中包含任何Python argparse文件错误,python,logging,argparse,Python,Logging,Argparse,我正在尝试制作一个脚本,它将所有错误记录到日志文件中。我想在此文件中包含任何argparse错误。我已经使用logging包和sys.excepthook驱动日志文件的意外异常 下面是一个示例代码: import argparse import logging import sys import traceback def log_uncaught_exceptions(ex_cls, ex, tb): logging.critical(''.join(traceback.format
argparse
错误。我已经使用logging
包和sys.excepthook
驱动日志文件的意外异常
下面是一个示例代码:
import argparse
import logging
import sys
import traceback
def log_uncaught_exceptions(ex_cls, ex, tb):
logging.critical(''.join(traceback.format_tb(tb)))
logging.critical('{0}: {1}'.format(ex_cls, ex))
logging.basicConfig(
level=logging.DEBUG,
filename='foo.log',
filemode='w',
format='%(asctime)s - %(levelname)s - %(message)s')
sys.excepthook = log_uncaught_exceptions
logging.debug('This is a typical debug line')
parser = argparse.ArgumentParser(description='Foo String')
parser.add_argument('foo',type=int)
args = parser.parse_args()
logging.debug('Input was %i'%(args.foo))
当我用python logger_argparse.py 1运行它时,一切都很好。如果我运行python logger_argparse.py a
我将在控制台中获得输出,而不是日志文件:
usage: logger_argparse.py [-h] foo
logger_argparse.py: error: argument foo: invalid int value: 'a'
如何将这些信息转到日志文件?我发现了一个我认为不是很好的解决方法。但它似乎在起作用:
import argparse
import logging
import sys
import traceback
class argparse_logger(argparse.ArgumentParser):
def _print_message(self, message, file=None):
if file is sys.stderr:
logger.error('Arg Parse did something bad...see below:')
logger.error(message)
else:
super()._print_message(message,file=file)
def log_uncaught_exceptions(ex_cls, ex, tb):
logging.critical(''.join(traceback.format_tb(tb)))
logging.critical('{0}: {1}'.format(ex_cls, ex))
logging.basicConfig(
level=logging.DEBUG,
filename='foo.log',
filemode='w',
format='%(asctime)s - %(levelname)s - %(message)s')
# logging.getLogger('root')
logger = logging.getLogger('root')
sys.excepthook = log_uncaught_exceptions
logger.debug('This is a typical debug line')
parser = argparse_logger(description='Foo String')
parser.add_argument('foo',type=int)
args = parser.parse_args()
logger.debug('Input was %i'%(args.foo))
我发现了一个我认为不是很好的解决办法。但它似乎在起作用:
import argparse
import logging
import sys
import traceback
class argparse_logger(argparse.ArgumentParser):
def _print_message(self, message, file=None):
if file is sys.stderr:
logger.error('Arg Parse did something bad...see below:')
logger.error(message)
else:
super()._print_message(message,file=file)
def log_uncaught_exceptions(ex_cls, ex, tb):
logging.critical(''.join(traceback.format_tb(tb)))
logging.critical('{0}: {1}'.format(ex_cls, ex))
logging.basicConfig(
level=logging.DEBUG,
filename='foo.log',
filemode='w',
format='%(asctime)s - %(levelname)s - %(message)s')
# logging.getLogger('root')
logger = logging.getLogger('root')
sys.excepthook = log_uncaught_exceptions
logger.debug('This is a typical debug line')
parser = argparse_logger(description='Foo String')
parser.add_argument('foo',type=int)
args = parser.parse_args()
logger.debug('Input was %i'%(args.foo))
您可以在异常处理程序中使用吗?
编辑:那么从ArgumentParser.error()登录怎么样
这将显示以下内容:
J:\>python arg_parse_log.py 5
Exiting! - 5
J:\>more foo.log
2018-02-07 11:34:45,647 - DEBUG - Start
2018-02-07 11:34:45,648 - DEBUG - Finish - 5
J:\>python arg_parse_log.py
usage: arg_parse_log.py [-h] foo
arg_parse_log.py: error: the following arguments are required: foo
Error handling arguments
J:\>more foo.log
2018-02-07 11:34:54,577 - DEBUG - Start
2018-02-07 11:34:54,578 - ERROR - the following arguments are required: foo
您可以在异常处理程序中使用吗?
编辑:那么从ArgumentParser.error()登录怎么样
这将显示以下内容:
J:\>python arg_parse_log.py 5
Exiting! - 5
J:\>more foo.log
2018-02-07 11:34:45,647 - DEBUG - Start
2018-02-07 11:34:45,648 - DEBUG - Finish - 5
J:\>python arg_parse_log.py
usage: arg_parse_log.py [-h] foo
arg_parse_log.py: error: the following arguments are required: foo
Error handling arguments
J:\>more foo.log
2018-02-07 11:34:54,577 - DEBUG - Start
2018-02-07 11:34:54,578 - ERROR - the following arguments are required: foo
它不起作用,因为logger\u argparse.py:error:argument foo:invalid int value:'a'
不是引发的异常,它只是argparse
的一个打印输出。有没有好办法中断该打印以使其进入文件?在argparse.py
文件中,查看error
和exit
(文档中也提到了它们)。大多数解析错误都会引发ArgumentError
,然后被这些错误捕获,一条消息被打印到stderr
,然后调用sys.exit
。在[argparse][logging]上搜索出现以下情况:它不起作用,因为logger\u argparse.py:error:argument foo:invalid int value:'a'
不是引发的异常,它只是argparse
的打印输出。是否有好的方法中断该打印以使其进入文件?在argparse.py
文件中,查看error
和等方法ode>exit
(文档中也提到了它们)。大多数解析错误都会引发ArgumentError
,然后被这些错误捕获,一条消息被打印到stderr
,然后调用sys.exit
。在[argparse][logging]上搜索结果是:我会修改解析器.error
。但由于它编写的所有内容都经过\u print\u message
处理,所以您的方法同样好。我会修改解析器.error
。但是,由于它编写的所有内容都经过\u print\u message
处理,所以您的方法同样好。是的……但我想要捕获argparse打印错误以记录输入或丢失的内容。错误只是说明出现了问题…控制台输出告诉我出了什么问题。感谢澄清,我已更新了代码。是的…但我想捕获argparse打印错误以记录输入或丢失的内容。错误或者只是声明出了问题…控制台输出告诉我出了什么问题。感谢澄清,我已经更新了代码。