gpt4 book ai didi

python - 我为在日志记录模块 python 中获取 ValueError 所做的工作

转载 作者:太空宇宙 更新时间:2023-11-04 02:45:15 25 4
gpt4 key购买 nike

我想将日志写入文件和标准输出,因此我使用这段代码,但我总是在标准输出中得到这个 ValueError 而不是在文件中。

我想从 INFO 级别及更高级别进行日志记录,以获取标准输出和文件。并将 DEBUG 级别及以上级别写入文件。

import logging

logging.basicConfig(level=logging.DEBUG,
format='%(asctime)s %(name)-12s %(levelname)-8s %(funcName)-5s %(message)s',
filename='OkanimeDownloader.log',
filemode='w')

console = logging.StreamHandler()
console.setLevel(logging.INFO)
formatter = logging.Formatter('%(levelname):-8s %(message)s')
console.setFormatter(formatter)
logging.getLogger('').addHandler(console)



dic = {'Google Drive': 'https://docs.google.com/file/d/0B-fK-jS8DjCeYVVwRWdfWnc1YWs/preview', 'Mega': '//vk.com/video_ext.php?oid=359177611&id=456239042&hash=59d982cc2450bc8d&sd', 'Openload': 'https://openload.co/embed/MVysKUs5rm4/%5BOKanime.com%5D_s%40ks2_-_11_%28animok%29.mp4', 'Streamango': 'https://streamango.com/embed/pnfdsqnedcffrttm/_OKanime_com_s_ks2_-_11_animok_mp4', 'Cloudy': 'https://www.cloudy.ec/embed.php?id=09c53285b6ac4', 'Tune.pk': 'https://embed.tune.pk/play/7147555/okanimecom-sks2-11-animok', 'Mp4upload': 'https://www.mp4upload.com/embed-swz4hesaisnm.html'}

logging.info('The dic: {}'.format(dic))

输出:

--- Logging error ---
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/logging/__init__.py", line 992, in emit
msg = self.format(record)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/logging/__init__.py", line 838, in format
return fmt.format(record)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/logging/__init__.py", line 578, in format
s = self.formatMessage(record)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/logging/__init__.py", line 547, in formatMessage
return self._style.format(record)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/logging/__init__.py", line 391, in format
return self._fmt % record.__dict__
ValueError: unsupported format character ':' (0x3a) at index 12
Call stack:
File "/Users/hamza/Dropbox/My Py Projects/Okanime/testing json.py", line 18, in <module>
logging.info('The dic: {}'.format(dic))
Message: "The dic: {'Google Drive': 'https://docs.google.com/file/d/0B-fK-jS8DjCeYVVwRWdfWnc1YWs/preview', 'Mega': '//vk.com/video_ext.php?oid=359177611&id=456239042&hash=59d982cc2450bc8d&sd', 'Openload': 'https://openload.co/embed/MVysKUs5rm4/%5BOKanime.com%5D_s%40ks2_-_11_%28animok%29.mp4', 'Streamango': 'https://streamango.com/embed/pnfdsqnedcffrttm/_OKanime_com_s_ks2_-_11_animok_mp4', 'Cloudy': 'https://www.cloudy.ec/embed.php?id=09c53285b6ac4', 'Tune.pk': 'https://embed.tune.pk/play/7147555/okanimecom-sks2-11-animok', 'Mp4upload': 'https://www.mp4upload.com/embed-swz4hesaisnm.html'}"
Arguments: ()

最佳答案

formatter = logging.Formatter('%(levelname)-8s : %(message)s')

此语法修复了 Formatter 中的错误

关于python - 我为在日志记录模块 python 中获取 ValueError 所做的工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45309504/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com