multiple logger

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • peiyin.li@gmail.com

    #1

    multiple logger

    Hi,

    I want to have two loggers that logs to two different files. Here is
    what I have:

    import logging
    import logging.handler s

    project1Handler = logging.handler s.RotatingFileH andler( 'project1.log',
    maxBytes=1024)
    project1Handler .setLevel(loggi ng.INFO)
    formatter1 = logging.Formatt er('%(name)-12s: %(levelname)-8s
    %(message)s')
    project1Handler .setFormatter(f ormatter1)
    logging.getLogg er('project1'). addHandler(proj ect1Handler)
    logger1 = logging.getLogg er('project1')

    logger1.debug(' Quick zephyrs blow, vexing daft Jim.')
    logger1.info('H ow quickly daft jumping zebras vex.')
    logger1.warning ('Jail zesty vixen who grabbed pay from quack.')
    logger1.error(' The five boxing wizards jump quickly.')


    project2Handler = logging.handler s.RotatingFileH andler( 'project2.log',
    maxBytes=1024)
    project2Handler .setLevel(loggi ng.DEBUG)
    formatter2 = logging.Formatt er('%(name)-12s: %(levelname)-8s
    %(message)s')
    project2Handler .setFormatter(f ormatter2)
    logging.getLogg er('project2'). addHandler(proj ect2Handler)
    logger2 = logging.getLogg er('project2')

    logger2.debug(' Quick zephyrs blow, vexing daft Jim.')
    logger2.info('H ow quickly daft jumping zebras vex.')
    logger2.warning ('Jail zesty vixen who grabbed pay from quack.')
    logger2.error(' The five boxing wizards jump quickly.')


    However, I got this for project1.log:

    project1 : WARNING Jail zesty vixen who grabbed pay from quack.
    project1 : ERROR The five boxing wizards jump quickly.

    and this for project2.log:

    project2 : WARNING Jail zesty vixen who grabbed pay from quack.
    project2 : ERROR The five boxing wizards jump quickly.

    Where are the DEBUG and INFO messages go?

    I then add this at the begining of my code:

    logging.basicCo nfig(level=logg ing.DEBUG)

    Now it seems that both project1.log and project2.log has the correct
    output. But I also see all the log message going to the console, which
    I really don't want.

    What did I miss here? Do I have to set a handler for the root logger
    before I can set something for the child loggers? If yes, how do I make
    the root logger logs to nowhere?

    Thanks in advance!

    Albert

  • Vinay Sajip

    #2
    Re: multiple logger

    Just replace

    logging.basicCo nfig(level=logg ing.DEBUG)

    with

    logging.getLogg er().setLevel(l ogging.DEBUG)

    and you will no longer get messages written to the console. The
    basicConfig() method is meant for really basic use of logging - it
    allows one call to set level, and to add either a console handler a
    simple (non-rotating) file handler to the root logger. See the
    documentation for more information.

    Vinay Sajip

    Comment

    Working...