Hi,
Python 2.7.2
wxPython 2.9.1.1
OS X 10.7
I am very new to Python and programming, basically just a curious hobbyist. I
am building a learning app that hopefully will include a wxPython GUI. Right
now I am trying to understand my code better by including print statements.
Now I know that I could just print to the terminal, but I thought 'why not try
this nice little logger class.' And it works perfectly in the main class
App(wx.App), but I can't seem to pass the same logger object to the imported
modules. I'm verifying this by printing the logger object to terminal
(stdout?). I've spent close to 6 hours trying to figure this out, and I need
some help.
The logger object is created within a function in the main app class. Is this
wrong? I thought that logger ensured the same object is used by both classes.
The logger.getLogger(__name__) doesn't set anything other than the name used to
describe the source of the message, so how do I make sure I'm using the same
log object? I assume the problem is either scope (the log has to be in the
module, not the class) or passing the object properly, neither of which I"m
very comfortable with obviously.
Thanks in advance. Code snippets and prints follow.
Luke
# File / module one.
import FrameMaker # instantiates the subclassed wx.Frame object and fills it.
class App(wx.App):
def __init__(self)
self.logger = self.Log()
def Log(self):
appName = 'wxNew' # I thought this line would make it easier
to 'refactor' I hear people care about that.
logfile = ''.join([appName, '.log']) #Look, I learned to use
the .join() function!
if not os.path.exists(log file): # some examples I saw suggest
logger handles this file check.
f = open(logfile, 'w')
f.close()
logger = logging.getLogger('Main')
logger.setLevel(logging.DEBUG)
fh = logging.FileHandler(logfile)
fh.setLevel(logging.DEBUG) # If I only want one log, do I need
to create a special handler 'fh'?
format = logging.Formatter("%(asctime)s - %(name)s -
%(levelname)s - %(message)s")
fh.setFormatter(format)
logger.addHandler(fh)
logger.info('Starting log... ')
return logger
# File / Module two
class myFrame(wx.Frame):
def __init__(self, parent, id=-1, label="", size=(300, 500)):
#set the name of the logger.
self.logger = logging.getLogger('Frame') # The argument shouldn't
matter, as I follow the examples.
print self.logger
self.logger.info('In frame __init__, the size is : ', self.GetSize)
$ python app.py
<logging.Logger object at 0x105a1a110>
<logging.Logger object at 0x105a1a450>
--The log prints --
2011-07-26 01:39:07,642 - Main - INFO - Starting log...
2011-07-26 01:39:11,078 - Main - INFO - Shutting down main app.
Luke Thomas Mergner
Mechanicsville, MD
lmergner.blogspot.com
[email protected]
_______________________________________________
Tutor maillist - [email protected]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor