python
import logging
logger = logging.getLogger(__name__)Reference for Python logging, stack traces, warning visibility, trace hooks, and instrumentation patterns.
import logging
logger = logging.getLogger(__name__)import logging
logging.basicConfig(filename='app.log', level=logging.DEBUG)Attach the current stack to a log message.
logger.warning("Unexpected state", stack_info=True)try:
risky_call()
except Exception:
logger.error("Failure", exc_info=True)import warnings
warnings.simplefilter('default')python -W error app.pypython -m trace --trace app.pypython -m trace --count app.pyEmit stack traces after a timeout to catch hangs.
import faulthandler
import sys
faulthandler.dump_traceback_later(30, repeat=True, file=sys.stderr)faulthandler.cancel_dump_traceback_later()