473,739 Members | 2,375 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.getLogg er("main")
log.setLevel(lo gging.DEBUG)
ch = logging.StreamH andler()
ch.setLevel(log ging.DEBUG)
formatter = logging.Formatt er("%(asctime) s %(name)s %(levelname)-8s
%(message)s",da tefmt='%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.splitex t(os.path.basen ame(metric))
log.debug( "Attempting to import %s" % module_name )

module = __import__(modu le_name)
modules.append( module )
log.info( "Successful l import of %s" % module_name )
except ImportError , e:
log.error( "Failed import of %s - %s" % ( module_name, e) )

for module in modules:
module.main( )


---- Called module -----

def main():
print "Yep were in"

if __name__ == '__main__':

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

---- Called module -----

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

if logging_handle:
log=logging.han dle

( SOME WAY TO CHANGE THE getLogger name to Module )

log.debug( "Hey it works..")
def setUpLog():
# Setup the logger..
import logging
log = logging.getLogg er("module")
log.setLevel(lo gging.DEBUG)
ch = logging.StreamH andler()
ch.setLevel(log ging.DEBUG)
formatter = logging.Formatt er("%(asctime) s %(name)s
%(levelname)-8s %(message)s",da tefmt='%d %b %Y % H:%M:%S')
ch.setFormatter (formatter)
log.addHandler( ch)
return log

if __name__ == '__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
Thanks so much!!

Jul 31 '05 #1
2 1742
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


However if you want a working example go here..

Aug 4 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

by: Paul | last post by:
Hmmm, didn't seem to work. I have set session.use_cookies = 1 and session.use_trans_sid = 1 in my php.ini file. Index.php contains: ---------------------------------------------------------------------------- <?php ini_set("session.use_cookies", "off"); ini_set("session.use_trans_sid", "on"); session_start(); $_SESSION = ""; $_SESSION = ""; echo "<form method='POST' action='login.php'>
by: Steve Erickson | last post by:
I have a logger class that uses the Python logging module. When I call it within a program using the unittest module, I get one line in the log file for the first test, two identical ones for the second, etc. I'm using local variables, which should go out of context with each test. Setting the 'propagate' property to False doesn't have any affect. import logging, os, signal, sys from datetime import datetime
by: plb | last post by:
All: I am struggling with an import problem... In my package, myapp, I have a module, logging.py. That module, naturally, imports the library module logging with an 'import logging' statement. However, when I use 'import myapp.logging' in my script, the myapp.logging module tries to import itself rather than the library logging module.
by: plb | last post by:
All: I am struggling with an import problem... In my package, myapp, there is a module called logging. This module, naturally, imports the standard library module logging. However, when I try 'import myapp.logging', the 'import logging' statement appears to be finding the myapp.logging module instead of the one in the standard library.
by: Chris Smith | last post by:
Hola, pythonisas: The documentation for the logging module is good, but a bit obscure. In particular, there seems to be a lot of action at a distance. The fact that getLogger() can actually be a call to Logger.__init__(), which is mentioned in para 6.29.1, also bears stressing on 6.29. I grasp _why_ you'd implement it that way, but I may not be the only coder who feels queasy with the word 'get' being used both to fetch an instance and...
by: Frank Aune | last post by:
Hello, I've been playing with the python logging module lately, and I manage to log to both stderr and MySQL database. What I'm wondering, is if its possible to specify the database handler in a config file like: class=DBHandler
by: hg | last post by:
I have a function called logprintf that take a printf-like syntax. On top of that I want to implement a debug-printf function which calls logprintf but prefix the information using a string: void _dbglogprintf(const char *szFormat, ...) { char szOutput; va_list arglist; sprintf(szOutput, "DEBUG: ");
by: koara | last post by:
Hello, is there a way to access a module that is hidden because another module (of the same name) is found first? More specifically, i have my own logging.py module, and inside this module, depending on how initialization goes, i may want to do 'from logging import *' from the built-in logging. I hope my description was clear, cheers. I am using python2.4.
by: The Frog | last post by:
Hi Guys, Just wanting some opinions on best method to approach this. I am working on an Access97 db and we have two forms and an ADO connection object to contend with. The desired state is this- first form has the functionality for the user to choose an MDB file to connect to, the user does as such, and the ado connection object is set up and the connection is opened.
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.