-1

我只想在文件中写入异常详细信息:我尝试如下“

def WriteErrorLog(e) :
   global lcnt

   lstErr=[]

   iUrlfOutputFile = open("C:\Error.log", "a")
   csvUrlfInfoWriter = csv.writer(iUrlfOutputFile,  delimiter=',', lineterminator='\n')
   exc_type, exc_obj, exc_tb = sys.exc_info()
   fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
   csvUrlfInfoWriter.writerow('[Exc_Type, FileName, Script_Line No,InputFile_LineNo]')
   lstErr.append(exc_type)
   lstErr.append(fname)
   lstErr.append(g_lcnt)
   for value in lstErr :
       csvUrlfInfoWriter.writerow(value)

   iUrlfOutputFile.close()

在除了块:

 except Exception as e :
        WriteErrorLog(e)

它不起作用,有人可以纠正我吗?或者可以提出任何更好的选择?谢谢!!

4

1 回答 1

2

我会使用 Python Loggin -> http://docs.python.org/2/library/logging.html 例如,您可以将其行格式化为“,”分隔

[formatter_generic_form]
format='%(asctime)s , %(levelname)s , %(message)s'
datefmt='%Y-%m-%d %H:%M:%S'

日期时间、level_of_log、whatever_message_you_want_to_log

我推荐你http://docs.python.org/2/howto/logging.html#logging-basic-tutorial开始

于 2013-07-11T10:08:40.440 回答