Logging module gives duplicate log entries

Discussion in 'Python' started by Shiao, Aug 21, 2007.

  1. Shiao

    Shiao Guest

    Hi,
    I am getting duplicate log entries with the logging module.

    The following behaves as expected, leading to one log entry for each
    logged event:

    logging.basicConfig(level=logging.DEBUG, filename='/tmp/foo.log')

    But this results in two entries for each logged event:

    applog = logging.getLogger()
    applog.setLevel(logging.DEBUG)
    hdl = logging.FileHandler('/tmp/foo.log')
    applog.addHandler(hdl)


    The app is based on the web.py framework, so I guess my problem may
    be
    connected to be some interaction with other uses of logging within
    the
    framework. This is not specific to the root logger, the same happens
    with logging.getLogger('foo').

    Any clue would be more than welcome.

    best,
    ShiaoBu
     
    Shiao, Aug 21, 2007
    #1
    1. Advertising

  2. Shiao

    Amit Khemka Guest

    On 8/21/07, Shiao <> wrote:
    > Hi,
    > I am getting duplicate log entries with the logging module.
    >
    > The following behaves as expected, leading to one log entry for each
    > logged event:
    >
    > logging.basicConfig(level=logging.DEBUG, filename='/tmp/foo.log')
    >
    > But this results in two entries for each logged event:
    >
    > applog = logging.getLogger()
    > applog.setLevel(logging.DEBUG)
    > hdl = logging.FileHandler('/tmp/foo.log')
    > applog.addHandler(hdl)
    >


    You need to remove the handler from the logging object

    # remove the handler once you are done
    applog.removeHandler(hdl)


    Cheers,
    amit.

    ----
    Amit Khemka
    website: www.onyomo.com
    wap-site: www.owap.in
    Home Page: www.cse.iitd.ernet.in/~csd00377

    Endless the world's turn, endless the sun's Spinning, Endless the quest;
    I turn again, back to my own beginning, And here, find rest.
     
    Amit Khemka, Aug 21, 2007
    #2
    1. Advertising

  3. Shiao

    Shiao Guest


    >
    > You need to remove the handler from the logging object
    >
    > # remove the handler once you are done
    > applog.removeHandler(hdl)
    >
    > Cheers,
    > amit.
    >


    I'm not sure how this could help.
     
    Shiao, Aug 21, 2007
    #3
  4. Shiao

    Peter Otten Guest

    Shiao wrote:

    >> You need to remove the handler from the logging object
    >>
    >> # remove the handler once you are done
    >> applog.removeHandler(hdl)


    > I'm not sure how this could help.


    If you have multiple handlers you'll get a logged message for every handler.

    In your code logging.basicConfig() implicitly adds a handler and you add
    another one calling addHandler(). Let's use a StreamHandler to see the
    effect immediately:

    >>> import logging
    >>> logging.basicConfig()
    >>> root = logging.getLogger()
    >>> root.warn("once")

    WARNING:root:eek:nce
    >>> handler = logging.StreamHandler()
    >>> root.addHandler(handler)
    >>> root.warn("twice")

    WARNING:root:twice
    twice
    >>> root.removeHandler(handler)
    >>> root.warn("once again")

    WARNING:root:eek:nce again

    Of course it would be preferable not to add a second handler in the first
    place, either by omitting the basicConfig() or the explicit addHandler()
    call.

    Peter
     
    Peter Otten, Aug 21, 2007
    #4
  5. Shiao

    Shiao Guest

    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.
     
    Shiao, Aug 21, 2007
    #5
  6. Shiao

    Peter Otten Guest

    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:eek:nce

    Peter
     
    Peter Otten, Aug 22, 2007
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Wade
    Replies:
    3
    Views:
    1,733
  2. rh0dium
    Replies:
    2
    Views:
    356
    rh0dium
    Aug 4, 2005
  3. Gal Aviel
    Replies:
    1
    Views:
    485
    Gerard Flanagan
    Mar 4, 2008
  4. Jean-Michel Pichavant

    python logging, handling multiline log entries

    Jean-Michel Pichavant, Nov 4, 2010, in forum: Python
    Replies:
    0
    Views:
    266
    Jean-Michel Pichavant
    Nov 4, 2010
  5. Jean-Michel Pichavant

    Re: python logging, handling multiline log entries

    Jean-Michel Pichavant, Nov 4, 2010, in forum: Python
    Replies:
    0
    Views:
    513
    Jean-Michel Pichavant
    Nov 4, 2010
Loading...

Share This Page