As I understand it,
'hello {0}'.format("world")
is slower than:
"hello %s" % "world"
My question is, how does logger format the strings when passed like so:
logger.debug("hello %s", "world")
And assuming the logs will never be turned off would it be better to do:
logger.debug("hello %s" % "world")
Don't forget that if you end up using a logging aggregation service such as Sentry, then all the calls to:
logger.error("hello %s", planet)
will be grouped together as multiple occurrences of the same error, while all the calls to:
logger.error("hello %s" % (planet,))
will be listed as many different errors, each of which happened once. This can make it hard to triage which errors are actually frequently occurring.
This grouping behaviour is based on the value of the first parameter to the logging call. In the first example, they are all identical, whereas in the second, they all depend on the value of 'planet'.
So it's important not to use the '%' or .format operators on the strings you pass to logging. Let the logging call do it for you.
logger.debug(msg, arg1, arg2)
does internally: msg = msg % (arg1, arg2)
as documented so no surprise here.
logger.debug(msg, arg1, arg2, ...)
might look tidier than logger.debug(msg % (arg1, arg2, ...))
and doesn't perform formatting prematurely.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With