python相当于idl的日志函数?

时间:2012-09-14 13:36:45

标签: logging ipython idl idl-programming-language journal

我试图在python(或ipython)中找到idl的日志的等效函数。 我知道ipython有%logstart函数,但只记录ipython中的输入/输出,所以如果我运行我的脚本并要求我输入值,这些都不会进入日志。为了清楚起见,我在运行EELTnoM6.py脚本时是我的终端:

In [11]: run EELTnoM6

################################################################
##             STOKES vector after the system                 ##
################################################################ 

== Demodulation matrix ==
Automatic:                A
ZIMPOL/EELT:              Z
EELT IF                   Eif
Custom                    C
Demodulation matrix?: C
Efficiency of the detector?: 1.
Demodulation matrix? (e.g. [ [1.,0.],[0.,1.] ]): [[1/6.,1/6.,1/6.,1/6.,1/6.,1/6.],     [0.5,-0.5,0.,0.,0.,0.],[0.,0.,-0.5,0.5,0.,0.],[0.,0.,0.,0.,-0.5,0.5]]
ModelStokesMeasurement time =  65.6509261131
Simulation time =  151.731481075

以下是我在日志中的内容:

# IPython log file
%logstart -o -r EELTnoM6_log rotate
ls
%logstop
run EELTnoM6
%logoff

我想在日志中存储脚本要求时输入的输入,即

Demodulation matrix?: C 
Efficiency of the detector?: 1.
Demodulation matrix? (e.g. [ [1.,0.],[0.,1.] ]): [[1/6.,1/6.,1/6.,1/6.,1/6.,1/6.],[0.5,-0.5,0.,0.,0.,0.],[0.,0.,-0.5,0.5,0.,0.],[0.,0.,0.,0.,-0.5,0.5]]

所以C,1和矩阵能够用相同的值再次运行它。这在IDl中非常容易,所以当我找不到相同的ipython时,我感到非常惊讶......

1 个答案:

答案 0 :(得分:0)

我想你只想使用logging功能。它非常棒,可以很好地处理多线程。

import logging

log_file = 'journal.log' # Specify path to your log file

log_format = '[%(asctime)s] %(levelname)s: %(message)s'  # How the output is displayed
log_date_fmt = '%m/%d/%Y %I:%M:%S %p'  # How the asctime is displayed

logging.basicConfig(filename=log_file, level='DEBUG', filemode='w', format=log_format, datefmt=log_date_fmt)  # Fire up the logger
logging.info('Logger initialized')  # There are DEBUG, INFO, WARNING, ERROR levels

def journal(msg):
    ans = raw_input(msg)  # Get your user input
    logging.debug(msg + ans)  # Choosing to put it as DEBUG category
    return ans

ans = journal('Demodulation matrix?: ')  # From your example

给出输出(包含在./journal.log

中)
  

[09/17/2014 04:11:49 PM] INFO:Logger初始化

     

[09/17/2014 04:11:51 PM] DEBUG:解调矩阵?:C