如何在ipython会话中完全重置Python stdlib日志记录模块? [英] How to completely reset Python stdlib logging module in an ipython session?

查看:152
本文介绍了如何在ipython会话中完全重置Python stdlib日志记录模块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在ipython会话中使用%run重复调用Python脚本,并根据通过%run传递的cmdline参数来记录每个脚本。

I'd like to make repeated calls to Python scripts using %run in an ipython session, and for each of those scripts to log based on cmdline arguments passed via %run.

例如在调试cmd.py时我可能会想要运行:

For example while debugging cmd.py I might over time want to run:

%run cmd.py
... logs with default behavior, e.g. 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

不幸的是这很难,因为记录在第一个%run命令之后,logging.basicConfig创建的状态仍然存在(对于所有模块更为普遍,并且在使用%run时通常也是如此)。

Unfortunately this is difficult because the logging state created by logging.basicConfig persists after the first %run command (as is more generally true of all modules, and often desirable when using %run).

我意识到完全通用的一系列%run命令与在新进程中运行每个命令不同。但是,如果可以重新初始化log_level和log_file之类的东西会非常方便。

I realize that in full generality a series of %run commands like above will not be the same as running each command in a new process. However, it would be very convenient if things like the log_level and log_file could be re-initialized.

我在cmd.py:

I've 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

和logging_config.py确实(压缩):

and logging_config.py does (condensed):

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

它适用于简单情况,但如果cmd.py导入也使用日志记录的库则不行。我也尝试过logging.shutdown()(在每个cmd.py结束时调用),但这似乎没有帮助。

It works for simple cases but not if cmd.py imports libraries that also use logging. I've also experimented with logging.shutdown() (called at conclusion of each cmd.py) but that does not seem to help.

推荐答案

不要将 basicConfig()用于这种使用方式 - 它用于简单的一次性配置,并且如文档所述,后面的调用在第一次调用后没有效果(仅限于如果根记录器没有处理程序,则执行任何操作)。当您的脚本从shell提示符运行时,这将是正常的,但不是来自交互式解释器,IPython,IDLE,PythonWin或其他与您交互的进程不会退出的类似环境。

Don't use basicConfig() for this style of usage - it's meant for simple one-off configuration and, as the documentation says, subsequent calls after the first have no effect (it only does anything if the root logger has no handlers). This will be fine when your script is run from the shell prompt, but not from the interactive interpreter, IPython, IDLE, PythonWin, or other similar environment where the process you're interacting with doesn't exit.

相反,使用编程配置,或 fileConfig() dictConfig() - 这些模式完全替换现有的日志记录配置一个新的。

Instead, use programmatic configuration, or fileConfig() or dictConfig() - these have modes where the completely replace the existing logging configuration with a new one.

这篇关于如何在ipython会话中完全重置Python stdlib日志记录模块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆