Title: No f-string in logging.basicConfig()
Type: enhancement Stage: resolved
Components: Versions: Python 3.8
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: eric.smith, shubham.25powertech, vinay.sajip
Priority: normal Keywords:

Created on 2020-09-14 13:49 by shubham.25powertech, last changed 2020-09-14 15:17 by eric.smith. This issue is now closed.

Messages (4)
msg376878 - (view) Author: Shubham Singh (shubham.25powertech) Date: 2020-09-14 13:49
There is no method to implement format strings in the basicConfig() method of the logging module. There are the implementations of '%', '{' or '$' for printf-style, str.format() or string.Template respectively using the style keyword.
msg376879 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2020-09-14 13:52
Since an f-string just evaluates to a regular string at the call site, before any logging code is invoked, there's nothing for the logging code to do. As far as it's concerned, it just gets a regular string.
msg376880 - (view) Author: Vinay Sajip (vinay.sajip) * (Python committer) Date: 2020-09-14 15:09
It's as Eric said. So I'll close this issue as "not a bug". An additional point - by using f-strings, you may be doing unnecessary string formatting - e.g. if the event doesn't actually get handled because of logger or handler level or filter settings. It's worse if any of the parameters in the f-string are expensive to compute, as they would effectively be thrown away if the event were not to be handled.
msg376881 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2020-09-14 15:17
Vinay raises a good point about performance that's well worth being aware of, especially with expensive objects (as he says). But since f-strings are much faster than other formatting (and especially .format()), there's a tradeoff. If I have something that I know is likely to be logged, sometimes I'll format it with f-strings.
Date User Action Args
2020-09-14 15:17:44eric.smithsetmessages: + msg376881
2020-09-14 15:09:14vinay.sajipsetstatus: open -> closed
resolution: not a bug
messages: + msg376880

stage: resolved
2020-09-14 13:59:40xtreaksetnosy: + vinay.sajip
2020-09-14 13:52:38eric.smithsetnosy: + eric.smith
messages: + msg376879
2020-09-14 13:49:54shubham.25powertechcreate