creativecommons / quantifying

quantify the size and diversity of the commons--the collection of works that are openly licensed or in the public domain
MIT License
24 stars 34 forks source link

Integrate Python logging module into all .py files of Quantifying + README Update for Logging #97

Closed naishasinha closed 7 months ago

naishasinha commented 8 months ago

Fixes

Fixes #85 by @naishasinha

Description

This pull request incorporates an efficient method to use logging within all Python files, keeping in mind the possibility of shared modules. I've also added an update to the "Resources" section in the README with a link to the logging section of the Python docs that other contributors can reference to if they need help with any aspect of the module.

In addition to replacing all the current print() statements with logging, I've also added descriptor LOG.info() statements within the start of each function so that when being logged in the terminal, it's easier for the contributor to understand which function is being debugged.

Technical details

Multiple calls to LOG (which is a variable stored from getLogger()) will return a reference to the same logger object, both within the same module as well as across modules

This was the general logger setup for all files. As it is consistent among all files, it makes it easier to access and reference among shared modules:

  1. Set up the logger
  2. Define both the handler and the formatter
  3. Add formatter to the handler
  4. Add handler to the logger
  5. Log the start of the script execution

Other notes:

The Handler object is responsible for sending the logs to different destinations, and the Formatter object determines the order, structure, and contents of the log message. Having these objects will make it easier to log between shared modules.

Checklist

Developer Certificate of Origin

For the purposes of this DCO, "license" is equivalent to "license or public domain dedication," and "open source license" is equivalent to "open content license or public domain dedication."

Developer Certificate of Origin ``` Developer Certificate of Origin Version 1.1 Copyright (C) 2004, 2006 The Linux Foundation and its contributors. 1 Letterman Drive Suite D4700 San Francisco, CA, 94129 Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. Developer's Certificate of Origin 1.1 By making a contribution to this project, I certify that: (a) The contribution was created in whole or in part by me and I have the right to submit it under the open source license indicated in the file; or (b) The contribution is based upon previous work that, to the best of my knowledge, is covered under an appropriate open source license and I have the right under that license to submit that work with modifications, whether created in whole or in part by me, under the same open source license (unless I am permitted to submit under a different license), as indicated in the file; or (c) The contribution was provided directly to me by some other person who certified (a), (b) or (c) and I have not modified it. (d) I understand and agree that this project and the contribution are public and that a record of the contribution (including all personal information I submit with it, including my sign-off) is maintained indefinitely and may be redistributed consistent with this project or the open source license(s) involved. ```
naishasinha commented 8 months ago

looking good! just a few requested changes

Thank you for the feedback @TimidRobot! I just updated the exception handling in all of the files (base code shown below), which 1. replaces the e.code line using an f-string instead, 2. Keeps the (130) and/or (1) in the LOG.info() statements, and 3. I made sure to include the traceback in the log statements as well. Please let me know if I need to fix anything else!

if __name__ == "__main__":
    # Exception Handling
    try:
        main()
    except SystemExit as e:
        LOG.error(f"System exit with code: {e.code}")
        sys.exit(e.code)
    except KeyboardInterrupt:
        LOG.info("(130) Halted via KeyboardInterrupt.")
        sys.exit(130)
    except Exception:
        LOG.error(f"(1) Unhandled exception: {traceback.format_exc()}")
        sys.exit(1)
TimidRobot commented 8 months ago

@naishasinha don't forget to pull changes from merge conflict resolution

naishasinha commented 8 months ago

Please fix last f-string and resolve static analysis errors

@TimidRobot I believe the current static analysis errors in my version are due to the code not aligning with pre-commit, I'll fix those and also pull changes from merge conflict resolution by tonight

naishasinha commented 7 months ago

@TimidRobot I've fixed all the static analysis errors and have also pulled changes from merge conflict resolution! Please let me know if there's anything else I need to do. Thank you for your patience!

naishasinha commented 7 months ago

There's a few more static analysis errors to resolve

@TimidRobot Fixed!