2012-08-06 47 views
9

有什麼辦法可以讓IPython的日誌功能包括輸出和輸入?登錄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似乎也沒有影響

回答

8

還有的-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 

注意到,重新啓動後,「輸出記錄」現在爲「真」。

+1

該解決方案似乎是在啓動腳本中執行此操作,如下所述:http://wiki.ipython.org/Cookbook/DatedLog(這是http://stackoverflow.com/questions/11836612中提供的解決方案/其中-如何合什麼-上下文是-ipythons配置文件執行的?LQ = 1) – keflavich 2012-08-15 15:46:40