Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > how to use logging module to log an object like print()

Reply
Thread Tools

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

 
 
davy zhang
Guest
Posts: n/a
 
      10-29-2008
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")
 
Reply With Quote
 
 
 
 
Diez B. Roggisch
Guest
Posts: n/a
 
      10-29-2008
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
 
Reply With Quote
 
 
 
 
davy zhang
Guest
Posts: n/a
 
      10-29-2008
thanks so much , I ganna check the formatter str for more info

On Wed, Oct 29, 2008 at 5:10 PM, Diez B. Roggisch <(E-Mail Removed)> 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
>

 
Reply With Quote
 
Steve Holden
Guest
Posts: n/a
 
      10-29-2008
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/

 
Reply With Quote
 
Vinay Sajip
Guest
Posts: n/a
 
      10-29-2008
On Oct 29, 11:24 am, Steve Holden <(E-Mail Removed)> 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
 
Reply With Quote
 
bieffe62@gmail.com
Guest
Posts: n/a
 
      10-29-2008
On 29 Ott, 12:24, Steve Holden <(E-Mail Removed)> 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
 
Reply With Quote
 
Diez B. Roggisch
Guest
Posts: n/a
 
      10-29-2008
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
 
Reply With Quote
 
Steve Holden
Guest
Posts: n/a
 
      10-30-2008
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> On 29 Ott, 12:24, Steve Holden <(E-Mail Removed)> 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/

 
Reply With Quote
 
Steve Holden
Guest
Posts: n/a
 
      10-30-2008
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/

 
Reply With Quote
 
Tim Roberts
Guest
Posts: n/a
 
      10-31-2008
Steve Holden <(E-Mail Removed)> 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, (E-Mail Removed)
Providenza & Boekelheide, Inc.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Logging exception with java.util.logging.Logger#log() to file only Royan Java 1 04-01-2008 09:42 PM
Re: Logging to a file and closing it again properly (logging module) Christoph Haas Python 1 06-14-2006 08:47 AM
Logging to a file and closing it again properly (logging module) Christoph Haas Python 0 06-12-2006 09:58 PM
Passing a log handle to a module? Help needed with logging module and rh0dium Python 2 08-04-2005 02:27 PM



Advertisments