[Python-Dev] [Python-checkins] r87202 - python/branches/py3k/Doc/library/logging.rst

Nick Coghlan ncoghlan at gmail.com
Mon Dec 13 04:45:50 CET 2010


On Mon, Dec 13, 2010 at 8:45 AM, vinay.sajip <python-checkins at python.org> wrote:
> +to get the value which you'll pass to :func:`basicConfig` via the *level*
> +argument. You may want to error check any user input value, perhaps as in the
> +following example::
> +
> +   # assuming loglevel is bound to the string value obtained from the
> +   # command line argument. Convert to upper case to allow the user to
> +   # specify --log=DEBUG or --log=debug
> +   numeric_level = getattr(logging, loglevel.upper(), None)
> +   assert numeric_level is not None, 'Invalid log level: %s' % loglevel
> +   logging.basicConfig(level=numeric_level, ...)

Minor nit - using asserts to check user input is generally a bad idea.
A more explicit check might be a better example:

if not isinstance(numeric_level, int):
   raise ValueError('Invalid log level: %s' % loglevel)

This also covers the case where someone does something weird like pass
in "basic_format" as a logging level.

Cheers,
Nick.

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


More information about the Python-Dev mailing list