[re-sending this since I forgot to CC the list]

> Unfortunately, there is no logging.loglevel attribute.

> At the moment, the only way of doing this is modifying the program, parsing input parameters and setting up the logging system.

Indeed, that's why I clarified in the follow-up message; sorry it's been a long day :-).

"loglevel" was simply an arbitrary name chosen for that particular example and getattr() was used as a way to prefix the module so it would access the logging level constant instead of the literal name of the level as a string (which would be invalid to pass to level). You'd likely use argparse or some other command-line argument parsing utility to get it in the first place. For example:


parser = argparse.ArgumentParser()
parser.add_argument("--log", dest='logging_level', default='WARNING')
args = parser.parse_args()

numeric_level = getattr(logging, args.logging_level.upper())
logging.basicConfig(level=numeric_level, ...)


(input validation omitted for simplification)

> If you want it only temporarily for debugging purposes, or permanently for all scripts in your system, it's not really feasible.

Hmm, I'm not sure that I like the idea of overriding the default logging level being set system-wide, or that there would be an especially strong use case for it. Instead, it seems more practically common to want to set it on an individual run of a program temporarily. In such cases, using some form of command-line parsing utility is a perfectly a viable option.

> We have debugging flags for warnings, faulthandlers, asyncio etc... Logging I would say is the most important and trivial of all, yet we don't have a flag for it.

Arguably though, the easier it is to implement on your own, the less of a need there is to be an environment variable to do it. Also, I think the logging configuration should be on a per-program basis, rather than globally (which is what env vars are typically used for).

FWIW I'm not opposed to the principle behind the idea, I'm just not convinced yet that it would be that much better than the options that are already available, and would be worthwhile to add an entirely new environment variable for (or be good usage of an env var for that matter). Perhaps there are other areas to explore for simplifying the process of changing the logging level from the command-line though.

Also, although not the same proposal, there was a discussion that brought up some similar points in a python-ideas thread back in 2014: https://mail.python.org/archives/list/python-ideas@python.org/thread/7ZLMSQU2C65YZ2KVZKZ6MICPTWVFZVJ3/#GCJZGNJOO27WUWKN2DKUP3D6HLYNLFUI. I think some of the points made in there are relevant to this discussion.

On Wed, Feb 19, 2020 at 10:26 PM Bar Harel <bzvi7919@gmail.com> wrote:
Thanks Kyle for the feedback :-)

Unfortunately, there is no logging.loglevel attribute.

At the moment, the only way of doing this is modifying the program, parsing input parameters and setting up the logging system.

If you want it only temporarily for debugging purposes, or permanently for all scripts in your system, it's not really feasible.

Think of -Wdefault. You can change settings in the warnings module and modify files each time you wish to enable all warnings. But then again, why would you?

We have debugging flags for warnings, faulthandlers, asyncio etc... Logging I would say is the most important and trivial of all, yet we don't have a flag for it.

On Thu, Feb 20, 2020, 4:24 AM Kyle Stanley <aeros167@gmail.com> wrote:
> This can already be done from the command line through --log=LEVEL, which sets the logging.loglevel attribute. In the how-to section of the docs, this is demonstrated in the following example:

Note: "loglevel" is an arbitrary name, but this can be implemented trivially with something like argparse to check for a --log argument.

On Wed, Feb 19, 2020 at 9:19 PM Kyle Stanley <aeros167@gmail.com> wrote:
> Setting PYTHONLOGGING to any log level or level name will initialize logging.basicConfig() with that appropriate level.

This can already be done from the command line through --log=LEVEL, which sets the logging.loglevel attribute. In the how-to section of the docs, this is demonstrated 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)
if not isinstance(numeric_level, int):
    raise ValueError('Invalid log level: %s' % loglevel)
logging.basicConfig(level=numeric_level, ...)

(https://docs.python.org/3/howto/logging.html#logging-to-a-file)
Is there a practical reason why the above doesn't work for you? Otherwise, I see no reason to add a new environment variable that effectively does the same thing.

On Wed, Feb 19, 2020 at 8:16 PM Bar Harel <bzvi7919@gmail.com> wrote:
Another idea I've had that may be of use:

PYTHONLOGGING environment variable.

Setting PYTHONLOGGING to any log level or level name will initialize logging.basicConfig() with that appropriate level.

Another option would be that -x dev or a different -x logging will initialize basic config.

Will be useful mostly for debugging purposes instead of temporarily modifying the code. 

Kinda surprised it doesn't exist tbh.

Bar Harel
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-leave@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/I74LVJWJLE2LUCCZGOF5A5JDSDHJ6WX2/
Code of Conduct: http://python.org/psf/codeofconduct/