0

I was wondering if you could log every error that a python script has without changing the script itself (i.e adding a try/exception clause or using the logging module)? So whenever an error occurs it automatically writes down in a chosen log file.

Or at least be possible to create a python script which is responsible for listening to other python scripts?

  • If you do not have a try-catch, your script stops, then it cannot write any logs. You probably can get a dump of error if your pipe it to a file while starting like `python script.py > log.txt` – Kris Mar 23 '22 at 14:29
  • 2
    Unless the script in question was designed to handle those errors, it's going to terminate at an uncaught exception no matter what you do from the outside. If you just want to record the error, capturing the stderr with something like `python3 my_script.py 2> error.log` would do the trick. – Brian61354270 Mar 23 '22 at 14:30
  • Look at this answer: https://stackoverflow.com/questions/33500220/how-to-redirect-stderr-to-a-file-in-python – Liutprand Mar 23 '22 at 14:30
  • That's pretty much what I want, thanks. Everytime there is a new error, does this overwrite the log file or is it appended in the end of the file? – Lucas Libório Mar 23 '22 at 17:50
  • If you want to append add an angled bracket ```python3 my_script.py 2>> error.log``` – freshpasta Mar 23 '22 at 19:47

0 Answers0