nick nick - 1 year ago 117
Python Question

How to config file name of logger in each class in scrapy?

I have used scrapy for several months.Several weeks ago,I started to use file to record log information.I wrote

function as this:

def logging_to_file(file_name):
import logging
from scrapy.utils.log import configure_logging

filename = '%s-log.txt' % file_name
import os

if os.path.isfile(filename):
format='%(levelname)s: %(message)s',
return logging.getLogger()

Then,in each scrapy spider class,I use
logger = logging_file.logging_to_file('./logs/xxx-%s' % time.strftime('%y%m%d'))
function to customize log file name.

Something went wrong today,I found if I wrote two scrapy classes in one
file,and after I started spider of the second class,the log file was also named by the file name which is given in the first class!

I think this is caused by python log rule,but I don't know how to resolve.

Answer Source

I'm not sure if I understand what your question is but in general you don't have to create any functions or anything to configure your logger.

What you should do create a logger and assign it a FileHandler and then just use your created logger to log your info.

import logging
logger = logging.getLogger('mylogger')  # skip name for global rules
fh = logging.FileHandler(LOG_FILE_DIR, mode='a')

You can put this anywhere that gets executed on program startup, like or something.

Now when you want to log something just:

logger = logging.getLogger('mylogger')
logger.error("error happened, oh no!")

Official Python logging tutorial can be found here

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download