Mohamed Mohamed - 3 years ago 109
Python Question

what I've done to get ValueError in logging module python

I want to write log to both file and stdout, Hence I use this code but I always get this ValueError in stdout but not in file.

I want logging from INFO level and above to get to both stdout and a file. and DEBUG level and above to be wrote to a file.

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))


output:

--- 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: ()

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

This syntax fix the error in the Formatter

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download