By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,165 Members | 849 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,165 IT Pros & Developers. It's quick & easy.

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

P: n/a
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!!

Jul 31 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
rh0dium wrote:
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?
--
Michael Hoffman
Jul 31 '05 #2

P: n/a
I found my problem it wasn't this piece of the problem it was
another...

Thanks.

However if you want a working example go here..
http://www.onlamp.com/pub/a/python/2...2/logging.html

Aug 4 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.