For a fairly simple, pythonic way to get the class name for output with your registrar, simply use the logging class.
import logging # Create a base class class LoggingHandler: def __init__(self, *args, **kwargs): self.log = logging.getLogger(self.__class__.__name__) # Create test class A that inherits the base class class testclassa(LoggingHandler): def testmethod1(self): # call self.log.<log level> instead of logging.log.<log level> self.log.error("error from test class A") # Create test class B that inherits the base class class testclassb(LoggingHandler): def testmethod2(self): # call self.log.<log level> instead of logging.log.<log level> self.log.error("error from test class B") testclassa().testmethod1() testclassb().testmethod2()
By naming the registrar as indicated above, %(name)s will be the name of your class
Output example
$ python mymodule.py [2016-02-03 07:12:25,624] ERROR [testclassa.testmethod1:29] error from test class A [2016-02-03 07:12:25,624] ERROR [testclassb.testmethod2:36] error from test class B
Alternative Option (s)
For inheritance
import logging def log(className): return logging.getLogger(className) class testclassa: def testmethod1(self): log(self.__class__.__name__).error("error from test class A") class testclassb: def testmethod2(self): log(self.__class__.__name__).error("error from test class B") testclassa().testmethod1() testclassb().testmethod2()
kylehuff
source share