Feb-28-2022, 05:20 PM
(Feb-28-2022, 04:42 PM)deanhystad Wrote: I am confused. In our test you used the global variable "i" and you incremented it like htis:
i += 1; logger.info("I am INFO 0")In log_format.py you changed "i" to an instance variable and you stop incrementing it.
Why do you expect "i" to change if you don't change it?
Thank you for your reply.
Actually in RUN 2 I did try to have class ContextFilter as
class ContextFilter(logging.Filter): def filter(self, record): record.log_count = i return TrueAnd call the logger.addFilter as
logger.addFilter(my.ContextFilter())but then I get following error:
Traceback (most recent call last): File "./log_format_test.py", line 36, in <module> i += 1; logger.info("I am INFO 0") File "~/miniconda3/lib/python3.6/logging/__init__.py", line 1301, in info self._log(INFO, msg, args, **kwargs) File "~miniconda3/lib/python3.6/logging/__init__.py", line 1437, in _log self.handle(record) File "~/miniconda3/lib/python3.6/logging/__init__.py", line 1446, in handle if (not self.disabled) and self.filter(record): File "~/miniconda3/lib/python3.6/logging/__init__.py", line 713, in filter result = f.filter(record) File "/local_home/log_format.py", line 19, in filter record.log_count = i NameError: name 'i' is not definedI am new to object oriented programming so don't understand it very well yet. I tried defining i as global in log_format_test.py and tried passing it as
logger.addFilter(my.ContextFilter(i))and many other combinations. The problem that you pointed was one of those many attempts.
I think I am missing something very basic here.