SeedSigner / seedsigner

Use an air-gapped Raspberry Pi Zero to sign for Bitcoin transactions! (and do other cool stuff)
MIT License
720 stars 168 forks source link

Setup Python logging + convert print statements to logger calls #558

Closed dbast closed 4 months ago

dbast commented 6 months ago

There are many many commented out print statements in the code base that could be converted to logger.debug(...) calls and thus be shown additionally via running main.py -l DEBUG, but that is for next PRs as all those commented out print statements can be potential broken by referencing non existing variables etc.

This is a move towards more Pythonic ways of doing: The logging framework with its central configurability, options to set levels, the ability to activate certain log lines by choosing a different log level (without un-commenting/commenting print statements), the possibility to the see how much times is between log calls (without extra instrumenting the code) etc is all in all very convenient.

The help output on cli:

usage: main.py [-h] [-l {CRITICAL,FATAL,ERROR,WARN,WARNING,INFO,DEBUG,NOTSET}]

options:
  -h, --help            show this help message and exit
  -l {CRITICAL,FATAL,ERROR,WARN,WARNING,INFO,DEBUG,NOTSET}, --loglevel {CRITICAL,FATAL,ERROR,WARN,WARNING,INFO,DEBUG,NOTSET}
                        Set the log level (default: INFO)

Description

Describe the change simply. Provide a reason for the change.

Include screenshots of any new or modified screens (or at least explain why they were omitted)

This pull request is categorized as a:

Checklist

If you modified or added functionality/workflow, did you add new unit tests?

I have tested this PR on the following platforms/os:

Note: Keep your changes limited in scope; if you uncover other issues or improvements along the way, ideally submit those as a separate PR. The more complicated the PR the harder to review, test, and merge.

dbast commented 6 months ago

yay. green.

dbast commented 4 months ago

rebased

dbast commented 4 months ago

rebased

jdlcdl commented 4 months ago

I'm torn on the concept of logging between:

My worry is that currently, the sdcard is mounted if the user left it in. Add human error (like me not paying attention to remove logging of a mnemonic and passphrase to a debug log before submitting a pr... and it makes it into a persisted file, plus human error like others not paying attention and it gets merged.

Thank you for all of your great prs... and in advance for settling my paranoid (because I don't know better) nerves.

dbast commented 4 months ago

@jdlcdl right... both the existing print statements AND the new logging statements can be problematic IF the console output (stdout/stderr) is redirected to a file on sd-card:

kdmukai commented 4 months ago

Logging > print: ACK


Recommend we drop mention of FATAL in the usage and standardize on CRITICAL.

see: https://stackoverflow.com/a/31170408/11611893

Later TODO: Provide explicit style / convention guidance for devs in a README.

kdmukai commented 4 months ago

One thing I can't think through is: what kinds of logging should be INFO vs DEBUG? I think the question is a bit different for us than typical projects since our devs are the only ones who'll ever see any of these messages. So in that sense, EVERY log line is intended to aid debugging.

I guess for now the best we can do is default to INFO and then over time make decisions about what log chatter we want to quiet down and demote to DEBUG.

kdmukai commented 4 months ago

Also filter out WARN?

https://stackoverflow.com/a/15540092/11611893

kdmukai commented 4 months ago

Moving main.py is only successful if the setup has already run python -m pip install -e . (which is not currently in our local dev setup steps in raspberry_pi_os_build_instructions.md; I don't know how SeedSigner OS is setup).

Because main.py is importing from the seedsigner.controller package, EITHER main.py has to sit above the seedsigner package / dir OR the seedsigner module needs to be globally available to import.

I also dislike symlinks in repos in general.

Moving main.py was caused by the desire to add these new tests to the test suite. With maybe the exception of verifying the stderr writes, I don't think the tests are necessary. We don't need to test argparse module's basic functionality and I can live without verifying the log level. Even the verification of stderr is maybe arguably just testing the logging module itself.

I'm reading up on logging.ini configs now. Having the output explicitly stated there (plus some other needed config, e.g. silencing PIL) tentatively feels preferable.

dbast commented 4 months ago

@kdmukai Added another commit that 1. moves the main.py back 2. eliminates the introduced symlink 3. still gets the added tests working by doing an import trick :)

I cautiously converted all existing print statements to logging.info and set the INFO log level as default log level to mimic the current behaviour of the print statements... further PRs can then refine the level of each message and also enable the currently commented-out print statements on e.g. debug level.... this PR is intended more on finding agreement regarding the overall concept

kdmukai commented 4 months ago

Final minor nits noted above, but otherwise very happy to ACK this!

Very nice quality-of-life improvement here, @dbast.

newtonick commented 4 months ago

tACK. Great contribution as usual @dbast!