Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save RichardDally/ca5e53678304b66b5682366425effdf5 to your computer and use it in GitHub Desktop.
Save RichardDally/ca5e53678304b66b5682366425effdf5 to your computer and use it in GitHub Desktop.
Log exceptions
import sys
import logging
def throw_an_exception():
raise RuntimeError("omfg this does not work")
def calling_dummy_code():
x = 5
throw_an_exception()
def calling_another_dummy_code():
calling_dummy_code()
def main():
logging.basicConfig(
level=logging.DEBUG,
format="%(asctime)s.%(msecs)03d | %(levelname)s | %(module)s.%(funcName)s | %(message)s",
datefmt="%Y/%m/%d %H:%M:%S",
handlers=[logging.StreamHandler(sys.stdout)],
)
try:
calling_another_dummy_code()
except Exception as exception:
logging.exception(exception)
main()
@RichardDally
Copy link
Author

c:\TEMP\Bootstrapper> c: && cd c:\TEMP\Bootstrapper && cmd /C "c:\Tools\Python3.11\python.exe c:\Users\rdall.vscode\extensions\ms-python.debugpy-2025.6.0-win32-x64\bundled\libs\debugpy\launcher 51356 -- c:\TEMP\Bootstrapper\play_with_exceptions.py "
2025/05/14 22:53:00.522 | ERROR | play_with_exceptions.main | omfg this does not work
Traceback (most recent call last):
File "c:\TEMP\Bootstrapper\play_with_exceptions.py", line 26, in main
calling_another_dummy_code()
File "c:\TEMP\Bootstrapper\play_with_exceptions.py", line 15, in calling_another_dummy_code
calling_dummy_code()
File "c:\TEMP\Bootstrapper\play_with_exceptions.py", line 11, in calling_dummy_code
throw_an_exception()
File "c:\TEMP\Bootstrapper\play_with_exceptions.py", line 6, in throw_an_exception
raise RuntimeError("omfg this does not work")
RuntimeError: omfg this does not work

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment