Reputation: 13
I have some use case where classes are inherited. In base class I have created logger object
import logging
class abc():
logging.basicConfig(filename=r"D:\logger.txt",format="%(asctime)s %(message)s",filemode="w")
abcd=logging.getLogger()
abcd.setLevel(logging.INFO)
and in derived class I tried to use the same object
from test import abc
import logging
class pqr(abc):
p=logging.getLogger('abcd')
p.setLevel(logging.INFO)
def you(self):
self.p("Ad")
obj=pqr()
obj.you()
but I am getting this:
Traceback (most recent call last):
File "d:\Test\test2.py", line 9, in <module>
obj.you()
File "d:\Test\test2.py", line 7, in you
self.p("Ad")
TypeError: 'Logger' object is not callable
Upvotes: 1
Views: 1607
Reputation: 77827
I'm not sure what you're trying to accomplish with this code. You've provided three pieces, but included some disconnected components. Let's review at the pieces:
class abc():
logging.basicConfig(filename=r"D:\logger.txt",
format="%(asctime)s %(message)s",
filemode="w")
abcd=logging.getLogger()
abcd.setLevel(logging.INFO)
You've set up a class abc
, initialized the logging
1 configuration, and created a class attribute abcd
... which you don't refer to in your later code, so I'm not sure why it's in your posting.
class pqr(abc):
p=logging.getLogger('abcd')
p.setLevel(logging.INFO)
You derive a class pqr
from your previous class. Then you instantiate a new logger labeled "abcd", make it a class attribute, and set its reporting level. *Note that this is a new logger, not the one you created in abc
.
We're fine so far. However ...
# In pqr:
def you(self):
self.p("Ad")
# Main
obj=pqr()
obj.you()
obj.p (the self.p
in method you
) is the pqr
class logger. You've tried to call the logger object as a whole. It's not a method or function; it's an entire logger object. If you're expecting to log something, you have to call an appropriate method, such as self.p.info(<your message>)
.
Upvotes: 1