登录IPython的输出?(Log IPython output?)

2019-06-26 19:55发布

有没有什么办法让IPython中的日志记录功能,包括输出和输入?

这是一个日志文件看起来像目前:

#!/usr/bin/env python 
# 2012-08-06.py 
# IPython automatic logging file
# 12:02 
# =================================
print "test"

我想有一个多行显示:

#!/usr/bin/env python 
# 2012-08-06.py 
# IPython automatic logging file
# 12:02 
# =================================
print "test"
# test

(在#是因为我认为是需要防止断裂的IPython的logplay功能)

我想这是可能的使用IPython的笔记本电脑,但至少有一台机器上我需要这个,我仅限于IPython的0.10.2。

编辑:我想知道如何自动设置的,即在配置文件中。 现在我的配置看起来像

from time import strftime
import os
logfilename = strftime('ipython_log_%Y-%m-%d')+".py" 
logfilepath = "%s/%s" % (os.getcwd(),logfilename) 

file_handle = open(logfilepath,'a') 
file_handle.write('########################################################\n') 
out_str = '# Started Logging At: '+ strftime('%Y-%m-%d %H:%M:%S\n') 
file_handle.write(out_str) 
file_handle.write('########################################################\n') 
file_handle.close() 

c.TerminalInteractiveShell.logappend = logfilepath
c.TerminalInteractiveShell.logstart = True

但指定c.TerminalInteractiveShell.log_output = True似乎也没有影响

Answer 1:

还有的-o为选项%logstart

-o: log also IPython's output.  In this mode, all commands which
  generate an Out[NN] prompt are recorded to the logfile, right after
  their corresponding input line.  The output lines are always
  prepended with a '#[Out]# ' marker, so that the log remains valid
  Python code.

附录:如果你是在哪个日志已经启动了一个互动的IPython会话,你必须首先停止记录,然后重新启动:

In [1]: %logstop

In [2]: %logstart -o
Activating auto-logging. Current session state plus future input saved.
Filename       : ./ipython.py
Mode           : backup
Output logging : True
Raw input log  : False
Timestamping   : False
State          : active

观察到,在重新启动后,“输出记录”,现在是“真”。



文章来源: Log IPython output?
标签: ipython