Log exception with traceback

后端 未结 11 1179
失恋的感觉
失恋的感觉 2020-12-02 04:25

How can I log my Python errors?

try:
    do_something()
except:
    # How can I log my exception here, complete with its traceback?
11条回答
  •  长情又很酷
    2020-12-02 04:57

    Uncaught exception messages go to STDERR, so instead of implementing your logging in Python itself you could send STDERR to a file using whatever shell you're using to run your Python script. In a Bash script, you can do this with output redirection, as described in the BASH guide.

    Examples

    Append errors to file, other output to the terminal:

    ./test.py 2>> mylog.log
    

    Overwrite file with interleaved STDOUT and STDERR output:

    ./test.py &> mylog.log
    

提交回复
热议问题