2

Python has two main string formatting options % and str.format. logging module has a lazy feature.

logging.debug('The value is %s', huge_arg)

This does not construct the string if the log line is not going to be print. However, this feature works only if the sting uses old style % format. Is there a way to use str.format with this lazy feature? There could be a named arg like:

logging.debug('The value is {}', fmt_arg=(huge_arg))

2 Answers 2

5

The only answer to your question is that - so far - nobody has volunteered to change the logging code to support the newer format feature. If you're volunteering, why not ask about it on the Python-Dev mailing list? I expect it's trickier than you realize (e.g., there may be calls to logging functions already that happen to pass a fmt_arg keyword argument). Good luck ;-)

Sign up to request clarification or add additional context in comments.

Comments

4

I'm a little scared to contradict the TimBot, but I have another answer :-)

The logging module uses % formatting because it pre-dates the appearance of {}-formatting in Python (logging appeared in 2.3, str.format in 2.6). Logging has not been converted over to {}-formatting because:

  1. You can't just switch over without breaking a lot of existing code in third party libraries and applications, so %-formatting is here to stay.
  2. When {}-formatting arrived, it was a bit slower that %-formatting (AFAIK, it still is, though it offers more control over output), and people regard logging as an overhead as it is, never mind adding to that overhead ;-)
  3. There is already support for {}-formatting and even $-style formatting (string.Template), as described in this post from 2010. The approach described supports logging's lazy formatting.

4 Comments

Why would it break existing code if it just introduces an extra keyword argument(unless some one uses a custom class)? {} being slow makes more appropriate to have lazy logging. Thanks for the link.
@balki perhaps it wouldn't, but you can never tell what assumptions people make about your code. Anyway, it would certainly complicate the code paths and add code for very little functional benefit (given that you can already use {}-formatting with logging, as outlined in the post I linked to). And people have indicated that they want logging overhead to be as low as possible, even though lazy formatting has been there from day one.
@VinaySajip , is this answer still accurate to current logging library and what about f-strings?
@VietThan Yes it is, and you can use f-strings but of course then formatting won't be lazy. They will just look to logging like ordinary strings. Don't mix f-strings with any other formatting style, though - because it will be confusing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.