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

R

rh0dium

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!!
 
M

Michael Hoffman

rh0dium said:
But I can't figure out how to pass the d$!@# logging handle to the
called module -

I'm sorry, but what have you tried? It seems like it should be a simple
matter of calling module.main(log).
> much less change the getLogger name to "module".

Why do you want to do this?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top