How to specify log file name with spider's name in scrapy?
Question: I'm using scrapy,in my scrapy project,I created several spider classes,as the
official document said,I used this way to specify log file name:

    
    
     def logging_to_file(file_name):
    """
    @rtype: logging
    @type file_name:str
    @param file_name:
    @return:
    """
    import logging
    from scrapy.utils.log import configure_logging
    configure_logging(install_root_handler=False)
    logging.basicConfig(
        filename=filename+'.txt',
        filemode='a',
        format='%(levelname)s: %(message)s',
        level=logging.DEBUG,
    
    )
     Class Spider_One(scrapy.Spider):
          name='xxx1'
          logging_to_file(name)
      ......
     Class Spider_Two(scrapy.Spider):
          name='xxx2'
          logging_to_file(name)
      ......
    

Now,if I start `Spider_One`,everything is correct!But,if I start `Spider
Two`,the log file of `Spider Two` will also be named with the name of `Spider
One`!  
I have searched many answers from google and stackoverflow,but
unfortunately,none worked!  
I am using python 2.7 & scrapy 1.1!  
Hope anyone can help me!


Answer: It's because you initiate `logging_to_file` every time when you load up your
package. You are using a class variable here where you should use instance
variable.

When python loads in your package or module ir loads every class and so on.

    
    
    class MyClass:
        # everything you do here is loaded everytime when package is loaded
        name = 'something'
    
        def __init__(self):
            # everything you do here is loaded ONLY when the object is created
            # using this class
    

To resolve your issue just move `logging_to_file` function call to your
spiders `__init__()` method.

    
    
    class MyClass(Spider):
        name = 'xx1'
    
        def __init__(self):
            super(MyClass, self).__init__()
            logging_to_file(self.name)
    

