I want to capture log entries into a string, to display in a wx dialog. I just can't get the StringIO to be filled by the log entries... what wrong here?
# prepare logging
log = StringIO.StringIO('Report')
logger = logging.getLogger (__name__)
logger.setLevel(logging.DEBUG)
handler = logging.StreamHandler(log)
logger.addHandler(handler)
# do something and log it
logging.info('Some log entry')
# display log
handler.flush()
dlg = wx.lib.dialogs.ScrolledMessageDialog(window, log.getvalue(), "Import Report")
dlg.ShowModal()
log.close()
The dialog shows the initial StringIO content ('Report'), but nothing added via the log ('Some log entry').
I looked at this without enlightenment, and read the logging tutorials without hunches, so I turned here.
Thanks for pointers, nobi
logger = logging.getLogger (__name__)
You are passing a name to getLogger
, so it is giving you a logger with the specified name (instead of the root logger, this is important later).
logging.info('Some log entry')
You are calling logging.info (note: very different from logger.info). logging.info
logs to the root logger. Which is a different entity than your logger
.
Either switch that call to logger.info('Some log entry')
or just get the root logger from logging.getLogger()
by not passing it a string.
Reading back over this I just used the word "logger" about 60 times in about as many contexts, so if this is confusing I can try to clarify.