Reputation: 22103
I am learning to utilize logging
rather than print
to debug my code:
In [89]: logging.basicConfig(level=logging.DEBUG, format=" %(asctime)s - %(levelname)s - %(message)s")
In [90]: logging.debug("Some debugging details.")
2018-08-28 16:41:15,371 - DEBUG - Some debugging details.
I tried to rewrite the format as literal format,
In [5]: logging.basicConfig(level=logging.DEBUG, format=f" {(asctime)} - {(levelname)} - {(message)}")
NameError: name 'asctime' is not defined
or
In [5]: logging.basicConfig(level=logging.DEBUG, format=f" {(asctime)} - {(levelname)} - {(message)}")
-------------------------------------------------------------------------
In [6]: logging.basicConfig(level=logging.DEBUG, format=f" {(asctime)s} - {(levelname)s} - {(message)s}")
File "<fstring>", line 1
((asctime)s)
^
SyntaxError: invalid syntax
Is it possible to write a literal format of format=" %(asctime)s - %(levelname)s - %(message)s"
?
Upvotes: 1
Views: 3415
Reputation: 106792
No it isn't possible because the placeholder names in the logging format are interpreted by the logging
module, while the expressions in the f-string are interpreted by the Python compiler itself, which is unaware of the meaning of the placeholder names understood only by the logging
module.
Upvotes: 1