Skip to content

Scrapy, a fast high-level web crawling & scraping framework for Python.

Merge branch 'master' into logFormatter-doc-patch

Anubhav Patel66a502db8c14
diff --cc scrapy/logformatter.py
index 717120242,65f347dcf..b4d6787ff
--- a/scrapy/logformatter.py
+++ b/scrapy/logformatter.py
@@@ -12,41 -12,26 +12,40 @@@ CRAWLEDMSG = u"Crawled (%(status)s) %(r
class LogFormatter(object):
"""Class for generating log messages for different actions.
--
- All methods must return a dictionary listing the parameters ``level``,
- ``msg`` and ``args`` which are going to be used for constructing the log
- message when calling logging.log.
++
+ All methods must return a dictionary listing the parameters ``level``, ``msg``
+ and ``args`` which are going to be used for constructing the log message when
+ calling ``logging.log``.
Dictionary keys for the method outputs:
- * ``level`` should be the log level for that action, you can use those
- from the python logging library: logging.DEBUG, logging.INFO,
- logging.WARNING, logging.ERROR and logging.CRITICAL.
- * ``msg`` should be a string that can contain different formatting
- placeholders. This string, formatted with the provided ``args``, is
- going to be the log message for that action.
+ * ``level`` is the log level for that action, you can use those from the
+ `python logging library <https://docs.python.org/3/library/logging.html>`_ :
+ ``logging.DEBUG``, ``logging.INFO``, ``logging.WARNING``, ``logging.ERROR``
+ and ``logging.CRITICAL``.
+ * ``msg`` should be a string that can contain different formatting placeholders.
+ This string, formatted with the provided ``args``, is going to be the long message
+ for that action.
+ * ``args`` should be a tuple or dict with the formatting placeholders for ``msg``.
+ The final log message is computed as ``msg % args``.
- * ``args`` should be a tuple or dict with the formatting placeholders
- for ``msg``. The final log message is computed as output['msg'] %
- output['args'].
- """
+ Here is an example on how to create a custom log formatter to lower the severity level of
+ the log message when an item is dropped from the pipeline::
+ class PoliteLogFormatter(logformatter.LogFormatter):
+ def dropped(self, item, exception, response, spider):
+ return {
+ 'level': logging.INFO, # lowering the level from logging.WARNING
+ 'msg': u"Dropped: %(exception)s" + os.linesep + "%(item)s",
+ 'args': {
+ 'exception': exception,
+ 'item': item,
+ }
+ }
-
+ """
-
++
def crawled(self, request, response, spider):
+ """Logs a message when the crawler finds a webpage."""
request_flags = ' %s' % str(request.flags) if request.flags else ''
response_flags = ' %s' % str(response.flags) if response.flags else ''
return {