Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to see output of print statements when scrapy logger enabled

For debug purposes I need to see both output of log messages and print statements when working with Scrapy. Hovewer, when I start Scrapy logger, I no longer see output of print statements! How should I modify the following sample code to see both print statements?

from scrapy import log

print 'PRINT OUTPUT BEFORE'
log.start(loglevel='DEBUG',logstdout=True)
print 'PRINT OUTPUT AFTER'

Output:

PRINT OUTPUT BEFORE

I beleive redirecting stderr or stdout may solve the problem. Any help would be greatly appreciated!

like image 255
Apogentus Avatar asked Oct 02 '13 10:10

Apogentus


People also ask

How do I print logging info in Python?

Python - Print Logs in a File. If you want to print python logs in a file rather than on the console then we can do so using the basicConfig() method by providing filename and filemode as parameter. The format of the message can be specified by using format parameter in basicConfig() method.

How do I print logs from console?

You should use the console. log() method to print to console JavaScript. The JavaScript console log function is mainly used for code debugging as it makes the JavaScript print the output to the console.

How do you run a Scrapy project?

You can start by running the Scrapy tool with no arguments and it will print some usage help and the available commands: Scrapy X.Y - no active project Usage: scrapy <command> [options] [args] Available commands: crawl Run a spider fetch Fetch a URL using the Scrapy downloader [...]


2 Answers

logstdout must be set to False to disable stdout from being redirected.

from scrapy import log

print 'PRINT OUTPUT BEFORE'
log.start(loglevel='DEBUG', logstdout=False)
print 'PRINT OUTPUT AFTER'

With output:

PRINT OUTPUT BEFORE
PRINT OUTPUT AFTER
like image 193
user650654 Avatar answered Sep 30 '22 21:09

user650654


I think above works for old version. They seems to have deprecated scrapy.log . Refer: https://docs.scrapy.org/en/latest/topics/logging.html

Eg:

import logging

logging.warning("This is a warning")
logging.info("This is an info")
logging.error("This is an error")

As my config was set to warn. I got only below

WARNING:root:This is a warning
ERROR:root:This is an error

Note that this is helpful if you are using scrapyd. As scrapy crawl will print all but scrapyd doesn't do same way.

like image 38
mrtipale Avatar answered Sep 30 '22 21:09

mrtipale