Removing default logging handler (causes duplicate logging)

Discussion in 'Python' started by Gal Aviel, Mar 4, 2008.

  1. Gal Aviel

    Gal Aviel Guest

    Hello All,

    I want to add a logger to my application, then addHandler to it to log to a
    special destination.

    Unfortunately when I use logging.getLogger("my_logger") to create the new
    logger, it apparently comes with a default handler that logs to STDOUT (or
    STDERR?). When I add my special handler it works Ok, but still logs to terminal,
    which I do not want.

    The only way I found to remove the default handler is by using
    'logger.removeHandler(logger.handlers[0])'.

    Is there a more elegant solution that uses the logging API?

    my setup: Python 2.5.1 on Linux Suse 9.

    P.S.I am not using Basic config, just 'import logging' and then regular
    logging.* calls.

    Thanks in advance,
    Gal Aviel.
    Gal Aviel, Mar 4, 2008
    #1
    1. Advertising

  2. On Mar 4, 1:29 pm, Gal Aviel <> wrote:
    > Hello All,
    >
    > I want to add a logger to my application, then addHandler to it to log to a
    > special destination.
    >
    > Unfortunately when I use logging.getLogger("my_logger") to create the new
    > logger, it apparently comes with a default handler that logs to STDOUT (or
    > STDERR?). When I add my special handler it works Ok, but still logs to terminal,
    > which I do not want.
    >


    from docs:
    ------------------
    getLogger( [name])

    Return a logger with the specified name or, if no name is specified,
    return a logger which is the root logger of the hierarchy.
    ------------------

    so you should do 'root = getlogger()', then append your own logger to
    the root.

    Gerard
    Gerard Flanagan, Mar 4, 2008
    #2
    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. sumit
    Replies:
    1
    Views:
    6,438
    Anatoly
    Nov 25, 2003
  2. Replies:
    5
    Views:
    2,290
    Andrey Kuznetsov
    Apr 5, 2006
  3. wooks
    Replies:
    3
    Views:
    518
    wooks
    Jul 22, 2004
  4. sri2097
    Replies:
    4
    Views:
    564
    sri2097
    Jan 10, 2006
  5. Replies:
    2
    Views:
    542
    Andy Dingley
    Dec 6, 2006
Loading...

Share This Page