Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8382293
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T16:53:56+00:00 2026-06-09T16:53:56+00:00

Is it advantageous to call logging functions with format string + args list vs.

  • 0

Is it advantageous to call logging functions with format string + args list vs. formatting inline?

I’ve seen (and written) logging code that uses inline string formatting:

logging.warn("%s %s %s" % (arg1, arg2, arg3))

and yet I assume it’s better (performance-wise, and more idiomatic) to use:

logging.warn("%s %s %s", arg1, arg2, arg3)

because the second form avoids string formatting operations prior to invoking the logging function. If the current logging level would filter out the log message, no formatting is necessary, reducing computing time and memory allocations.

Am I on the right track here, or have I missed something?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T16:53:58+00:00Added an answer on June 9, 2026 at 4:53 pm

    IMHO, for messages that are very likely to be displayed, such as those given to error or warn it does not make much of a difference.

    For messages that are less likely displayed, I would definitely go for the second version, mainly for performance reasons. I often give large objects as a parameter to info, which implement a costly __str__ method. Clearly, sending this pre-formatted to info would be a performance waste.

    UPDATE

    I just checked the source code of the logging module and, indeed, formatting is done after checking the log level. For example:

    class Logger(Filterer):
        # snip
        def debug(self, msg, *args, **kwargs):
            # snip
            if self.isenabledfor(debug):
                self._log(debug, msg, args, **kwargs)
    

    One can observe that msg and args are untouched between calling log and checking the log level.

    UPDATE 2

    Spired by Levon, let me add some tests for objects that have a costly __str__ method:

    $ python -m timeit -n 1000000 -s "import logging" -s "logger = logging.getLogger('foo')" -s "logger.setLevel(logging.ERROR)" "logger.warn('%s', range(0,100))"
    1000000 loops, best of 3: 1.52 usec per loop
    $ python -m timeit -n 1000000 -s "import logging" -s "logger = logging.getLogger('foo')" -s "logger.setLevel(logging.ERROR)" "logger.warn('%s' % range(0,100))"
    1000000 loops, best of 3: 10.4 usec per loop
    

    In practice, this could give a fairly high performance boost.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In some programming API's I see a list of methods to call, like getBoolean(String
I understand that in jQuery, it's advantageous to be more specific when using selectors
I have written some physics simulation code in C++ and parsing the input text
The second ReferenceEquals call returns false. Why isn't the string in s4 interned? (I
Call me crazy, but I'm the type of guy that likes constructors with parameters
I have a php application that has about 50-55 code files. The file that
A lot of the code base methods my peers have written perform their own
I'm working with some C code that does not contain function prototypes for a
I work on a web app that uses some Spring Application Event publishing and
I want to perform a sort operation on some field. Is it advantageous making

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.