#!/usr/bin/env python# -*- coding: utf-8; -*-# Copyright (c) 2022 Oracle and/or its affiliates.# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/importloggingimportsys# TODO - Revisit this as part of ADS logging changes https://jira.oci.oraclecorp.com/browse/ODSC-36245# Use a unique logger that we can individually configure without impacting other log statements.# We don't want the logger name to mention "ads", since this logger will report any exception that happens in a# notebook cell, and we don't want customers incorrectly assuming that ADS is somehow responsible for every error.logger=logging.getLogger("ipython.traceback")# Set propagate to False so logs aren't passed back up to the root logger handlers. There are some places in ADS# where logging.basicConfig() is called. This changes root logger configurations. The user could import/use code that# invokes the logging.basicConfig() function at any time, making the behavior of the root logger unpredictable.logger.propagate=Falselogger.handlers.clear()traceback_handler=logging.StreamHandler()traceback_handler.setFormatter(logging.Formatter("%(levelname)s - %(message)s"))logger.addHandler(traceback_handler)def_log_traceback(self,exc_tuple=None,**kwargs):try:etype,value,tb=self._get_exc_info(exc_tuple)exceptValueError:print("No traceback available to show.",file=sys.stderr)returnmsg=etype.__name__,str(value)# User a generic message that makes no mention of ADS.logger.error("Exception",exc_info=(etype,value,tb))sys.stderr.write("{0}: {1}".format(*msg))
[docs]defconfigure_plotting():try:importIPythonfromIPythonimportget_ipythonfromIPython.core.errorimportUsageErroripy=get_ipython()ifipyisnotNone:try:# show matplotlib plots inlineipy.run_line_magic("matplotlib","inline")exceptUsageError:# ignore error and use the default matplotlib modepassexceptModuleNotFoundError:importmatplotlibasmplmpl.rcParams["backend"]="agg"importmatplotlib.pyplotaspltplt.switch_backend("agg")
try:importIPythonglobalorig_ipython_tracebackifIPython.core.interactiveshell.InteractiveShell.showtraceback!=_log_traceback:orig_ipython_traceback=(IPython.core.interactiveshell.InteractiveShell.showtraceback)# Override the default showtraceback behavior of ipython, to show only the error message and log the stacktraceIPython.core.interactiveshell.InteractiveShell.showtraceback=_log_tracebackexceptModuleNotFoundError:pass