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

D

davy zhang

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")
 
D

Diez B. Roggisch

davy said:
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
 
S

Steve Holden

Diez said:
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
 
V

Vinay Sajip

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
 
B

bieffe62

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

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
 
D

Diez B. Roggisch

Steve said:
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
 
S

Steve Holden

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
 
T

Tim Roberts

Steve Holden said:
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.
 
V

Vinay Sajip

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top