472,102 Members | 2,113 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,102 software developers and data experts.

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

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
2 1644
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
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.

Similar topics

1 post views Thread by Paul | last post: by
8 posts views Thread by Steve Erickson | last post: by
3 posts views Thread by Chris Smith | last post: by
5 posts views Thread by koara | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.