I am encountering a strange problem that relates to instance attributes. I have a variable logger, which I want to be an instance attribute. However, I get the error AttributeError: can't set attribute unless I move the attribute logger outside the __init__() function, which (IIRC), means that I am declaring logger as a class attribute (not what I want).
Here is a snippet of my code:
class MyScraper(ABC,scrapy.Spider):
"""Abstract base class for scraping non JS Web pages"""
#logger = None # Commented out, as I don't want class instance
def __init__(self, *args, **kwargs):
self.connection = None
self.channel = None
self.topic = None
log_format = "%(asctime)s - %(levelname)s - %(message)s"
log_level = 10
handler = TimedRotatingFileHandler("{0}.log".format(kwargs['log_filename']), when="midnight", interval=1)
handler.setLevel(log_level)
formatter = logging.Formatter(log_format)
handler.setFormatter(formatter)
# add a suffix which you want
handler.suffix = "%Y%m%d"
#need to change the extMatch variable to match the suffix for it
handler.extMatch = re.compile(r"^\d{8}$")
self.logger = logging.getLogger('my_logger') # <- barfs here
# finally add handler to logger
self.logger.addHandler(handler)
# Set up messaging infrastructure ...
Why am I getting this error, and how to fix it?
loggervariable,