Skip to content

logger: capture warnings #5072

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 3 commits into from
Jan 12, 2023
Merged

Conversation

bastimeyer
Copy link
Member

See #4785

This makes Streamlink's root logger capture warnings (disabled by default, enabled via streamlink_cli) and it replaces various warning log messages for deprecated (user-facing) stuff with FutureWarnings.
https://docs.python.org/3/library/exceptions.html#warnings
https://docs.python.org/3/library/warnings.html#warning-categories

The reason for this is that this allows for finer control over the kind of deprecation messages (API stuff for devs, or other stuff for end-users) and it uses the standard interfaces for warnings, which is preferred. Warnings from dependencies can also be caught this way, but I'm not sure how useful this is, because I modified the default warning message format, so it doesn't include the file+line of the warning, because it's not useful for Streamlink's deprecation messages (FutureWarning). This can be changed later on if it causes problems. I only realized this while writing this PR text.


Warnings can always be filtered via Python's -W parameter (in addition to the regular log level of Streamlink's root logger):
https://docs.python.org/3/library/warnings.html#describing-warning-filters

$ python -m streamlink --https-proxy ...
[warnings][futurewarning] The https-proxy option has been deprecated in favor of a single http-proxy option
usage: streamlink [OPTIONS] <URL> [STREAM]

$ python -W ignore -m streamlink --https-proxy socks5h://localhost:1920
usage: streamlink [OPTIONS] <URL> [STREAM]

$ python -W ignore::FutureWarning -m streamlink --https-proxy socks5h://localhost:1920
usage: streamlink [OPTIONS] <URL> [STREAM]

The changes of the logger's warning messages are this, when printed from a caught warning, not from regular warning() calls:

[streamlink][warning] The https-proxy option has been deprecated in favor of a single http-proxy option
-->
[warnings][futurewarning] The https-proxy option has been deprecated in favor of a single http-proxy option

So instead of the logger name and the "warning" log level, it logs "warnings" as logger name and uses the name of the warning as log level name.


As a next step, dedicated warning subclasses could be added for reusable code and better deprecation messages with links to the docs, and with better warning names, etc.


Opening as a draft for now...

@bastimeyer
Copy link
Member Author

bastimeyer commented Jan 10, 2023

Decided to change the warning message format, because of warnings from dependencies which should contain all the info necessary to find the cause of those warnings.

The paths of Streamlink deprecation warnings could be suppressed in a future change. Warnings should be subclassed anyway, as mentioned, so this could be done in this step.

New format:

[warnings][name] message
  path:line

Examples:

Streamlink deprecation warning

$ streamlink --https-proxy foo
[warnings][futurewarning] The `https-proxy` option has been deprecated in favor of a single `http-proxy` option
  /home/basti/repos/streamlink/src/streamlink/session.py:104

InsecureRequestWarning (urllib3)

$ streamlink --http-no-ssl-verify '...'
[cli][info] Found matching plugin hls for URL ...
[warnings][insecurerequestwarning] Unverified HTTPS request is being made to host '...'. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/1.26.x/advanced-usage.html#ssl-warnings
  /home/basti/venv/streamlink-311/lib/python3.11/site-packages/urllib3/connectionpool.py:1045

Current master:

Streamlink deprecation warning

$ streamlink --https-proxy foo
[session][warning] The `https-proxy` option has been deprecated in favor of a single `http-proxy` option

InsecureRequestWarning (urllib3)

$ streamlink --http-no-ssl-verify '...'
[cli][info] Found matching plugin hls for URL ...
/home/basti/venv/streamlink-311/lib/python3.11/site-packages/urllib3/connectionpool.py:1045: InsecureRequestWarning: Unverified HTTPS request is being made to host '...'. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/1.26.x/advanced-usage.html#ssl-warnings
  warnings.warn(
Available streams: ...

@bastimeyer bastimeyer marked this pull request as ready for review January 10, 2023 19:52
Add support for capturing warnings and logging them via Streamlink's
root logger on the warning log level. Use custom `WarningLogRecord`s
with a custom warning message format, and replace the record's
logger name with "warnings" and use the name of the warning type
as log level name when showing warnings.

This enables having proper `DeprecationWarning`s and `FutureWarning`s
in the code instead of just using the logger and its warning log level,
and those warnings can be filtered via the regular filtering mechanisms.

For example:
```
[warnings][deprecationwarning] Calling this method is deprecated
[warnings][futurewarning] Using this config file path is deprecated
```
- Replace all deprecation warning log messages with `FutureWarning`s
- Fix deprecated session options in tests
- Update and fix tests
@bastimeyer
Copy link
Member Author

Rebased to master and added StreamlinkWarning, which suppresses the origin of the warning, as it's not needed for Streamlink-related warnings/deprecations which get logged.

This should cover everything now.

- Add `StreamlinkWarning` and `StreamlinkDeprecationWarning`
  and replace `FutureWarning`s
- Don't include the warning's origin in the warning logger if it's a
  subclass of `StreamlinkWarning`
- Update tests
@gravyboat gravyboat merged commit 777d1be into streamlink:master Jan 12, 2023
@bastimeyer bastimeyer deleted the logger/warnings branch January 13, 2023 01:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants