2009-08-18 14:05:15 -03:00
|
|
|
.. _topics-logging:
|
2009-01-11 19:48:36 +00:00
|
|
|
|
|
|
|
=======
|
|
|
|
Logging
|
|
|
|
=======
|
|
|
|
|
2009-08-20 18:17:48 -03:00
|
|
|
Scrapy provides a logging facility which can be used through the
|
|
|
|
:mod:`scrapy.log` module. The current underling implementation uses `Twisted
|
|
|
|
logging`_ but this may change in the future.
|
2009-01-11 19:48:36 +00:00
|
|
|
|
|
|
|
.. _Twisted logging: http://twistedmatrix.com/projects/core/documentation/howto/logging.html
|
|
|
|
|
2009-08-20 18:17:48 -03:00
|
|
|
Logging service must be explicitly started through the :func:`scrapy.log.start` function.
|
2009-01-11 19:48:36 +00:00
|
|
|
|
2009-08-20 18:17:48 -03:00
|
|
|
.. _topics-logging-levels:
|
|
|
|
|
|
|
|
Log levels
|
|
|
|
==========
|
|
|
|
|
|
|
|
Scrapy provides 5 logging levels:
|
|
|
|
|
|
|
|
1. :data:`~scrapy.log.CRITICAL` - for critical errors
|
|
|
|
2. :data:`~scrapy.log.ERROR` - for regular errors
|
|
|
|
3. :data:`~scrapy.log.WARNING` - for warning messages
|
|
|
|
4. :data:`~scrapy.log.INFO` - for informational messages
|
|
|
|
5. :data:`~scrapy.log.DEBUG` - for debugging messages
|
|
|
|
|
|
|
|
How to set the log level
|
|
|
|
========================
|
|
|
|
|
|
|
|
You can set the log level using the `--loglevel/-L` command line option, or
|
|
|
|
using the :setting:`LOGLEVEL` setting.
|
|
|
|
|
|
|
|
How to log messages
|
|
|
|
===================
|
2009-01-11 19:48:36 +00:00
|
|
|
|
|
|
|
Here's a quick example of how to log a message using the ``WARNING`` level::
|
|
|
|
|
2009-01-30 23:20:21 +00:00
|
|
|
from scrapy import log
|
2009-01-11 19:48:36 +00:00
|
|
|
log.msg("This is a warning", level=log.WARNING)
|
|
|
|
|
2009-08-20 18:17:48 -03:00
|
|
|
Logging from Spiders
|
|
|
|
====================
|
|
|
|
|
|
|
|
The recommended way to log from spiders is by using the Spider
|
|
|
|
:meth:`~scrapy.spider.BaseSpider.log` method, which already populates the
|
|
|
|
``domain`` argument of the :func:`scrapy.log.msg` function. The other arguments
|
|
|
|
are passed directly to the :func:`~scrapy.log.msg` function.
|
|
|
|
|
2009-01-11 19:48:36 +00:00
|
|
|
scrapy.log module
|
|
|
|
=================
|
|
|
|
|
|
|
|
.. module:: scrapy.log
|
|
|
|
:synopsis: Logging facility
|
|
|
|
|
|
|
|
.. attribute:: log_level
|
|
|
|
|
|
|
|
The current log level being used
|
|
|
|
|
|
|
|
.. attribute:: started
|
|
|
|
|
|
|
|
A boolean which is ``True`` is logging has been started or ``False`` otherwise.
|
|
|
|
|
|
|
|
.. 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``).
|
|
|
|
|
2009-08-20 18:17:48 -03:00
|
|
|
``loglevel`` is the logging level. Availables ones are: :data:`CRITICAL`,
|
|
|
|
:data:`ERROR`, :data:`WARNING`, :data:`INFO` and :data:`DEBUG`.
|
2009-01-11 19:48:36 +00:00
|
|
|
|
|
|
|
``log_stdout`` is a boolean which specifies if log should be sent to standard
|
|
|
|
output (if ``True``) or standard error (if ``False``)
|
|
|
|
|
|
|
|
.. function:: msg(message, level=INFO, component=BOT_NAME, domain=None)
|
|
|
|
|
|
|
|
Log a message
|
|
|
|
|
2009-08-20 18:17:48 -03:00
|
|
|
:param message: the message to log
|
|
|
|
:type message: str
|
2009-01-11 19:48:36 +00:00
|
|
|
|
2009-08-20 18:17:48 -03:00
|
|
|
:param level: the log level for this message. See
|
|
|
|
:ref:`topics-logging-levels`.
|
2009-01-11 19:48:36 +00:00
|
|
|
|
2009-08-20 18:17:48 -03:00
|
|
|
:param component: the component to use for logging, it defaults to
|
|
|
|
:setting:`BOT_NAME`
|
|
|
|
:type component: str
|
2009-01-11 19:48:36 +00:00
|
|
|
|
2009-08-20 18:17:48 -03:00
|
|
|
:param domain: the spider domain to use for logging this message. This
|
|
|
|
parameter should always be used when logging things related to a
|
|
|
|
particular spider.
|
|
|
|
:type domain: str
|
2009-01-11 19:48:36 +00:00
|
|
|
|
|
|
|
.. 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
|
|
|
|
|
2009-08-20 18:17:48 -03:00
|
|
|
It accepts the same parameters as the :func:`msg` function.
|
2009-01-11 19:48:36 +00:00
|
|
|
|
2009-08-20 18:17:48 -03:00
|
|
|
.. data:: CRITICAL
|
2009-01-11 19:48:36 +00:00
|
|
|
|
2009-08-20 18:17:48 -03:00
|
|
|
Log level for critical errors
|
2009-04-02 22:43:52 +00:00
|
|
|
|
2009-08-20 18:17:48 -03:00
|
|
|
.. data:: ERROR
|
|
|
|
|
|
|
|
Log level for errors
|
|
|
|
|
|
|
|
.. data:: WARNING
|
|
|
|
|
|
|
|
Log level for warnings
|
|
|
|
|
|
|
|
.. data:: INFO
|
|
|
|
|
|
|
|
Log level for informational messages (recommended level for production)
|
|
|
|
|
|
|
|
.. data:: DEBUG
|
2009-04-02 22:43:52 +00:00
|
|
|
|
2009-08-20 18:17:48 -03:00
|
|
|
Log level for debugging messages (recommended level for development)
|
2009-04-02 22:43:52 +00:00
|
|
|
|