0

I'm trying to write all the stacktrace output that appears in the console to a file. Even better would be to also display in the console and written to a file. I'm running a pyqt application with multiple modules.

I'm using the approach here Print exception with stack trace to file

It works fine if I copy the above approach:

import logging
from logging.handlers import RotatingFileHandler
import traceback

logger = logging.getLogger("Rotating Log")
logger.setLevel(logging.ERROR)
handler = RotatingFileHandler("log.txt", maxBytes=10000, backupCount=5)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
handler.setFormatter(formatter)
logger.addHandler(handler)
try:
    1/0 
except Exception as e:
    logger.error(str(e))
    logger.error(traceback.format_exc())

But if I change the "try:" to run my application I get no output.

try:
     _ventana = arcMainWindow()
     _ventana.show()
    app.exec_()

For example, the application calls the function below, and this has an error in it like print(9/0) then I don't get any output.

self.myQTbutton.clicked.connect(lambda:mymodule.myfunction(self))

I've not been able to find a simple way to ensure that a traceback from any function in any module gets written to the same log file. I've tried importing the logger but couldn't get that to import correctly. Would appreciate any help with this as new to this aspect of python.

A Rob4
  • 1,278
  • 3
  • 17
  • 35

0 Answers0