Logging module, a few questions

Discussion in 'Python' started by Irmen de Jong, May 8, 2004.

  1. Hello
    I'm using the logging module in Python 2.3.3, with a format string
    containing %(asctime). But it now dumps a full date +timestamp in
    the log, which is nice but sometimes I only want the timestamp
    (no date). Is there an easy way to change this? How can I make
    %(asctime) dump only the time?

    I'm using a configuration file to set up the logging.

    Which brings me to another thing.
    In java's log4j, it's easy to "silence" or "enable" specific
    parts of a logging hierarchy, by doing:

    log4j.category.nl.company = DEBUG
    log4j.category.nl.company.lib = WARN
    log4j.category.org.apache = WARN

    and so on.

    I see no easy way of doing the same for Python's logging module;
    it seems that I have to create a handler for each of the different
    parts of the hierarchy of which I want to set the loglevel.
    Am I missing something?


    Thanks!

    --Irmen de Jong
    Irmen de Jong, May 8, 2004
    #1
    1. Advertising

  2. Irmen de Jong

    Peter Otten Guest

    Irmen de Jong wrote:

    > I'm using the logging module in Python 2.3.3, with a format string
    > containing %(asctime). But it now dumps a full date +timestamp in
    > the log, which is nice but sometimes I only want the timestamp
    > (no date). Is there an easy way to change this? How can I make
    > %(asctime) dump only the time?


    >>> import logging as lg
    >>> logger = lg.getLogger("sample")
    >>> hd = lg.StreamHandler()
    >>> fm = lg.Formatter("%(asctime)s %(message)s", "%H:%M:%S")


    > I'm using a configuration file to set up the logging.


    I think in a config file that would be

    [formatter_xxx]
    format=%(asctime)s %(message)s
    datefmt=%H:%M:%S

    > Which brings me to another thing.
    > In java's log4j, it's easy to "silence" or "enable" specific
    > parts of a logging hierarchy, by doing:
    >
    > log4j.category.nl.company = DEBUG
    > log4j.category.nl.company.lib = WARN
    > log4j.category.org.apache = WARN
    >
    > and so on.
    >
    > I see no easy way of doing the same for Python's logging module;
    > it seems that I have to create a handler for each of the different
    > parts of the hierarchy of which I want to set the loglevel.
    > Am I missing something?


    >>> hd.setFormatter(fm)
    >>> root = lg.getLogger()
    >>> root.setLevel(lg.WARNING)
    >>> root.addHandler(hd)
    >>> logger.warn("so what")

    10:15:59 so what
    >>> logger.info("so what")
    >>> logger.setLevel(lg.INFO)
    >>> logger.info("so what")

    10:16:50 so what
    >>> root.info("so what")


    But does it apply to the whole branch?

    >>> sublogger = lg.getLogger("sample.sub")
    >>> sublogger.info("so what")

    10:26:11 so what
    >>> logger.setLevel(lg.WARNING)
    >>> sublogger.info("so what")
    >>>


    Peter
    Peter Otten, May 9, 2004
    #2
    1. Advertising

  3. Sorry to reply to myself, but I found the solution.

    > I'm using the logging module in Python 2.3.3, with a format string
    > containing %(asctime). But it now dumps a full date +timestamp in
    > the log, which is nice but sometimes I only want the timestamp
    > (no date). Is there an easy way to change this? How can I make
    > %(asctime) dump only the time?


    Never mind. I didn't read the manual very well. It tells me
    to add a "datefmt" to the formatter, which works like a charm.


    However, the other thing still stands:

    > I'm using a configuration file to set up the logging.
    >
    > Which brings me to another thing.
    > In java's log4j, it's easy to "silence" or "enable" specific
    > parts of a logging hierarchy, by doing:
    >
    > log4j.category.nl.company = DEBUG
    > log4j.category.nl.company.lib = WARN
    > log4j.category.org.apache = WARN
    >
    > and so on.
    >
    > I see no easy way of doing the same for Python's logging module;
    > it seems that I have to create a handler for each of the different
    > parts of the hierarchy of which I want to set the loglevel.
    > Am I missing something?


    Anybody got a good idea about this? Thanks!


    --Irmen.
    Irmen de Jong, May 11, 2004
    #3
  4. Irmen de Jong

    Vinay Sajip Guest

    Irmen de Jong <> wrote in message news:<40a134f7$0$64453$4all.nl>...
    > > I'm using a configuration file to set up the logging.
    > >
    > > Which brings me to another thing.
    > > In java's log4j, it's easy to "silence" or "enable" specific
    > > parts of a logging hierarchy, by doing:
    > >
    > > log4j.category.nl.company = DEBUG
    > > log4j.category.nl.company.lib = WARN
    > > log4j.category.org.apache = WARN
    > >
    > > and so on.
    > >
    > > I see no easy way of doing the same for Python's logging module;
    > > it seems that I have to create a handler for each of the different
    > > parts of the hierarchy of which I want to set the loglevel.
    > > Am I missing something?

    >


    Since you are using a configuration file, you can set up config
    entries for each logger for which you want to configure a level. For
    example:

    [loggers]
    keys=root,log02,log03,log04

    [logger_log02]
    level=DEBUG
    propagate=1
    qualname=nl.company

    [logger_log03]
    level=WARN
    propagate=1
    qualname=nl.company.lib

    [logger_log04]
    level=WARN
    propagate=1
    qualname=org.apache

    Regards,


    Vinay Sajip
    Vinay Sajip, Jun 3, 2004
    #4
    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. rh0dium
    Replies:
    2
    Views:
    332
    rh0dium
    Aug 4, 2005
  2. Christoph Haas
    Replies:
    0
    Views:
    429
    Christoph Haas
    Jun 12, 2006
  3. Christoph Haas
    Replies:
    1
    Views:
    435
    Vinay Sajip
    Jun 14, 2006
  4. Replies:
    10
    Views:
    524
  5. Murali
    Replies:
    2
    Views:
    532
    Jerry Coffin
    Mar 9, 2006
Loading...

Share This Page