1

I have a long script which I run on a remote server and I want to log all outputs as well as error messages to a file.

  1. I know how to log all terminal outputs (e.g. print()) to a .txt file:
# in script:
import sys sys.stdout = open('./myfile.txt', 'w')
# ... 
sys.stdout.close()

# in terminal:
python myscript.py > ./myfile.txt

This writes all print() outputs to a file, which I want. But it does not write error messages to the file in case it fails.

  1. I know how to log errors
import logging
try:
   # ...
except ZeroDivisionError as e:
   logging.error(e)  # ERROR:root:division by zero

The problem with all solutions linked to the logging module is, that I need to know where the error will occur to wrap it in a logging.error() function. But I don't always know where my errors will occur.

=> How do I (1) write all outputs and (2) the error that makes my script fail to a file?

Moritz
  • 2,835
  • 2
  • 6
  • 12
  • install [sentry](https://sentry.io/welcome/), its simple and easy, also they do offer a free subscription, i myself have been using it for the past 2 months and i love it. error logging as never been easier – Dean Van Greunen Aug 01 '22 at 14:15

3 Answers3

0

You have to redirect python error output sys.stderr to a file:

import sys

sys.stderr = open("errors.txt", "w")
alaptiko
  • 499
  • 3
  • 14
  • ah right thanks, I didn't know that there is a separate sys.stderr for errors next to sys.stdout. Is it possible to write both standard outputs and errors to the same file? what would the code look like? – Moritz Aug 02 '22 at 19:44
0

I think it is possible to create a new file that contains all the logs you want to write. Configuration would look something like this:

import logging
logging.basicConfig(filename='std.log', filemode='w', format='%(name)s - %(levelname)s - %(message)s')

this will output the logging messages to the std.log file

09fgjbHH90_
  • 53
  • 1
  • 6
  • So in this case, I first manually crate a std.log file in my directory and then after running this code every output from the following code (prints, errors, warnings etc.) are written to this file? – Moritz Aug 02 '22 at 19:42
0

Found the answer myself: just type this in the terminal and make sure that you have a log.text file in the right directory. No changes in the script needed.

python script.py &> ./log.txt

Inspired from this question: Redirect stdout and stderr to same file using Python

Moritz
  • 2,835
  • 2
  • 6
  • 12