473,387 Members | 1,687 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Loggers and reloads

(Disclaimer: I'm new to both Python and OOP in general, and I may have
skipped a few lines in the docs)

I'm using the logging module inside one of my module. I instanciate a
logger which uses a StreamHandler to output to sys.stdout. It works
really well; however, I mainly use my module in the Python interpreter
in an interactive way, and I often have to reload said module.

The problem is that each time I reload my module, a new logger object
seems to be created, and the old one is not deleted. Both the new and
the old object are still working, so that each log message is
repeated; in general, if I did N reloads of my module, each message is
repeated N+1 times.

The del statement seems to only delete references, so that it can't be
used to get rid of the old logger objects. Maybe the
logging.shutdown() function is supposed to be of some help here, but I
don't see how to use it. Any hint as to what I'm doing wrong or what I
should do is welcome.

Here's some sample module code:

module logtest.py
---------------
import sys, logging

LOGLEVEL = logging.INFO
loghandler = logging.StreamHandler(sys.stdout)
logformat = logging.Formatter('%(name)s :: %(message)s')
loghandler.setFormatter(logformat)
logger = logging.getLogger('LoggerExample')
logger.addHandler(loghandler)
logger.setLevel(LOGLEVEL)

def info_message():
logger.info('This is an info-level message')
---------------
import logtest
logtest.info_message() LoggerExample :: This is an info-level message reload(logtest) <module 'logtest' from 'logtest.pyc'> logtest.info_message()

LoggerExample :: This is an info-level message
LoggerExample :: This is an info-level message
Jul 18 '05 #1
2 1375
Francois Bouffard wrote:
The problem is that each time I reload my module, a new logger object
seems to be created, and the old one is not deleted. Both the new and
the old object are still working, so that each log message is
repeated; in general, if I did N reloads of my module, each message is
repeated N+1 times.


The problem would disappear if you wrote a small testscript that uses your
module and execeted that from scratch, instead of reloading the module -
which might also create other state related problems.

But if you insist on using your module, you could place the logger
initialization into another module or encapsulate it in a function that you
call manually - but only once. As the logging systems state is kept in the
not-reloaded logging module, that should rid you of your problem. I bet it
would also be possible to check if for a given logger already a handler is
registered, and refuse to add another one.

--
Regards,

Diez B. Roggisch
Jul 18 '05 #2
> I bet it
would also be possible to check if for a given logger already a handler is
registered, and refuse to add another one.


You're completely right, thank you. There's always only one logger
object in my code, however, each time the module is reloaded, a new
handler is added. Therefore, something like:

for h in logger.handlers:
logger.removeHandler(h)

indeed does the trick if I put it before the logger.addHandler() call.

Thanks again.
Jul 18 '05 #3

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

Similar topics

3
by: Jeff Shannon | last post by:
I'm having some difficulty getting my logging configuration set correctly. I'm using a config file (copied at end of post), with the intent of setting several loggers which write to a combination...
13
by: Kai Grossjohann | last post by:
I have a web app which comprises different frames: a menu bar frame, a search form frame, a result list frame. Depending on the state, only a subset of these frames is available. (Eg, after...
0
by: David Virgil Hobbs | last post by:
What programming codes will enable me to achieve interaction between vb or c# and HTML form values, without a page reload occurring every time a script is run? At least with javascript and vb...
10
by: Marshall Dudley | last post by:
When I do the following line in Netscape, the popup loads as it should, but the parent window usually, but not always, reloads as well. <a href="#"...
0
by: Tamra | last post by:
Pages run perfectly on any computer with the .Net framework installed. Not so on those that do not. For example. One page uses a sqlDataSourceControl to populate a dropDownList. When an item is...
4
by: SevDer | last post by:
Hi We have a case that our AppDomain (workerprocess) reloads randomly. We simply have a public static hashtable that keeps an object inside which is determined by the key sessionID, and although...
9
by: mel | last post by:
Hi all, I need a persistent TCP connection with my web server over page reloads. This means that, even if the user goes to a different page (in my domain), I want to keep a TCP connection...
1
by: belred | last post by:
is there a way to dynamically get all the active loggers? i currently have code like this in different areas of the program: import logging log = logging.getLogger('foo') i would like to...
2
by: jonfroehlich | last post by:
I am a relative newbie to Python and its logging infrastructure; however, I have programmed extensively with Java/C# and log4j and log4net. That, I suppose, could be the root of my problem :) I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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...

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.