how to use logging module to log an object like print()

Discussion in 'Python' started by davy zhang, Oct 29, 2008.

  1. davy zhang

    davy zhang Guest

    mport logging
    import pickle


    # create logger
    logger = logging.getLogger("simple_example")
    logger.setLevel(logging.DEBUG)
    # create console handler and set level to debug
    ch = logging.StreamHandler()
    ch.setLevel(logging.DEBUG)
    # create formatter
    formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s
    - %(message)s ")
    # add formatter to ch
    ch.setFormatter(formatter)
    # add ch to logger
    logger.addHandler(ch)

    d = {'key':'msg','key2':'msg2'}

    # "application" code
    logger.debug("debug message",d)#can not do this
    logger.info("info message")
    logger.warn("warn message")
    logger.error("error message")
    logger.critical("critical message")
    davy zhang, Oct 29, 2008
    #1
    1. Advertising

  2. davy zhang schrieb:
    > mport logging
    > import pickle
    >
    >
    > # create logger
    > logger = logging.getLogger("simple_example")
    > logger.setLevel(logging.DEBUG)
    > # create console handler and set level to debug
    > ch = logging.StreamHandler()
    > ch.setLevel(logging.DEBUG)
    > # create formatter
    > formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s
    > - %(message)s ")
    > # add formatter to ch
    > ch.setFormatter(formatter)
    > # add ch to logger
    > logger.addHandler(ch)
    >
    > d = {'key':'msg','key2':'msg2'}
    >
    > # "application" code
    > logger.debug("debug message",d)#can not do this


    logger.debug("yes you can: %r", d)


    Diez
    Diez B. Roggisch, Oct 29, 2008
    #2
    1. Advertising

  3. davy zhang

    davy zhang Guest

    thanks so much , I ganna check the formatter str for more info:)

    On Wed, Oct 29, 2008 at 5:10 PM, Diez B. Roggisch <> wrote:
    > davy zhang schrieb:
    >>
    >> mport logging
    >> import pickle
    >>
    >>
    >> # create logger
    >> logger = logging.getLogger("simple_example")
    >> logger.setLevel(logging.DEBUG)
    >> # create console handler and set level to debug
    >> ch = logging.StreamHandler()
    >> ch.setLevel(logging.DEBUG)
    >> # create formatter
    >> formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s
    >> - %(message)s ")
    >> # add formatter to ch
    >> ch.setFormatter(formatter)
    >> # add ch to logger
    >> logger.addHandler(ch)
    >>
    >> d = {'key':'msg','key2':'msg2'}
    >>
    >> # "application" code
    >> logger.debug("debug message",d)#can not do this

    >
    > logger.debug("yes you can: %r", d)
    >
    >
    > Diez
    > --
    > http://mail.python.org/mailman/listinfo/python-list
    >
    davy zhang, Oct 29, 2008
    #3
  4. davy zhang

    Steve Holden Guest

    Diez B. Roggisch wrote:
    > davy zhang schrieb:
    >> mport logging
    >> import pickle
    >>
    >>
    >> # create logger
    >> logger = logging.getLogger("simple_example")
    >> logger.setLevel(logging.DEBUG)
    >> # create console handler and set level to debug
    >> ch = logging.StreamHandler()
    >> ch.setLevel(logging.DEBUG)
    >> # create formatter
    >> formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s
    >> - %(message)s ")
    >> # add formatter to ch
    >> ch.setFormatter(formatter)
    >> # add ch to logger
    >> logger.addHandler(ch)
    >>
    >> d = {'key':'msg','key2':'msg2'}
    >>
    >> # "application" code
    >> logger.debug("debug message",d)#can not do this

    >
    > logger.debug("yes you can: %r", d)
    >

    One deficiency of this approach, however, is that the string formatting
    is performed even when no logging is required, thereby wasting a certain
    amount of effort on unnecessary formatting.

    regards
    Steve
    --
    Steve Holden +1 571 484 6266 +1 800 494 3119
    Holden Web LLC http://www.holdenweb.com/
    Steve Holden, Oct 29, 2008
    #4
  5. davy zhang

    Vinay Sajip Guest

    On Oct 29, 11:24 am, Steve Holden <> wrote:

    >
    > One deficiency of this approach, however, is that the string formatting
    > is performed even when nologgingis required, thereby wasting a certain
    > amount of effort on unnecessary formatting.
    >


    Though you can mitigate this using the pattern:

    if logger.isEnabledFor(logging.DEBUG):
    logger.debug("Message with variable data which may be expensive:
    %s", expensive_call())

    Which will only make the expensive_call() and formatting when the
    logging call will actually do something.

    Regards,

    Vinay Sajip
    Vinay Sajip, Oct 29, 2008
    #5
  6. davy zhang

    Guest

    On 29 Ott, 12:24, Steve Holden <> wrote:
    > Diez B. Roggisch wrote:
    > > davy zhang schrieb:
    > >> mport logging
    > >> import pickle

    >
    > >> # create logger
    > >> logger = logging.getLogger("simple_example")
    > >> logger.setLevel(logging.DEBUG)
    > >> # create console handler and set level to debug
    > >> ch = logging.StreamHandler()
    > >> ch.setLevel(logging.DEBUG)
    > >> # create formatter
    > >> formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s
    > >> - %(message)s ")
    > >> # add formatter to ch
    > >> ch.setFormatter(formatter)
    > >> # add ch to logger
    > >> logger.addHandler(ch)

    >
    > >> d = {'key':'msg','key2':'msg2'}

    >
    > >> # "application" code
    > >> logger.debug("debug message",d)#can not do this

    >
    > > logger.debug("yes you can: %r", d)

    >
    > One deficiency of this approach, however, is that the string formatting
    > is performed even when no logging is required, thereby wasting a certain
    > amount of effort on unnecessary formatting.
    >
    > regards
    >  Steve
    > --
    > Steve Holden        +1 571 484 6266   +1 800 494 3119
    > Holden Web LLC              http://www.holdenweb.com/- Nascondi testo citato
    >


    Sure about that?

    This is the implementation of Logger.debug in
    the file : ..Python25\lib\logging\__init__.py

    def debug(self, msg, *args, **kwargs):
    """
    Log 'msg % args' with severity 'DEBUG'.

    To pass exception information, use the keyword argument
    exc_info with
    a true value, e.g.

    logger.debug("Houston, we have a %s", "thorny problem",
    exc_info=1)
    """
    if self.manager.disable >= DEBUG:
    return
    if DEBUG >= self.getEffectiveLevel():
    apply(self._log, (DEBUG, msg, args), kwargs)


    The other methods (info, warning, ...) are similar. It looks like
    the formatting is only done if actually used.

    Ciao
    -----
    FB
    , Oct 29, 2008
    #6
  7. Steve Holden schrieb:
    > Diez B. Roggisch wrote:
    >> davy zhang schrieb:
    >>> mport logging
    >>> import pickle
    >>>
    >>>
    >>> # create logger
    >>> logger = logging.getLogger("simple_example")
    >>> logger.setLevel(logging.DEBUG)
    >>> # create console handler and set level to debug
    >>> ch = logging.StreamHandler()
    >>> ch.setLevel(logging.DEBUG)
    >>> # create formatter
    >>> formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s
    >>> - %(message)s ")
    >>> # add formatter to ch
    >>> ch.setFormatter(formatter)
    >>> # add ch to logger
    >>> logger.addHandler(ch)
    >>>
    >>> d = {'key':'msg','key2':'msg2'}
    >>>
    >>> # "application" code
    >>> logger.debug("debug message",d)#can not do this

    >> logger.debug("yes you can: %r", d)
    >>

    > One deficiency of this approach, however, is that the string formatting
    > is performed even when no logging is required, thereby wasting a certain
    > amount of effort on unnecessary formatting.


    Nope, it's not. It only happens when the message is really logged.

    Diez
    Diez B. Roggisch, Oct 29, 2008
    #7
  8. davy zhang

    Steve Holden Guest

    wrote:
    > On 29 Ott, 12:24, Steve Holden <> wrote:
    >> Diez B. Roggisch wrote:
    >>> davy zhang schrieb:
    >>>> mport logging
    >>>> import pickle
    >>>> # create logger
    >>>> logger = logging.getLogger("simple_example")
    >>>> logger.setLevel(logging.DEBUG)
    >>>> # create console handler and set level to debug
    >>>> ch = logging.StreamHandler()
    >>>> ch.setLevel(logging.DEBUG)
    >>>> # create formatter
    >>>> formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s
    >>>> - %(message)s ")
    >>>> # add formatter to ch
    >>>> ch.setFormatter(formatter)
    >>>> # add ch to logger
    >>>> logger.addHandler(ch)
    >>>> d = {'key':'msg','key2':'msg2'}
    >>>> # "application" code
    >>>> logger.debug("debug message",d)#can not do this
    >>> logger.debug("yes you can: %r", d)

    >> One deficiency of this approach, however, is that the string formatting
    >> is performed even when no logging is required, thereby wasting a certain
    >> amount of effort on unnecessary formatting.
    >>
    >> regards
    >> Steve
    >> --
    >> Steve Holden +1 571 484 6266 +1 800 494 3119
    >> Holden Web LLC http://www.holdenweb.com/- Nascondi testo citato
    >>

    >
    > Sure about that?
    >
    > This is the implementation of Logger.debug in
    > the file : ..Python25\lib\logging\__init__.py
    >
    > def debug(self, msg, *args, **kwargs):
    > """
    > Log 'msg % args' with severity 'DEBUG'.
    >
    > To pass exception information, use the keyword argument
    > exc_info with
    > a true value, e.g.
    >
    > logger.debug("Houston, we have a %s", "thorny problem",
    > exc_info=1)
    > """
    > if self.manager.disable >= DEBUG:
    > return
    > if DEBUG >= self.getEffectiveLevel():
    > apply(self._log, (DEBUG, msg, args), kwargs)
    >
    >
    > The other methods (info, warning, ...) are similar. It looks like
    > the formatting is only done if actually used.
    >

    Pretty sure, and Vinay's reply convinced me I was correct. He should
    know, after all ...

    regards
    Steve
    --
    Steve Holden +1 571 484 6266 +1 800 494 3119
    Holden Web LLC http://www.holdenweb.com/
    Steve Holden, Oct 30, 2008
    #8
  9. davy zhang

    Steve Holden Guest

    Diez B. Roggisch wrote:
    > Steve Holden schrieb:
    >> Diez B. Roggisch wrote:
    >>> davy zhang schrieb:
    >>>> mport logging
    >>>> import pickle
    >>>>
    >>>>
    >>>> # create logger
    >>>> logger = logging.getLogger("simple_example")
    >>>> logger.setLevel(logging.DEBUG)
    >>>> # create console handler and set level to debug
    >>>> ch = logging.StreamHandler()
    >>>> ch.setLevel(logging.DEBUG)
    >>>> # create formatter
    >>>> formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s
    >>>> - %(message)s ")
    >>>> # add formatter to ch
    >>>> ch.setFormatter(formatter)
    >>>> # add ch to logger
    >>>> logger.addHandler(ch)
    >>>>
    >>>> d = {'key':'msg','key2':'msg2'}
    >>>>
    >>>> # "application" code
    >>>> logger.debug("debug message",d)#can not do this
    >>> logger.debug("yes you can: %r", d)
    >>>

    >> One deficiency of this approach, however, is that the string formatting
    >> is performed even when no logging is required, thereby wasting a certain
    >> amount of effort on unnecessary formatting.

    >
    > Nope, it's not. It only happens when the message is really logged.
    >

    Vinay, please tell me whether I was right or wrong ...

    regards
    Steve
    --
    Steve Holden +1 571 484 6266 +1 800 494 3119
    Holden Web LLC http://www.holdenweb.com/
    Steve Holden, Oct 30, 2008
    #9
  10. davy zhang

    Tim Roberts Guest

    Steve Holden <> wrote:
    >Diez B. Roggisch wrote:
    >> Steve Holden schrieb:
    >>> Diez B. Roggisch wrote:
    >>>> davy zhang schrieb:
    >>>>> logger.debug("debug message",d)#can not do this
    >>>>
    >>>> logger.debug("yes you can: %r", d)
    >>>>
    >>> One deficiency of this approach, however, is that the string formatting
    >>> is performed even when no logging is required, thereby wasting a certain
    >>> amount of effort on unnecessary formatting.

    >>
    >> Nope, it's not. It only happens when the message is really logged.
    >>

    >Vinay, please tell me whether I was right or wrong ...


    If you will forgive me for putting words into your mouth, Steve, I suspect
    that you originally read the line as this:
    logger.debug("yes you can: %r" % d)
    instead of what was actually written:
    logger.debug("yes you can: %r" , d)

    With the %, of course the formatting will be done whether or not the string
    is logged. With the comma, the string formatting is done inside the logger
    module, and will be skipped if the message is not needed.

    This is exactly why logger supports this somewhat unusual feature. Now,
    one can certainly imagine circumstances where the parameter evaluation is
    expensive, so Vinay's bypass is still useful in some circumstances.
    --
    Tim Roberts,
    Providenza & Boekelheide, Inc.
    Tim Roberts, Oct 31, 2008
    #10
  11. davy zhang

    Vinay Sajip Guest

    On Oct 30, 4:34 am, Steve Holden <> wrote:
    >
    > Vinay, please tell me whether I was right or wrong ...
    >


    What Tim Roberts has already said is right ... my post was
    highlighting how to mitigate any overhead which is typically (or at
    least in general terms) higher than the cost of formatting, viz. the
    computation of the values which get formatted.

    Regards,

    Vinay Sajip
    Vinay Sajip, Oct 31, 2008
    #11
    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. David Konerding
    Replies:
    0
    Views:
    409
    David Konerding
    Sep 10, 2003
  2. rh0dium
    Replies:
    2
    Views:
    334
    rh0dium
    Aug 4, 2005
  3. Replies:
    2
    Views:
    437
    Joel Hedlund
    May 11, 2006
  4. fuzzylollipop
    Replies:
    1
    Views:
    368
  5. Royan
    Replies:
    1
    Views:
    1,415
    Andrea Francia
    Apr 1, 2008
Loading...

Share This Page