-
Notifications
You must be signed in to change notification settings - Fork 20
allow multiple handlers per logger #44
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
Conversation
The choice is now whether to print a warning. Previously this was about re-raising the exception. This is done for stability's sake.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tested this out with a Feather S2 TFT and noticed some differences from current released version behavior that I'm not sure if were intentional or not.
The simpletest script inside this repo run with the current released version outputs this:
code.py output:
6081.068: ERROR - Error message
When the same example is run with the version from this PR branch it outputs multiple times to the error level:
code.py output:
6229.832: ERROR - Error message
6229.832: ERROR - Error message
6229.836: ERROR - Error message
Was this change intentional? It seems that the first two prints are duplicates from logger.error("Error message")
and the 3rd one is from the null logger further down in the example null_logger.error("Error message")
which is intended not to print if my understand is correct.
Good catch. This is expected, although the behavior is undesired and not matching CPython. This stems from the difference to CPython that has a concept of "last resort" handler. In CircuitPython logging a Will need to change this somewhat. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me. I tested the changes successfully on a Feather S2 TFT using a modified version of the simpletest to use multiple handlers (one file, one stream in my case.)
Thanks for working on this @vladak!
Do you know if your MQTT handler could work with adafruit.io as the backend server instead of a custom one? Or is there some specific behavior in your server that allows that to work? If it's possible to do that with adafruit.io, I'd be in favor of sharing that as an additional example in this repo if you're open to that idea. I could see that being particularly useful for projects that are in inconvenient to access locations.
Thanks for the improvements to simpletest script as well. The additional case, and labels printed make it more clear what's happening and how the class behaves with different possible inputs.
Updating https://github.com/adafruit/Adafruit_CircuitPython_MLX90640 to 1.2.14 from 1.2.13: > Merge pull request adafruit/Adafruit_CircuitPython_MLX90640#30 from tcfranks/main > Add .venv to .gitignore Updating https://github.com/adafruit/Adafruit_CircuitPython_Wiznet5k to 1.12.16 from 1.12.15: > Merge pull request adafruit/Adafruit_CircuitPython_Wiznet5k#70 from BiffoBear/add_typing > Add .venv to .gitignore Updating https://github.com/adafruit/Adafruit_CircuitPython_HTTPServer to 1.0.1 from 1.0.0: > Merge pull request adafruit/Adafruit_CircuitPython_HTTPServer#27 from spovlot/response-header-overwrite > Add .venv to .gitignore Updating https://github.com/adafruit/Adafruit_CircuitPython_Logging to 5.1.0 from 5.0.1: > Merge pull request adafruit/Adafruit_CircuitPython_Logging#44 from vladak/multiple_handlers > Add .venv to .gitignore Updating https://github.com/adafruit/Adafruit_CircuitPython_Bundle/circuitpython_library_list.md to NA from NA: > Updated download stats for the libraries
Sure ! With Adafruit IO this works rather nicely. Will submit a PR with the example. As for the tests, there is a big room for improvement. I was thinking about mocking, however it needs some more thought. |
This change adds the possibility to have multiple handlers per logger. My motivation for this was a specific use case, that is a MQTT handler that I'd like to use in addition to the classic stream handler to make it easier to see what is going on with my QtPy's:
and the actual use:
While this seemed like a no brainer change initially, I had to do some choices that might not be in concordance with the intended use of this library. Namely:
emit()
raises exceptionSpecifically the second choice is a bit stronger than what is found in CPython (see the comments on differences I found between CPython and CircuitPython logging on [1]), however my feeling was that in embedded environment (where CircuitPython is more likely to be used, I think) it is more important to make sure all the handlers are visited during
emit()
, no matter what the individual outcome is. To see how it works in action, consider this handler:and its use:
which produces the following to the console:
and the
log.txt
file will actually contain the log record.If you want this to behave differently, just let me know in comments and I will change it.
There do not seem to be any tests, let me know how to proceeed there. I only ran the above programs using Blinka.
[1] https://gist.github.com/vladak/15788891bdc2d4742dd106d65667b5a9