473,562 Members | 3,126 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 )

try:
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) )
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.han dle

( 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.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__':
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 1732
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
7772
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 =...
8
1981
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...
2
1279
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.
0
1353
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.
3
2458
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...
4
3317
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
3
2054
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: ");
5
1516
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. ...
6
1786
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...
0
7652
marktang
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...
0
7576
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7867
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8095
jinu1996
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...
1
7625
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...
0
6219
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5193
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...
1
1187
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
901
bsmnconsultancy
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...

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.