LBNL-ETA / Adapter

The Adapter software is developed at the Energy Efficiency Standards Department and it provides a convenient data table loader from various formats such as xlsx, csv, db (sqlite database), and sqlalchemy. Its main feature is the ability to convert data tables identified in one main and optionally one or more additional input files into database tables and Pandas DataFrames for downstream usage in any compatible software.
1 stars 1 forks source link

Logger and note #15

Closed milicag closed 2 years ago

milicag commented 2 years ago

Please address comments I provided here:

https://github.com/LBNL-ETA/Adapter/pull/11#pullrequestreview-838004710

0xd5dc commented 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'>

milicag commented 2 years ago

Why is this better than just having the adapter package report the error?

milicag commented 2 years ago

We pass the logger between Python packages and rely on this in the log files.

0xd5dc commented 2 years ago

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.

0xd5dc commented 2 years ago

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.