Logging module gives duplicate log entries
Peter Otten
__peter__ at web.de
Wed Aug 22 03:05:59 EDT 2007
Shiao wrote:
> Maybe my question wasn't very clear. What I meant is that these four
> lines lead in my case to two entries per logged event:
>
> applog = logging.getLogger()
> applog.setLevel(logging.DEBUG)
> hdl = logging.FileHandler('/tmp/foo.log')
> applog.addHandler(hdl)
>
> However if I REPLACE the above by:
>
> logging.basicConfig(level=logging.DEBUG, filename='/tmp/foo.log')
>
> things work as expected.
Then you have a logic error in your program that causes that piece of code
to run twice (I simulate that by the for-loop):
$ python
Python 2.5.1 (r251:54863, May 2 2007, 16:56:35)
[GCC 4.1.2 (Ubuntu 4.1.2-0ubuntu4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import logging
>>> for i in range(2):
... logging.getLogger().addHandler(logging.StreamHandler())
...
>>> logging.warn("twice")
twice
twice
logging.basicConfig() on the other hand does nothing if it finds existing
handlers:
$ python
Python 2.5.1 (r251:54863, May 2 2007, 16:56:35)
[GCC 4.1.2 (Ubuntu 4.1.2-0ubuntu4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import logging
>>> for i in range(2):
... logging.basicConfig()
...
>>> logging.warn("once")
WARNING:root:once
Peter
More information about the Python-list
mailing list