Use Python logging to log messages and stderr

Viewed 46

I know how to log message/errors I create myself:

logging.basicConfig(filename='error.log', filemode='a', level=logging.DEBUG)

logging.error(f"could not insert error: {error.args[0]}")

But this message is created when catching an exception, what if there was some uncaught error to the stderr, how can I redirect that message in the same log?

I know about redirecting error output through

python test.py 2>> error.log

But I am not sure this is the right way. I want both the messages I log myself and the stderr logged into one homogeneous log.

To be clear you cannot do this two things at the same time to same file, the permission is denied for the logging.

0 Answers
Related