3.0 KiB
Logging
Scrapy logging facility is provided by the scrapy.log module, and is currently implemented as a wrapper for Twisted logging.
Logging must be started through the scrapy.log.start function.
Quick example
Here's a quick example of how to log a message using the WARNING level:
from scrapy import log
log.msg("This is a warning", level=log.WARNING)
scrapy.log module
System Message: ERROR/3 (<stdin>, line 25)
Unknown directive type "module".
.. module:: scrapy.log :synopsis: Logging facility
System Message: ERROR/3 (<stdin>, line 28)
Unknown directive type "attribute".
.. attribute:: log_level The current log level being used
System Message: ERROR/3 (<stdin>, line 32)
Unknown directive type "attribute".
.. attribute:: started A boolean which is ``True`` is logging has been started or ``False`` otherwise.
System Message: ERROR/3 (<stdin>, line 36)
Unknown directive type "function".
.. function:: start(logfile=None, loglevel=None, log_stdout=None)
Start the logging facility. This must be called before actually logging any
messages. Otherwise, messages logged before this call will get lost.
``logfile`` is a string with the file path to use for logging output. If
omitted, :setting:`LOGFILE` setting will be used. If both are ``None``, log
will be sent to standard output (if ``log_stdout`` or :setting:`LOG_STDOUT` is
``True``) or standard error (if ``log_stderr`` or :setting:`LOG_STDOUT` is
``False``).
``loglevel`` is the logging level. Availables ones are:
* ``scrapy.log.CRITICAL``
* ``scrapy.log.ERROR``
* ``scrapy.log.WARNING``
* ``scrapy.log.INFO``
* ``scrapy.log.DEBUG``
``log_stdout`` is a boolean which specifies if log should be sent to standard
output (if ``True``) or standard error (if ``False``)
System Message: ERROR/3 (<stdin>, line 57)
Unknown directive type "function".
.. function:: msg(message, level=INFO, component=BOT_NAME, domain=None)
Log a message
``message`` is a string with the message to log
``level`` is the log level for this message. See ``start()`` function for
available log levels.
``component`` is a string with the component to use for logging, it defaults to :setting:`BOT_NAME`
``domain`` is a string with the domain to use for logging this message.
This parameter should always be used when logging stuff paricular to any
domain or spider.
System Message: ERROR/3 (<stdin>, line 72)
Unknown directive type "function".
.. function:: exc(message, level=ERROR, component=BOT_NAME, domain=None)
Log an exception. Similar to ``msg()`` but it also appends a stack trace
report using `traceback.format_exc`.
.. _traceback.format_exc: http://docs.python.org/library/traceback.html#traceback.format_exc
``message`` - same as ``msg()`` function
``level`` - same as ``msg()`` function
``component`` - same as ``msg()`` function
``domain`` - same as ``msg()`` function
Logging from Spiders
The recommended way for logging from spiders is to use the Spider log() method, which already populates the :func:`~scrapy.log.msg` domain argument. The other arguments of the Spider log() method are the same as the :func:`~scrapy.log.msg` function.
System Message: ERROR/3 (<stdin>, line 90); backlink
Unknown interpreted text role "func".System Message: ERROR/3 (<stdin>, line 90); backlink
Unknown interpreted text role "func".