Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 40 commits into from
Apr 1, 2024

Conversation

naishasinha
Copy link
Member

@naishasinha naishasinha commented Mar 18, 2024

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

  • My pull request has a descriptive title (not a vague title like Update index.md).
  • My pull request targets the default branch of the repository (main or master).
  • My commit messages follow best practices.
  • My code follows the established code style of the repository.
  • I added or updated tests for the changes I made (if applicable).
  • I added or updated documentation (if applicable).
  • I tried running the project locally and verified that there are no
    visible errors.

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 naishasinha requested review from a team as code owners March 18, 2024 06:28
@naishasinha naishasinha requested review from TimidRobot and annatuma and removed request for a team March 18, 2024 06:28
Copy link
Member

@TimidRobot TimidRobot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looking good! just a few requested changes

Line 205 - changed the log so that it keeps the traceback
Line 124 - Logging statement includes traceback
Line 70 - logging statement includes traceback
Line 330 - Logging statement includes traceback
Line 541 - kept exit status (130)
Line 544 - kept exit status (1) 
Replaced with f-strings on lines 412, 413, and 538
- Logging kept inclusion of Error (1), (130), as well as using f-string for e.code
- Logging included keeping Error (1), Error (130), and used f-string for e.code
- Logging kept the inclusion of Error (1), Error (130), and f-string for e.code
- Logging kept the inclusion of Error (1), Error (130), tracebacks, and f-string for e.code
- Kept the inclusion of (1), (130), traceback, and f-string for e.code
- Updated by including (1), (130), traceback, and f-string for e.code
- Updated logging statements to include (1), (130), traceback, and f-string for e.code
@naishasinha
Copy link
Member Author

naishasinha commented Mar 21, 2024

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)

Copy link
Member

@TimidRobot TimidRobot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please fix last f-string and resolve static analysis errors

@TimidRobot
Copy link
Member

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

@naishasinha
Copy link
Member Author

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
Copy link
Member Author

@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 naishasinha requested a review from TimidRobot March 30, 2024 21:50
Copy link
Member

@TimidRobot TimidRobot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a few more static analysis errors to resolve

./dev/tools.sh
 isort                                                                           
Loading .env environment variables...
Skipped 1 files

 black                                                                           
Loading .env environment variables...
reformatted /Users/timidrobot/CreativeCommons/git/quantifying/wikipedia/wikipedia_scratcher.py
reformatted /Users/timidrobot/CreativeCommons/git/quantifying/google_custom_search/google_scratcher.py
reformatted /Users/timidrobot/CreativeCommons/git/quantifying/visualization/visualization_engineering.ipynb

All done! ✨ 🍰 ✨
3 files reformatted, 15 files left unchanged.

 flake8                                                                          
Loading .env environment variables...
./deviantart/deviantart_scratcher.py:7:1: F401 'datetime as dt' imported but unused
./metmuseum/metmuseum_scratcher.py:8:1: F401 'datetime as dt' imported but unused
./vimeo/vimeo_scratcher.py:11:1: F401 'datetime as dt' imported but unused
./wikicommons/wikicommons_scratcher.py:8:1: F401 'datetime as dt' imported but unused
./youtube/youtube_scratcher.py:8:1: F401 'datetime as dt' imported but unused

or

pipenv run pre-commit run -a
Loading .env environment variables...
check for added large files..............................................Passed
check python ast.........................................................Passed
check for case conflicts.................................................Passed
check docstring is first.................................................Passed
check that executables have shebangs.....................................Passed
check json...............................................................Passed
check for merge conflicts................................................Passed
check toml...............................................................Passed
check yaml...............................................................Passed
detect destroyed symlinks................................................Passed
fix end of files.........................................................Passed
mixed line ending........................................................Passed
trim trailing whitespace.................................................Failed
- hook id: trailing-whitespace
- exit code: 1
- files were modified by this hook

Fixing wikipedia/wikipedia_scratcher.py
Fixing google_custom_search/google_scratcher.py

Black....................................................................Passed
Flake8...................................................................Failed
- hook id: flake8
- exit code: 1

deviantart/deviantart_scratcher.py:7:1: F401 'datetime as dt' imported but unused
wikicommons/wikicommons_scratcher.py:8:1: F401 'datetime as dt' imported but unused
metmuseum/metmuseum_scratcher.py:8:1: F401 'datetime as dt' imported but unused
youtube/youtube_scratcher.py:8:1: F401 'datetime as dt' imported but unused
vimeo/vimeo_scratcher.py:11:1: F401 'datetime as dt' imported but unused

isort....................................................................Passed

@naishasinha naishasinha requested a review from TimidRobot April 1, 2024 16:00
@naishasinha
Copy link
Member Author

naishasinha commented Apr 1, 2024

There's a few more static analysis errors to resolve

@TimidRobot Fixed!

Copy link
Member

@TimidRobot TimidRobot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good work!

@TimidRobot TimidRobot merged commit 9edade9 into creativecommons:main Apr 1, 2024
1 check passed
@TimidRobot TimidRobot self-assigned this Apr 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Feature] Integrate Python Logging into Codebase for Improved Management
2 participants