-
Notifications
You must be signed in to change notification settings - Fork 122
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Extend ops.log to capture warnings #1077
Comments
Good idea, thanks for the suggestion! |
I have done some research into the current Code (drastically simplified import warnings
import logging
class MyLogHandler(logging.Handler):
def emit(self, record):
# pretend to send it to juju log (model_backend.juju_log)
print(f"======= From my log handler: {record.getMessage()}=======")
# important stuff
def showwarning(message, category, filename, lineno, file=None, line=None):
logger.warning(message)
# important stuff 2
warnings.showwarning = showwarning
if __name__ == "__main__":
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
logger.addHandler(MyLogHandler())
logger.info("This is a log.")
warnings.warn("This is a warning.") Result: $ python3 main.py
======= From my log handler: This is a log.=======
======= From my log handler: This is a warning.======= This is achieved by overriding If we want to keep the warnings in the output, another approach is to use import warnings
import logging
# important stuff
logging.captureWarnings(True)
class MyLogHandler(logging.Handler):
def emit(self, record):
# pretend to send it to juju log (model_backend.juju_log)
print(f"======= From my log handler: {record.getMessage()}=======")
if __name__ == "__main__":
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
logger.addHandler(MyLogHandler())
logger.info("This is a log.")
warnings.warn("This is a warning.") Result: $ python3 main.py
======= From my log handler: This is a log.=======
======= From my log handler: /Users/tiexin/work/log-warning-test/main.py:21: UserWarning: This is a warning.
warnings.warn("This is a warning.")
======= |
We definitely want to distinguish the class of warning (DeprecationWarning, PendingDeprecationWarning, etc). The first example doesn't do that, but Do we want the full filename & line number in debug-log? I think we likely do want to have the module and line number, but it seems like the exact position of the charm code might be noise (and debug-log is already so noisy). Similarly, is it valuable to have the code that triggered the warning in debug-log, rather than just the location, the class, and the passed message? I think we would want to make sure that the debug-log output did not contain newlines (the default In terms of other considerations:
|
I missed that I suspect I missed this when I originally opened the ticket because I regularly run debug log like So this can be closed as invalid. I do think that:
Very sorry for requesting something that already exists! |
Although charms can simply use the
logging
module for everything, warnings generated with thewarnings
module have some nice filtering functionality and are generally the more Pythonic way to indicate (e.g.) that something is deprecated. Unit tests generally have all warnings enabled (at least forunittest
- I'm not sure aboutpytest
).The framework could catch warnings emitted by charms and appropriately direct those to the Juju debug log.
The text was updated successfully, but these errors were encountered: