0

I use the scrapy library for scraping and it handels all the logging for me. The log file is very big and I was hoping for another way than to read through the whole file. I'm only interested in the exception.

Is it posible to get the raw logging.exception and read from that without creating another log file?

1 Answer 1

1

Use the python logging facility which is what Scrapy uses. At the program entry point you need to customize this logging facility, it can be in the __init__.py file for example or anything else before you use any log related calls.

What you need to do is set different handlers for different levels (ERROR, WARNING, ...). Exception level do not exist, you might be mistaking with Error or Critical levels.

This is the "Logging How To", you should check it.

Your solution would look like :

import logging

#this is the logger Scrapy uses throughout the app.
logger = logging.getLogger() 

my_exception_handler = logging.FileHandler()
my_exception_handler.setLevel(Logging.ERROR)

logger.addHandler(my_exception_handler)

Finally, if you are really talking about exception and not error or critical levels. You'll need to inherit from your Exception class or overwrite it so you can use the same logging approach I just explained.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, I got it working with your answer, however i was hoping to avoid creating files. Now i create the file, read the file, store the exceptions in mongodb, and delete the file. The problem is that i dont do the logging myself, is it possible to log directly to mongodb?
This was not in your question. You can do what you want by creating your own handler which would subclass StreamHandler() and setting the emit() method, which would call pymongo/mongodb. The process stays the same as stated above. Please accept my answer if this has fix your problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.