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

时间:2017-07-25 16:59:05

标签: python python-3.x logging

我想将log写入文件和stdout,因此我使用此代码,但我总是在stdout中获取此ValueError但不在文件中。

我希望从INFO级别及以上级别登录以获取stdout和文件。和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: ()

1 个答案:

答案 0 :(得分:2)

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

此语法修复了Formatter

中的错误