GeoEco.Logging.Logger.LogExceptionAsError
- classmethod Logger.LogExceptionAsError(format=None, *args)
Logs a Python exception caught by a GeoEco class as an error message and additional information as debug messages.
GeoEco classes should use
LogExceptionAsWarning()orLogExceptionAsError()to report exceptions caught by except clauses of try statements, like this:Logger.Debug(_(u'Copying file %s to %s.') % (sourceFile, destinationFile)) try: shutil.copy2(sourceFile, destinationFile) except: Logger.LogExceptionAsError(_(u'Could not copy file %(source)s to %(dest)s.') % \ {u'source' : sourceFile, u'dest' : destinationFile}) raise
As shown, the except clause should re-raise the exception (if appropriate) using a raise statement with no parameters.
LogExceptionAsWarning()andLogExceptionAsError()will log the exception and some debugging information, including a stack trace. If the caller provides the optional format string, it is logged as a “consequence” of the original error. For example the code above produces the following output when the caller does not have permission to write the destination file:DEBUG Copying file c:\foo.txt to c:\bar.txt. ERROR IOError: [Errno 13] Permission denied: u'c:\\bar.txt' DEBUG ---------- BEGINNING OF DEBUGGING INFORMATION ---------- DEBUG Traceback (most recent call last): DEBUG File "<stdin>", line 1, in ? DEBUG File "<stdin>", line 2, in tryit DEBUG File "C:\Python24\Lib\site-packages\GeoEco\FileSystemUtils.py", line 47, in CopyFile DEBUG shutil.copy2(sourceFile, destinationFile) DEBUG File "C:\Python24\lib\shutil.py", line 92, in copy2 DEBUG copyfile(src, dst) DEBUG File "C:\Python24\lib\shutil.py", line 48, in copyfile DEBUG fdst = open(dst, 'wb') DEBUG IOError: [Errno 13] Permission denied: u'c:\\bar.txt' DEBUG End of traceback. Logging other useful debugging information... DEBUG sys.argv = [''] DEBUG sys.version = 2.4.4 (#71, Oct 18 2006, 08:34:43) [MSC v.1310 32 bit (Intel)] DEBUG sys.version_info = (2, 4, 4, 'final', 0) DEBUG sys.platform = win32 DEBUG sys.getwindowsversion() = (5, 1, 2600, 2, 'Service Pack 2') DEBUG ... DEBUG ---------- END OF DEBUGGING INFORMATION ---------- ERROR The following consequences resulted from the original error: ERROR Could not copy file c:\foo.txt to c:\bar.txt.
Unless the user has debugging messages turned on, they will only see the warning and error messages in the log:
ERROR IOError: [Errno 13] Permission denied: u'c:\\bar.txt' ERROR The following consequences resulted from the original error: ERROR Could not copy file c:\foo.txt to c:\bar2\bar.txt.
Except clauses higher on the stack can also call
LogExceptionAsWarning()andLogExceptionAsError(). The methods keep track of whether the original exception was logged and will not log it a second time. Instead they will just log the optional format string, if provided, as a subsequent “consequence” of the original exception. This allows nested methods to illustrate how the low-level failure causes a problem in the high-level operation the user actually cares about. For example, if the output file from a function cannot be copied from a temporary location because a directory cannot be created, the log might look like this:ERROR IOError: [Errno 13] Permission denied: u'c:\\output' ERROR The following consequences resulted from the original error: ERROR Could create directory c:\output. ERROR Could not copy file c:\processing\results.txt to c:\output\results.txt. ERROR Could not copy the results to the output directory.
- Parameters:
format (
str, optional) – A printf-style format string. Minimum length꞉ 1.args (
tupleofobject) – Values to merge into the format string.