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

getLevelName is missing a part of the behavior #51

Closed
alexprengere opened this issue Jul 29, 2022 · 3 comments · Fixed by #52
Closed

getLevelName is missing a part of the behavior #51

alexprengere opened this issue Jul 29, 2022 · 3 comments · Fixed by #52

Comments

@alexprengere
Copy link
Contributor

logging.getLevelName works as a two way street between level and level names:

>>> import logging
>>> logging.getLevelName(logging.WARNING)
'WARNING'
>>> logging.getLevelName("WARNING")
30

In picologging, the second way raises a TypeError:

>>> logging.getLevelName("WARNING")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: level must be an integer
@aminalaee
Copy link
Contributor

I agree that for the sake of backwards compatibility this should be modified,
but is there a specific use-case for it or just to be on par with logging?
It's really a weird behaviour.

@alexprengere
Copy link
Contributor Author

I have used it in a private codebase to convert a logging level as a str to the corresponding int, so roughly:

import logging
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("logging-level", default="DEBUG")
...
args = parser.parse_args()

level = logging.getLevelName(args.logging_level)
logging.basicConfig(..., level=level)

There are other ways to implement this (with their own problems..):

  • level = getattr(logging, args.logging_level)
  • level = logging._nameToLevel[args.logging_level]

I totally agree that this behavior of logging.getLevelName is out of place, and should be under its own function logging.getLevelFromName or so. The docs mention it too:

Changed in version 3.4: In Python versions earlier than 3.4, this function could also be passed a text level, and would return the corresponding numeric value of the level. This undocumented behaviour was considered a mistake, and was removed in Python 3.4, but reinstated in 3.4.2 due to retain backward compatibility.

@aminalaee
Copy link
Contributor

Yeah I agree, seems like it shiuld be just kept for backwards conpatibility.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants