Pregunta

I just wants to write exception details in file: I tried as below"

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()

in except block:

 except Exception as e :
        WriteErrorLog(e)

Its not working, can someone correct me? or can suggest any better option? Thanks!!

¿Fue útil?

Solución

I would use Python Loggin -> http://docs.python.org/2/library/logging.html You can format its lines to be ',' delimited, for example

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

Datetime, level_of_log, whatever_message_you_want_to_log

I recommend you http://docs.python.org/2/howto/logging.html#logging-basic-tutorial to start

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top