How to completely reset Python stdlib logging module in ipython session?

I would like to repeat calls to Python scripts using% run in an ipython session, and for each of these scripts to register based on cmdline arguments passed through% run.

For example, during debugging of cmd.py, over time I want to run:

%run cmd.py ... logs with default behavior, eg to STDERR with root level WARN %run cmd.py --log_level DEBUG --log_file /tmp/cmd.out ... logs with root level DEBUG to a file %run cmd.py --log_level ERROR 

Unfortunately, this is complicated because the logging state created by logging.basicConfig is saved after the first% run command (this usually applies to all modules and is often desirable when using% run).

I understand that in complete generality, a number of% run commands, as mentioned above, will not be the same as starting each command in a new process. However, it would be very convenient if things like log_level and log_file could be reinitialized.

I tried something like this in cmd.py:

 import logging_config # parse logging config from sys.argv reload(logging_config) # re-parse cmdline if using %run multiple times 

and logging_config.py does (is compressed):

 if logging_initialized: logging.getLogger().setLevel(lvl) else: logging.basicConfig(level=lvl) logging_initialized = True 

It works for simple cases, but not if cmd.py imports libraries that also use logging. I also experimented with logging.shutdown () (called at the end of each cmd.py), but that doesn't seem to help.

+6
source share
1 answer

Do not use basicConfig () for this usage style - it is intended for a simple one-time configuration and, as the documentation says, subsequent calls after the first have no effect (it does just that if the root log has no handlers). It will be good when your script is run from the command line, but not from the interactive interpreter, IPython, IDLE, PythonWin, or another similar environment in which the process you are interacting with does not exit.

Instead, use the program configuration or fileConfig () or dictConfig () - they have modes where you can completely replace the existing logging configuration with a new one.

+4
source

Source: https://habr.com/ru/post/886076/


All Articles