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

Discussion in 'Python' started by rh0dium, Jul 31, 2005.

  1. rh0dium

    rh0dium Guest

    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!!
    rh0dium, Jul 31, 2005
    #1
    1. Advertising

  2. Re: Passing a log handle to a module? Help needed with logging moduleand

    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
    Michael Hoffman, Jul 31, 2005
    #2
    1. Advertising

  3. rh0dium

    rh0dium Guest

    rh0dium, Aug 4, 2005
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Henrik_the_boss
    Replies:
    0
    Views:
    2,643
    Henrik_the_boss
    Nov 5, 2003
  2. David Konerding
    Replies:
    0
    Views:
    418
    David Konerding
    Sep 10, 2003
  3. Christoph Haas
    Replies:
    0
    Views:
    440
    Christoph Haas
    Jun 12, 2006
  4. Christoph Haas
    Replies:
    1
    Views:
    449
    Vinay Sajip
    Jun 14, 2006
  5. Royan
    Replies:
    1
    Views:
    1,433
    Andrea Francia
    Apr 1, 2008
Loading...

Share This Page