Passing a log handle to a module? Help needed with logging module and
Available news archives: comp.lang.tcl - comp.lang.python - comp.security.firewalls - sci.crypt - comp.lang.php - comp.lang.javascript
Google
 
Web news.hping.org


comp.lang.python archive

Passing a log handle to a module? Help needed with logging module and

From: rh0dium <sklass@pointcircle.com>
Date: Sun Jul 31 2005 - 06:29:00 CEST

Hi all,

So I have a slice of code which calls other python code. I have
started to take a real liking to the logging module, but I want to
extend this into the called python code. I have no idea how to pass
the handle from the calling code into the modules..

So basically here is what I do..

-- Main Program --

# Setup the logger..
import logging
log = logging.getLogger("main")
log.setLevel(logging.DEBUG)
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
formatter = logging.Formatter("%(asctime)s %(name)s %(levelname)-8s
%(message)s",datefmt='%d %b %Y %H:%M:%S')
ch.setFormatter(formatter)
log.addHandler(ch)

log.info(" ----> New run of started <---- ")

modules = []

# Add the mods directory so we can call the modules later
sys.path.insert( 0, os.getcwd() + "/mods" )

for metric in glob.glob("mods/*.py"):
     # Now lets start working on the individual metrics
     module_name, ext = os.path.splitext(os.path.basename(metric))
     log.debug( "Attempting to import %s" % module_name )

     try:
          module = __import__(module_name)
          modules.append( module )
          log.info( "Successfull import of %s" % module_name )
     except ImportError , e:
          log.error( "Failed import of %s - %s" % ( module_name, e) )
          pass

for module in modules:
     module.main( )

-----------------------

---- Called module -----

def main():
     print "Yep were in"

if __name__ == '__main__':
     main()

-----------------------

Now what I want to do is simple..
I want to change the called module so that it looks something like
this..

---- Called module -----

def main( logging_handle=None ):
     print "Yep were in"

     if logging_handle:
          log=logging.handle

         ( SOME WAY TO CHANGE THE getLogger name to Module )

     else:
          log=setUpLog()
     log.debug( "Hey it works..")

def setUpLog():
     # Setup the logger..
     import logging
     log = logging.getLogger("module")
     log.setLevel(logging.DEBUG)
     ch = logging.StreamHandler()
     ch.setLevel(logging.DEBUG)
     formatter = logging.Formatter("%(asctime)s %(name)s
%(levelname)-8s %(message)s",datefmt='%d %b %Y % H:%M:%S')
    ch.setFormatter(formatter)
    log.addHandler(ch)
     return log

if __name__ == '__main__':
     main()

-----------------------

But I can't figure out how to pass the d$!@# logging handle to the
called module - much less change the getLogger name to "module". Can
someone please help me with this? I know I must be doing something
braindead..

Thanks so much!!
Received on Thu Sep 29 17:16:46 2005