问题
I want to extract error detail from traceback, those tracebacks are extract from log file by using this method, and there are many different kinds of tracebacks, like below:
Traceback (most recent call last):
File "/usr/lib/python2.7/runpy.py", line 174, in _run_module_as_main
"__main__", fname, loader, pkg_name)
File "/usr/lib/python2.7/runpy.py", line 72, in _run_code
exec code in run_globals
File "/root/env/common/test/test/__main__.py", line 5, in <module>
main()
File "/root/env/common/test/test/cli/parser.py", line 55, in main
run_script(args)
File "/root/env/common/test/test/cli/runner.py", line 124, in run_script
exec_script(args.script, scope=globals(), root=True)
File "/root/workspace/group/test_regression_utils.py", line 123, in exec_script
cli_exec_script(*args,**kwargs)
File "/root/env/common/test/test/cli/runner.py", line 186, in exec_script
exec(compile(code, scriptpath, 'exec')) in scope
File "shiju_12.py", line 30, in <module>
File "/root/moaworkspace/group/testscript/utils/shiju_public.py", line 37, in shiju_move
Exception: close failed!
EndOfStream
EndOfStream
Traceback (most recent call last):
File "/usr/lib/python2.7/runpy.py", line 174, in _run_module_as_main
"__main__", fname, loader, pkg_name)
File "/usr/lib/python2.7/runpy.py", line 72, in _run_code
exec code in run_globals
File "/root/env/common/test/test/__main__.py", line 5, in <module>
main()
File "/root/env/common/test/test/cli/parser.py", line 55, in main
run_script(args)
File "/root/env/common/test/test/cli/runner.py", line 124, in run_script
exec_script(args.script, scope=globals(), root=True)
File "/root/env/common/test/test/cli/runner.py", line 186, in exec_script
exec(compile(code, scriptpath, 'exec')) in scope
File "/root/env/common/mator/mator/mator.py", line 520, in start
raise IOError("RPC server not started!")
IOError: RPC server not started
the expect result is:
("XXXX", "Exception: close failed!")
("XXXX","IOError: RPC server not started")
I have tried detail_regex = r'Traceback.*\n(\s+.*\n)*(.*)\n*'
the second traceback is right, but the first traceback result is ("Exception: close failed!", "EndOfStream")
any ideas?
回答1:
If I change your pattern to Traceback.*\n(\s+.*\n)*(.*?)\n*
it works for the given example. I am not sure though, that it solves your problem completely.
来源:https://stackoverflow.com/questions/45874348/how-to-extract-error-detail-from-traceback-by-using-python-regex