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?
1 Answer 1
If you look at the source code (or even if you just print(scrapy.Spider.logger) you can see that Spider.logger is a property, and in particular without a setter defined, so you can't easily assign to it.
You don't necessarily need to create your own logger if you want to add additional handlers though, so I'm not sure what you're trying to achieve beyond that. Though if you ''really'' wanted to override the default self.logger, since you're subclassing Spider there's nothing stopping you from adding something like:
@property
def logger(self):
return logging.getLogger('my_logger')
to your class.
1 Comment
@property then i need to move the self.variable to self._variable ... so i needed to assign object._variable = "x" and not object.variable = "x" ..
loggervariable,