python - 我如何记录 IPython 的输出,而不是在每次加载时都显示丑陋的 7 行日志记录信息?

标签 python logging installation ipython dotfiles

我的目标是调用 ipython,同时将所有输入/输出记录到 IPython,然后看到如下内容:

stu@stumac ~  $ ipython

In [1]: exit
stu@stumac ~  $

如果我设置,横幅可以很容易地移除

c.TerminalIPythonApp.display_banner = False

在我的 ~/.ipython/profile-default/ipython_config.py 文件。

但是我如何在记录日志的同时清理启动项呢?

在全新安装时,如果我在没有参数的情况下启动 IPython,我会看到:

sente@og ~ $ ipython
Python 2.7.3 (default, Jun 20 2013, 12:50:58)
Type "copyright", "credits" or "license" for more information.

IPython 0.13.2 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.

In [1]: exit
sente@og ~ $

如果我在调用 IPython 时传递一个 logfile=logfile.txt 参数,我会看到:

sente@og ~ $ ipython --logfile=logfile.txt
Activating auto-logging. Current session state plus future input saved.
Filename       : logfile.txt
Mode           : backup
Output logging : False
Raw input log  : False
Timestamping   : False
State          : active
Python 2.7.3 (default, Jun 20 2013, 12:50:58)
Type "copyright", "credits" or "license" for more information.

IPython 0.13.2 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.

In [1]: exit
sente@og ~ $

如何在不给我的终端增加额外的困惑的情况下使用日志记录:

Activating auto-logging. Current session state plus future input saved.
Filename       : logfile.txt
Mode           : backup
Output logging : False
Raw input log  : False
Timestamping   : False
State          : active

在其他机器上,我将 IPython 配置为通过 .ipython/profile_default/startup/01-log-everything.py 自动记录内容,其中包含以下行:

from time import strftime
import os.path

ip = get_ipython()

ldir = ip.profile_dir.log_dir
fname = strftime('%Y-%m-%d-%H-%M-%S') + ".py"
filename = os.path.join(ldir, fname)

ip.run_line_magic('logstart', '-o %s append' % filename)

这会导致与我添加 --logfile=logfile.txt

时相同的困惑

任何有关如何正确执行此操作的帮助将不胜感激。如果没有别的,我可以重定向 sys.stdout,配置日志记录,然后重置 sys.stdout,但我希望有一个不那么骇人听闻的解决方案。

最佳答案

要在没有横幅的情况下登录,请使用 --no-banner 标志。

ipython --no-banner --logfile='logfile.txt'

这可以在带有 --help-all 标志的帮助文件中找到。

ipython --help-all

关于python - 我如何记录 IPython 的输出,而不是在每次加载时都显示丑陋的 7 行日志记录信息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17342018/

相关文章:

powershell - 如何在 AppVeyor 上安装具有 GUI 安装程序的软件?

python - 用于 json 值的 Prometheus python 导出器

python - 在Python中使用插值统一ct扫描体素大小

javascript - 动态数据更新,无需刷新或重新加载

python - 在 BaseHTTPServer 中启动守护进程

ruby - 在 ruby​​ 中仅记录一次特定事件

java - OpenJPA 登录到 Websphere 中的单独文件

java - logback-spring.xml 不会附加文件名

python - distutils.core.setup 控制台脚本入口点?

installation - 有人在 mac os x lion 上成功安装了 openfire 或任何 xmpp 服务器吗?