I am developing customized logger program,as per the requirement i need to fetch the process,thread and name of object Inside the called function(In below example its obj needs to fetch inside the get_configured_logger function) and class name to which obj belongs. as shown with comments in below code, please give some ideas to achieve this.
import logging, logging.handlers
from logging import StreamHandler, Formatter
class A:
def get_configured_logger(self,name):
logger = logging.getLogger(name)
if (len(logger.handlers) == 0):
FORMAT = "%(process)s %(thread)s:-(asctime)s - %(name)s - %(levelname)s - %(message)-%(module)"
#print 'process:',process
#print 'thread:',thread
#print 'levelname:',levelname
#print 'Module:',(name portion of filename).
#print 'obj:,'name of the object(Eg:obj),current function( Eg: get_configured_logger) called by'
#print 'class name:(obj is instance of class)'
formatter = logging.Formatter(fmt=FORMAT)
handler = logging.StreamHandler()
handler.setFormatter(formatter)
logger.addHandler(handler)
logger.setLevel(logging.DEBUG)
return logger
if __name__=="__main__":
obj=A()
logger = obj.get_configured_logger("DEMO")
logger.debug("TEST")
Thanks
hema
ident
attribute, for getting the thread's identifier. For the current process I usually useos.getpid()
– Homopolar