Closed milicag closed 2 years ago
Here is my example of logging when an error encountered. The caller will handle the exception and log the error, so there's no need to have logging.error() at the callee level.
def raise_error(name):
# check if a file exists
raise IOError("File not exists")
def handler():
try:
raise_error('where is the file')
except Exception as err:
logging.error(f" {err=}, {type(err)=}")
if __name__ == '__main__':
logging.basicConfig(filename='run.log', level=logging.DEBUG)
handler()
and the log file looks like this
ERROR:root: err=OSError('File not exists'), type(err)=<class 'OSError'>
Why is this better than just having the adapter package report the error?
We pass the logger between Python packages and rely on this in the log files.
We pass the logger between Python packages and rely on this in the log files.
It will act the same because the way adapter's log configured here--using __name__
variable will hold the name of the module (python file) that called the code.
Why is this better than just having the adapter package report the error?
this approach captures error type and message, avoids redundant message passing(increases performance), and ensure the caller method handle the exception/error caused by the caller.
Please address comments I provided here:
https://github.com/LBNL-ETA/Adapter/pull/11#pullrequestreview-838004710