import logging logger = logging.getLogger(__name__) try: # Some code that may raise an exception except Exception as e: logger.exception("An error occurred: %s", e)
import logging import some_external_library logger = logging.getLogger(__name__) try: some_external_library.do_something() except Exception as e: logger.exception("An error occurred while calling the external library: %s", e)In this code snippet, an exception is caught when calling an external library and is logged using the logger.exception() method. The logging module is a built-in package in Python and does not require any external library.