yakshaveinc / python

Shaving Python Yaks
https://yakshaveinc.github.io/python/
The Unlicense
0 stars 0 forks source link

Lowercase log levels for Python #4

Open abitrolly opened 4 years ago

abitrolly commented 4 years ago

The task is to make Python logging logger.setLevel() accept lowercase string. It already accepts uppercase 'DEBUG' string.

import logging

logger = logging.getLogger(__name__)
handler = logging.StreamHandler()
logger.addHandler(handler)

logger.setLevel('DEBUG')
logger.debug('!!!')

Modifications need to be done to _checkLevel function.

https://github.com/python/cpython/blob/8c0f0016e2d39f86589187db75012238c4e760e9/Lib/logging/__init__.py#L189-L198

def _checkLevel(level):
    if isinstance(level, int):
        rv = level
    elif str(level) == level:
        if level not in _nameToLevel:
            raise ValueError("Unknown level: %r" % level)
        rv = _nameToLevel[level]
    else:
        raise TypeError("Level not an integer or a valid string: %r" % level)
    return rv
abitrolly commented 4 years ago

Docs: https://docs.python.org/3/library/logging.html#logging.Logger.setLevel