Catching python errors

I used this and it works fine if i run my client as py file:

import logging

#create an ampty log file
fname='error.log'
file=open(fname,"w")
file.close()

logging.basicConfig(filename=fname,filemode='w')

try:
    #client is like main.py
    import client
except Exception, ex:        
    logging.exception("")

But it does not work as a p3d file, the error.log file does not get the error written.

This:

nout = MultiplexStream() 
Notify.ptr().setOstreamPtr(nout, 0) 
nout.addFile(Filename("out.txt"))

shows only panda errors, but not a division by 0.

And sadly the panda runtime log folder log files dont show any errors at all.

How could i see/create a log file with error messages like this:

Traceback (most recent call last):
  File "client.py", line 10786, in <module>
    run()
  File "D:\Panda3D-1.7.0\direct\showbase\ShowBase.py", line 2531, in run
    self.taskMgr.run()
  File "D:\Panda3D-1.7.0\direct\task\Task.py", line 496, in run
    self.step()
  File "D:\Panda3D-1.7.0\direct\task\Task.py", line 454, in step
    self.mgr.poll()
  File "D:\Panda3D-1.7.0\direct\showbase\EventManager.py", line 61, in eventLoopTask
    self.doEvents()
  File "D:\Panda3D-1.7.0\direct\showbase\EventManager.py", line 55, in doEvents
    processFunc(self.eventQueue.dequeueEvent())
  File "D:\Panda3D-1.7.0\direct\showbase\EventManager.py", line 122, in processEvent
    messenger.send(eventName, paramList)
  File "D:\Panda3D-1.7.0\direct\showbase\Messenger.py", line 325, in send
    self.__dispatch(acceptorDict, event, sentArgs, foundWatch)
  File "D:\Panda3D-1.7.0\direct\showbase\Messenger.py", line 410, in __dispatch
    method (*(extraArgs + sentArgs))
  File "client.py", line 2040, in buttons
    self.clogic(but,tag)
  File "client.py", line 5089, in clogic
    x=10/0
ZeroDivisionError: integer division or modulo by zero

Thank you in advance.

Hi, for correct-ness the log file is named “p3dsession.log”, that contains your application’s terminal output when it’s running via a p3d file.

I’d try something like this:


import logging

#create an ampty log file
fname='error.log'
file=open(fname,"w")
file.close()

logging.basicConfig(filename=fname,filemode='w')

try:
    #client is like main.py
    import client
except Exception as ex:       
    logging.exception("") 

I’ve never seen the comma before, it may work, I’ve always used “Exception as i” or something… other than that it’s very possible that it did indeed work for you and you only read the wrong log file! :smiley:

That is correct yet the python errors dont appear there

" and ’ behave the same way.
If i run my app as a py file the error gets written into error.log file, running as a p3d the error does not get written.
PS im not trying to write stuff to p3dsession.log

Please, could you try this?

sys.stdout = open( 'yourApp_report.txt', 'w' )
sys.stderr = open( 'yourApp_errors.txt', 'w' )

Yes i could :slight_smile:
Can’t thank you enough this did exactly what i wanted.
Once again thank you very much.

Now all i have to do is cross my fingers and hope it works on linux and mac the same way.

You’re welcome! :slight_smile:

Yes, this should work (I use this on Win/OSX/Linux).