Logging
Scrapy uses logging for event logging. We’ll provide some simple
examples to get you started, but for more advanced use-cases it’s strongly
suggested to read its documentation thoroughly.
Logging works out of the box, and can be configured to some extent with the Scrapy settings listed in Logging settings.
Scrapy calls scrapy.utils.log.configure_logging() to set some reasonable
defaults and handle those settings in Logging settings when
running commands, so it’s recommended to manually call it if you’re running
Scrapy from scripts as described in Run Scrapy from a script.
Log levels
Python’s built-in logging defines 5 different levels to indicate the severity of a given log message. Here are the standard ones, listed in decreasing order:
logging.CRITICAL- for critical errors (highest severity)logging.ERROR- for regular errorslogging.WARNING- for warning messageslogging.INFO- for informational messageslogging.DEBUG- for debugging messages (lowest severity)
How to log messages
Here’s a quick example of how to log a message using the logging.WARNING
level:
import logging
logging.warning("This is a warning")
There are shortcuts for issuing log messages on any of the standard 5 levels,
and there’s also a general logging.log method which takes a given level as
argument. If needed, the last example could be rewritten as:
import logging
logging.log(logging.WARNING, "This is a warning")
On top of that, you can create different “loggers” to encapsulate messages. (For example, a common practice is to create different loggers for every module). These loggers can be configured independently, and they allow hierarchical constructions.
The previous examples use the root logger behind the scenes, which is a
top-level logger where all messages are propagated to (unless otherwise
specified). Using logging helpers is merely a shortcut for getting the root
logger explicitly, so this is also an equivalent of the last snippets:
import logging
logger = logging.getLogger()
logger.warning("This is a warning")
You can use a different logger just by getting its name with the
logging.getLogger function:
import logging
logger = logging.getLogger("mycustomlogger")
logger.warning("This is a warning")
Finally, you can ensure having a custom logger for any module you’re working on
by using the __name__ variable, which is populated with the current
module’s path:
import logging
logger = logging.getLogger(__name__)
logger.warning("This is a warning")
Logging from Spiders
Scrapy provides a logger within each Spider
instance, which can be accessed and used like this:
import scrapy
class MySpider(scrapy.Spider):
name = "myspider"
start_urls = ["https://scrapy.org"]
def parse(self, response):
self.logger.info("Parse function called on %s", response.url)
That logger is created using the Spider’s name, and every message logged
through it, as well as most messages logged by Scrapy itself while crawling,
carries a reference to the spider that triggered it. This is useful to tell
spiders apart when running several of them in the same process: add %(spider)s to LOG_FORMAT to
include the spider name in every log message, e.g.:
LOG_FORMAT = "%(asctime)s [%(name)s] %(spider)s %(levelname)s: %(message)s"
You can use any custom Python logger you want too. For example:
import logging
import scrapy
logger = logging.getLogger("mycustomlogger")
class MySpider(scrapy.Spider):
name = "myspider"
start_urls = ["https://scrapy.org"]
def parse(self, response):
logger.info("Parse function called on %s", response.url)
Logging configuration
Loggers on their own don’t manage how messages sent through them are displayed. For this task, different “handlers” can be attached to any logger instance and they will redirect those messages to appropriate destinations, such as the standard output, files, emails, etc.
By default, Scrapy sets and configures a handler for the root logger, based on the settings below.
Logging settings
If LOG_FILE is set, messages sent through the root logger will be
redirected to a file named LOG_FILE with encoding
LOG_ENCODING. If unset and LOG_ENABLED is True, log
messages will be displayed on the standard error. If LOG_FILE is set
and LOG_FILE_APPEND is False, the file will be overwritten
(discarding the output from previous runs, if any). Lastly, if
LOG_ENABLED is False, there won’t be any visible log output.
Set LOG_INSTALL_ROOT_HANDLER to False to prevent Scrapy from
installing a root logging handler altogether, e.g. to configure the root
logger yourself.
LOG_LEVEL determines the minimum level of severity to display, those
messages with lower severity will be filtered out. It ranges through the
possible levels listed in Log levels.
If LOG_SHORT_NAMES is set, then the logs will not display the Scrapy
component that prints the log. It is unset by default, hence logs contain the
Scrapy component responsible for that log output.
When logging to a terminal, LOG_COLOR colorizes messages by log
level.
With the default LOG_ROOT_FORMATTER, LOG_FORMAT and
LOG_DATEFORMAT specify formatting strings used as layouts for all
messages. Those strings can contain any placeholders listed in logging’s
logrecord attributes docs and datetime’s strftime
and strptime directives respectively.
LOG_ROOT_FORMATTER
Added in version 2.20.0.
Default: LogRootFormatter
Formatter of the root log handler that
configure_logging() installs. It implements
LogRootFormatterProtocol.
Note
This is a logging setting.
Rotating log files
Scrapy’s LOG_FILE setting writes logs to a single file. It does not
rotate log files automatically, but you can use Python’s standard
logging.handlers module when running Scrapy from a script.
For example, to rotate the log file every day:
import logging
from logging.handlers import TimedRotatingFileHandler
from scrapy import LogRootFormatter
from scrapy.crawler import CrawlerProcess
from scrapy.utils.project import get_project_settings
from myproject.spiders.myspider import MySpider
settings = get_project_settings()
settings.set("LOG_INSTALL_ROOT_HANDLER", False)
process = CrawlerProcess(settings)
handler = TimedRotatingFileHandler(
"scrapy.log",
when="midnight",
backupCount=7,
encoding=settings.get("LOG_ENCODING"),
)
handler.setFormatter(LogRootFormatter(settings))
root_logger = logging.getLogger()
root_logger.setLevel(settings.get("LOG_LEVEL"))
root_logger.addHandler(handler)
process.crawl(MySpider)
process.start()
Command-line options
There are command-line arguments, available for all commands, that you can use to override some of the Scrapy settings regarding logging.
--logfile FILEOverrides
LOG_FILE
--loglevel/-L LEVELOverrides
LOG_LEVEL
--nologSets
LOG_ENABLEDtoFalse
See also
- Module
logging.handlers Further documentation on available handlers
Custom Log Formats
A custom log format can be set for different actions by extending
LogFormatter class and making
LOG_FORMATTER point to your new class.
Advanced customization
Because Scrapy uses stdlib logging module, you can customize logging using all features of stdlib logging.
For example, let’s say you’re scraping a website which returns many HTTP 404 and 500 responses, and you want to hide all messages like this:
2016-12-16 22:00:06 [scrapy.spidermiddlewares.httperror] INFO: Ignoring
response <500 https://quotes.toscrape.com/page/1-34/>: HTTP status code
is not handled or not allowed
The first thing to note is a logger name - it is in brackets:
[scrapy.spidermiddlewares.httperror]. If you get just [scrapy] then
LOG_SHORT_NAMES is likely set to True; set it to False and re-run
the crawl.
Next, we can see that the message has INFO level. To hide it
we should set logging level for scrapy.spidermiddlewares.httperror
higher than INFO; next level after INFO is WARNING. It could be done
e.g. in the spider’s __init__ method:
import logging
import scrapy
class MySpider(scrapy.Spider):
# ...
def __init__(self, *args, **kwargs):
logger = logging.getLogger("scrapy.spidermiddlewares.httperror")
logger.setLevel(logging.WARNING)
super().__init__(*args, **kwargs)
If you run this spider again then INFO messages from
scrapy.spidermiddlewares.httperror logger will be gone.
You can also filter log records by LogRecord data. For
example, you can filter log records by message content using a substring or
a regular expression. Create a logging.Filter subclass
and equip it with a regular expression pattern to
filter out unwanted messages:
import logging
import re
class ContentFilter(logging.Filter):
def filter(self, record):
match = re.search(r"\d{3} [Ee]rror, retrying", record.message)
if match:
return False
A project-level filter may be attached to the root handler created by Scrapy. This is a useful way to filter all loggers in different parts of the project (middlewares, spider, etc.):
import logging
import scrapy
class MySpider(scrapy.Spider):
# ...
def __init__(self, *args, **kwargs):
for handler in logging.root.handlers:
handler.addFilter(ContentFilter())
Alternatively, you may choose a specific logger and hide it without affecting other loggers:
import logging
import scrapy
class MySpider(scrapy.Spider):
# ...
def __init__(self, *args, **kwargs):
logger = logging.getLogger("my_logger")
logger.addFilter(ContentFilter())
scrapy.utils.log module
- scrapy.utils.log.configure_logging(settings: Settings | dict[str, Any] | None = None, install_root_handler: bool | None = None) None[source]
Initialize logging defaults for Scrapy.
- Parameters:
settings (dict,
Settingsobject orNone) – settings used to create and configure a handler for the root logger (default: None).install_root_handler (bool) – whether to install root logging handler (default: the
LOG_INSTALL_ROOT_HANDLERsetting)
This function does:
Route warnings and twisted logging through Python standard logging
Set log levels for the Scrapy, Twisted and third-party loggers, which
LOG_LEVELScan overrideRoute stdout to log if LOG_STDOUT setting is True
When installing a root logging handler, this function also creates a handler for the root logger according to given settings (see Logging settings). You can override default options using
settingsargument. Whensettingsis empty or None, defaults are used.configure_loggingis automatically called when using Scrapy commands orCrawlerProcess, but needs to be called explicitly when running custom scripts usingCrawlerRunner. In that case, its usage is not required but it’s recommended.Another option when running custom scripts is to manually configure the logging. To do this you can use
logging.basicConfig()to set a basic root handler.Note that
CrawlerProcessautomatically callsconfigure_logging, so it is recommended to only uselogging.basicConfig()together withCrawlerRunner.This is an example on how to redirect
INFOor higher messages to a file:import logging logging.basicConfig( filename="log.txt", format="%(levelname)s: %(message)s", level=logging.INFO )
Refer to Run Scrapy from a script for more details about using Scrapy this way.
API reference
- class scrapy.LogRootFormatterProtocol(settings: Settings)[source]
Protocol for log root formatters.
Besides the
__init__method below, classes implementing this protocol must either subclassFormatteror implement its API.Mind that, during a crawl, Scrapy usually creates 2 instances of the log root formatter:
One early on with only the project settings.
Another one later on, where settings also includes add-on and spider settings.
If you are using multiple
Crawlerobjects, 1 log root formatter instance will be created for each crawler.
- class scrapy.LogRootFormatter(settings: Settings)[source]
Default
LOG_ROOT_FORMATTER.Allows customizing log formatting with the following settings.
Note
Alternative log root formatters are encouraged, but not required, to support these settings.
LOG_FORMAT
Default:
'%(asctime)s [%(name)s] %(levelname)s: %(message)s'String for formatting log messages. Refer to the Python logging documentation for the whole list of available placeholders, plus
%(spider)sfor the name of the spider that triggered the log message, or"-"for messages not tied to a spider (see Logging from Spiders).Note
This is a logging setting.
LOG_DATEFORMAT
Default:
'%Y-%m-%d %H:%M:%S'String for formatting date/time, expansion of the
%(asctime)splaceholder inLOG_FORMAT. Refer to the Python datetime documentation for the whole list of available directives.Note
This is a logging setting.
- class scrapy.logformatter.LogFormatter[source]
Class for generating log messages for different actions.
All methods must return a dictionary listing the parameters
level,msgandargswhich are going to be used for constructing the log message when callinglogging.log.Dictionary keys for the method outputs:
levelis the log level for that action, you can use those from the python logging library :logging.DEBUG,logging.INFO,logging.WARNING,logging.ERRORandlogging.CRITICAL.msgshould be a string that can contain different formatting placeholders. This string, formatted with the providedargs, is going to be the long message for that action.argsshould be a tuple or dict with the formatting placeholders formsg. The final log message is computed asmsg % args.
Users can define their own
LogFormatterclass if they want to customize how each action is logged or if they want to omit it entirely. In order to omit logging an action the method must returnNone.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": "Dropped: %(exception)s" + os.linesep + "%(item)s", "args": { "exception": exception, "item": item, }, }
- crawled(request: Request, response: Response, spider: Spider) LogFormatterResult[source]
Logs a message when the crawler finds a webpage.
- scraped(item: Any, response: Response | Failure | None, spider: Spider) LogFormatterResult[source]
Logs a message when an item is scraped by a spider.
- dropped(item: Any, exception: BaseException, response: Response | Failure | None, spider: Spider) LogFormatterResult[source]
Logs a message when an item is dropped while it is passing through the item pipeline.
- item_error(item: Any, exception: BaseException, response: Response | Failure | None, spider: Spider) LogFormatterResult[source]
Logs a message when an item causes an error while it is passing through the item pipeline.