logging producing redundant entries

Peter Otten __peter__ at web.de
Fri Mar 31 01:50:13 EST 2006


Jed Parsons wrote:

> I'm using the logging module for the first time.  I'm using it from
> within Zope Extensions.
> 
> My problem is that, for every event logged, the logger is producing
> multiple identical entries with the timestamp the same down to the
> millisecond.
> 
> Is this something I'm doing wrong?
> 
> Log snippet:
> 
> 2006-03-30 16:20:14,173 INFO: Login: Jed Parsons
> 2006-03-30 16:20:14,173 INFO: Login: Jed Parsons
> 2006-03-30 16:20:14,173 INFO: Login: Jed Parsons
> 2006-03-30 16:20:14,173 INFO: Login: Jed Parsons
> 2006-03-30 16:20:14,173 INFO: Login: Jed Parsons
> 2006-03-30 16:20:14,173 INFO: Login: Jed Parsons
> 2006-03-30 16:20:14,173 INFO: Login: Jed Parsons
> 2006-03-30 16:20:14,173 INFO: Login: Jed Parsons
> 
> I would like only one of the above lines in my log file; not all those
> copies.
> 
> I'm using this simple logging setup at the top of a zope Extension module:
> 
>      import logging
>      # basicConfig for python 2.3
>      logging.basicConfig()
>      _logger = logging.getLogger("login")
>      _logger.setLevel(logging.DEBUG)
>      _handler = logging.FileHandler(LOG_ROOT, 'login.log'))
>      _formatter = logging.Formatter("%(asctime)s %(levelname)s:
> %(message)s")
>      _handler.setFormatter(_formatter)
>      _logger.addHandler(_handler)
> 
> So these are global to the module.  The log lines above were produced by
> what I expected would be a single call to _logger.info() in a function
> in the module:
> 
>      _logger.info("Login: %s %s" % (firstname, lastname))
> 
> Can anyone explain what I'm doing wrong?  Many thanks,

Please cut and paste -- the FileHandler arguments are wrong and there is an
extra ')'. As alex23 said, basicConfig() adds a handler, but that logs to
stderr by default. You seem to be executing the code given above multiple
times. You can verify that by prepending your snippet with

    f = open("metalog.txt", "a")
    f.write("configuring handler\n")
    f.close()

If metalog.txt contains multiple lines after your program has terminated
(you may have to shut down Zope -- don't know about that), a quick fix
might be

    import logging
    if not logging.root.handlers:
        # your setup code

Peter



More information about the Python-list mailing list