Refactor logging handling

Old system was using manual string formatting for log messages.
This caused issues with common operations like exception logging
because often they need to be handled differently for Py2/Py3
compatibility. In order to unify the effort:

 - All logging is changed to `logging.level(msg, arg1, arg2)` style.
 - A `SafeLogger` is implemented to auto-decode exceptions properly
in the args (ref #1403).
 - Custom formatters were overriding useful logging functionality
like traceback outputing (ref #1402). They are refactored to be
more transparent. Traceback information is provided in `--debug`
mode for `read_file` errors in generators.
 - Formatters will now auto-format multiline log messages in order
to make them look related. Similarly, traceback will be formatted in
the same fashion.
 - `pelican.log.LimitFilter` was (ab)using logging message which
would result in awkward syntax for argumented logging style. This
functionality is moved to `extra` keyword argument.
 - Levels for errors that would result skipping a file (`read_file`)
changed from `warning` to `error` in order to make them stand out
among other logs.
 - Small consistency changes to log messages (i.e. changing all
to start with an uppercase letter) and quality-of-life improvements
(some log messages were dumping raw object information).
This commit is contained in:
Deniz Turgut 2014-07-22 11:48:15 -04:00
commit ed3209888a
14 changed files with 271 additions and 195 deletions

View file

@ -20,8 +20,10 @@ from six.moves.urllib.error import URLError
from six.moves.urllib.parse import urlparse
from six.moves.urllib.request import urlretrieve
from pelican.utils import slugify, SafeDatetime
# pelican.log has to be the first pelican module to be loaded
# because logging.setLoggerClass has to be called before logging.getLogger
from pelican.log import init
from pelican.utils import slugify, SafeDatetime
logger = logging.getLogger(__name__)
@ -131,7 +133,7 @@ def wp2fields(xml, wp_custpost=False):
title = unescape(item.title.contents[0])
except IndexError:
title = 'No title [%s]' % item.find('post_name').string
logger.warning('Post "%s" is lacking a proper title' % title)
logger.warning('Post "%s" is lacking a proper title', title)
filename = item.find('post_name').string
post_id = item.find('post_id').string
@ -594,24 +596,9 @@ def download_attachments(output_path, urls):
try:
urlretrieve(url, os.path.join(full_path, filename))
locations.append(os.path.join(localpath, filename))
except URLError as e:
error = ("No file could be downloaded from {}; Error {}"
.format(url, e))
logger.warning(error)
except IOError as e: #Python 2.7 throws an IOError rather Than URLError
# For japanese, the error might look kind of like this:
# e = IOError( 'socket error', socket.error(111, u'\u63a5\u7d9a\u3092\u62d2\u5426\u3055\u308c\u307e\u3057\u305f') )
# and not be suitable to use in "{}".format(e) , raising UnicodeDecodeError
# (This is at least the case on my Fedora running Python 2.7.5
# (default, Feb 19 2014, 13:47:28) [GCC 4.8.2 20131212 (Red Hat 4.8.2-7)] on linux2
try:
error = ("No file could be downloaded from {}; Error {}"
.format(url, e))
except UnicodeDecodeError:
# For lack of a better log message because we could not decode e, let's use repr(e)
error = ("No file could be downloaded from {}; Error {}"
.format(url, repr(e)))
logger.warning(error)
except (URLError, IOError) as e:
#Python 2.7 throws an IOError rather Than URLError
logger.warning("No file could be downloaded from %s\n%s", url, e)
return locations