Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No f-string in logging.basicConfig() #85948

Closed
shubham25powertech mannequin opened this issue Sep 14, 2020 · 4 comments
Closed

No f-string in logging.basicConfig() #85948

shubham25powertech mannequin opened this issue Sep 14, 2020 · 4 comments
Labels
3.8 only security fixes type-feature A feature request or enhancement

Comments

@shubham25powertech
Copy link
Mannequin

shubham25powertech mannequin commented Sep 14, 2020

BPO 41782
Nosy @vsajip, @ericvsmith

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = <Date 2020-09-14.15:09:14.722>
created_at = <Date 2020-09-14.13:49:54.225>
labels = ['invalid', 'type-feature', '3.8']
title = 'No f-string in logging.basicConfig()'
updated_at = <Date 2020-09-14.15:17:44.380>
user = 'https://bugs.python.org/shubham25powertech'

bugs.python.org fields:

activity = <Date 2020-09-14.15:17:44.380>
actor = 'eric.smith'
assignee = 'none'
closed = True
closed_date = <Date 2020-09-14.15:09:14.722>
closer = 'vinay.sajip'
components = []
creation = <Date 2020-09-14.13:49:54.225>
creator = 'shubham.25powertech'
dependencies = []
files = []
hgrepos = []
issue_num = 41782
keywords = []
message_count = 4.0
messages = ['376878', '376879', '376880', '376881']
nosy_count = 3.0
nosy_names = ['vinay.sajip', 'eric.smith', 'shubham.25powertech']
pr_nums = []
priority = 'normal'
resolution = 'not a bug'
stage = 'resolved'
status = 'closed'
superseder = None
type = 'enhancement'
url = 'https://bugs.python.org/issue41782'
versions = ['Python 3.8']

@shubham25powertech
Copy link
Mannequin Author

shubham25powertech mannequin commented Sep 14, 2020

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.

@shubham25powertech shubham25powertech mannequin added 3.8 only security fixes type-feature A feature request or enhancement labels Sep 14, 2020
@ericvsmith
Copy link
Member

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.

@vsajip
Copy link
Member

vsajip commented Sep 14, 2020

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.

@ericvsmith
Copy link
Member

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.

@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.8 only security fixes type-feature A feature request or enhancement
Projects
None yet
Development

No branches or pull requests

2 participants