2012-08-06 7 views
9

IPythonのロギング機能に出力と入力を含める方法はありますか?IPython出力をログに記録しますか?

これは、ログファイルが現在のようになります。

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

私は1つの以上の行が表示したいのです:私はそれが必要とされていると仮定しているため

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

#です機能を無効にするため)

これはIPythonノートブックを使用しても可能ですが、少なくとも1台のマシンではこれが必要ですipython 0.10.2を編集しました。

編集:これを設定ファイル内で自動的に設定する方法を知りたいと思います。今、私のconfigが

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を指定すると、持っていない%logstartため-oオプションあります

答えて

8

に影響を与えているようだ:

-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 

再起動後、 "出力ログ"が "True"になることに注意してください。

+1

解決策は、ここに記載されているように、スタートアップスクリプトでこれを行うように見えます。http://wiki.ipython.org/Cookbook/DatedLog(これはhttp://stackoverflow.com/questions/11836612で提供された解決策でした。/where-how-in-context-is-ipythons-configuration-file-executed?lq = 1) – keflavich

関連する問題