3

I read the Python Logging HOWTO, and there is a description I don't understand:

If you want to set the logging level from a command-line option such as:

--log=INFO

and you have the value of the parameter passed for --log in some variable loglevel, you can use:

getattr(logging, loglevel.upper())

to get the value which you’ll pass to basicConfig() via the level argument.

What does it mean? There is no example, it would be nice if someone could give one.

Can I set the logging level like this?

logging --log=INFO
mkrieger1
  • 14,486
  • 4
  • 43
  • 54
qin peter
  • 319
  • 2
  • 14

1 Answers1

6

The assumption is that you are using something like argparse to define command line arguments:

import argparse
p = argparse.ArgumentParser()
p.add_argument("--log")

args = p.parse_args()
loglevel = args.log

The argument to the --log option should then be one of the level constants defined in the logging module; getattr(logging, loglevel.upper()) would then be a way to get that value for a string.

chepner
  • 446,329
  • 63
  • 468
  • 610