我使用下面的代码来打印脚本中运行的python模块的异常:
except Exception as e:
logging.info('yt-dlp initated a break', exc_info=True)
目前它输出的信息比我需要的多得多。
大致是这样的:
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/app/web_ripper.py", line 58, in subscriptions_loop
ydl.download([url])
File "/app/venv/lib/python3.9/site-packages/yt_dlp/YoutubeDL.py", line 3113, in download
self.__download_wrapper(self.extract_info)(
File "/app/venv/lib/python3.9/site-packages/yt_dlp/YoutubeDL.py", line 3086, in wrapper
res = func(*args, **kwargs)
File "/app/venv/lib/python3.9/site-packages/yt_dlp/YoutubeDL.py", line 1344, in extract_info
return self.__extract_info(url, self.get_info_extractor(ie_key), download, extra_info, process)
File "/app/venv/lib/python3.9/site-packages/yt_dlp/YoutubeDL.py", line 1371, in wrapper
self.report_error(str(e), e.format_traceback())
File "/app/venv/lib/python3.9/site-packages/yt_dlp/YoutubeDL.py", line 912, in report_error
self.trouble(f'{self._format_err("ERROR:", self.Styles.ERROR)} {message}', *args, **kwargs)
File "/app/venv/lib/python3.9/site-packages/yt_dlp/YoutubeDL.py", line 853, in trouble
raise DownloadError(message, exc_info)
yt_dlp.utils.DownloadError: ERROR: [youtube:tab] Videogamedunkey: Unable to download API page: HTTP Error 404: Not Found
打印异常的最后一行的最有效方法是什么?
1条答案
按热度按时间lx0bsm1f1#
您可以使用自定义
Formatter
,它将处理您尝试记录的record
的exc_text
。下面的
MyFormatter
将检查记录是否有异常文本。如果没有,它将使用self.formatException
创建它。然后,由于record.exc_text
是一个字符串,将跟踪存储为文本,并使用\n
作为行与行之间的分隔符,因此可以使用.split("\n")[-1]
保留最后一行。然后,添加以下代码以使用新的
MyFormatter
:你会得到:
而不是以前的: