1

trying to understand and learn how to write packages... testing with something i've always used, logging...

Can you please help me understand why the 'log' variable is not working... and no logging is working on the screen?

Thanks!

main.py :

#!/opt/local/bin/python
 import sys
 sys.path.append('CLUSTER')
 import clusterlogging.differentlogging
 clusterlogging.differentlogging.consolelogging()
log.debug("Successfully logged in")

differentlogging.py

#!/opt/local/bin/python
def consolelogging():
 import logging
 class NullHandler(logging.Handler):
 def emit(self, record):
 pass
print "Console Logging loaded"
DEFAULTLOGLEVEL=logging.INFO
log = logging.getLogger(__name__)
log.addHandler(NullHandler())
log.debug("Successfully logged in")
def mysqllogging():
 print "mysql logging module here"
def sysloglogging():
print "rsyslog logging module here"

output

Console Logging loaded
Traceback (most recent call last):
 File "./svnprod.py", line 10, in <module>
 log.debug("Successfully logged in")
NameError: name 'log' is not defined
asked Aug 29, 2011 at 23:31
2
  • 1
    The recommended shebang for Python is #!/usr/bin/env python. Commented Aug 29, 2011 at 23:34
  • 1
    Something appears wrong with your post - from what I can see, it wouldn't compile. For example, the line after the def sysloglogging(): is not indented. Also, your code in consolelogging() doesn't appear to do anything useful, as the definitions in there are local to that function. The logging docs show you how to arrange logging across multiple modules - see docs.python.org/howto/… Commented Aug 29, 2011 at 23:43

3 Answers 3

4

log is a global variable in the differentlogging module. Thus you can access it as clusterlogging.differentlogging.log.

You could also do something like from clusterlogging.differentlogging import log and then access it as just log.

Edit: actually, on reviewing your code again I don't know what to make of it. Could you please fix up your code indentation so that it makes sense? Are you defining log inside the consolelogging function? If so, you'll need to either make it global with global log or return it from the function and assign it to a variable log on the line where you call the function.

answered Aug 29, 2011 at 23:36
Sign up to request clarification or add additional context in comments.

Comments

0

This will return the log array, and you will be able to use the logging function associated.

main.py:

#!/usr/bin/env python
import sys
sys.path.append('CLUSTER')
import clusterlogging.differentlogging
log=clusterlogging.differentlogging.ttylogging()
log.debug("Logging module loaded")
log.info ("It worked")

differentlogging.py :

#!/usr/bin/env python
def ttylogging():
 print "Console Logging loaded"
 import sys
 import logging
 class NullHandler(logging.Handler):
 def emit(self, record):
 pass
 DEFAULTLOGLEVEL=logging.INFO
 log = logging.getLogger(__name__)
 log.addHandler(NullHandler())
 log.setLevel(DEFAULTLOGLEVEL)
 logStreamHandler = logging.StreamHandler(sys.stdout)
 logStreamHandler.setFormatter(logging.Formatter("%(asctime)s %(levelname)5s %(name)s %(lineno)d: %(message)s"))
 log.addHandler(logStreamHandler)
 return log 
def mysqllogging():
 print "mysql logging module here"
def sysloglogging():
 print "rsyslog logging module here"
answered Aug 30, 2011 at 13:51

Comments

0

Your main.py doesn't do anything to define the name log in the global namespace. Importing a module can define names in the namespace of that module, but can't put anything in the global namespace.

In your main.py you should add this statement:

from clusterlogging.differentlogging import log

By the way, I that is such a long module name, I would use import as:

import clusterlogging.differentlogging as difflogging
log = difflogging.log

EDIT: I originally recommended this but it won't work:

from difflogging import log # doesn't work

You might even want to use a really short name like dl:

import clusterlogging.differentlogging as dl
dl.log('whatever')

Since dl is really short, maybe you don't need to get log bound in the global namespace.

Also, you could get every name from a module by using import * but this is not recommended.

from clusterlogging.differentlogging import * # not recommended

You usually don't want to clutter the global namespace with all the stuff defined in a module. Import just what you need. This is tidier and helps document what you are actually using.

answered Aug 29, 2011 at 23:42

3 Comments

import clusterlogging.differentlogging as difflogging ; from difflogging import log is an error. Importing a module using as only changes the name it is given in the importing namespace; it's still stored by the full name in sys.modules.
I tried, from clusterlogging.differentlogging import log, and got: AttributeError: 'module' object has no attribute 'log'. The full solution to my problem is posted below
Then do it this way: import clusterlogging.differentlogging as difflogging; log = difflogging.log Sorry that I posted something that didn't work. Usually I test my code but I don't have this clusterlogging.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.