[Python-Dev] Using logging in the stdlib and its unit tests

Nick Coghlan ncoghlan at gmail.com
Mon Dec 13 04:30:24 CET 2010


On Mon, Dec 13, 2010 at 11:22 AM, Robert Kern <robert.kern at gmail.com> wrote:
> level = getattr(logging, opt.logLevel)

While this is the approach I would recommend, it does have a couple of
downsides:

1. An upper() call is also needed to allow strings like "info" instead
of "INFO":
2. If an integer is available, it would be nice to return it
unmodified (or, if we ever get named values in the standard library,
convert it to that)
3. The asymmetry with "logging.getLevelName" grates a bit

So it would be far more obvious if there was a "logging.getLevel"
counterpart to "logging.getLevelName" that looked something like:

def getLevel(level):
  try:
    return operator.index(level) # Integers and equivalents
  except TypeError:
    pass
  try:
    key = level.upper()
  except Exception as ex:
    raise TypeError("Log level must be an integer or string") from ex
  return globals()[key]


> level = logging._levelNames[opt.logLevel]

That doesn't work (_levelNames maps from integers to strings, we want
the mapping from strings to integers and it is only the module globals
that provides that).

Regards,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia


More information about the Python-Dev mailing list