Skip to content

Remove max_level in favour of extracting it from the Filter #94

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

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

MarijnS95
Copy link
Member

Fixes #80

Documentation for log specifies that all implementations must have a way to initialize log::set_max_level() (because it otherwise defaults to Off), in our case via the with_max_level() builder - and by calling init_once(). Over time android_logger received env_filter support for much more fine-grained control over filtering, making android_logger similar to env_logger in its implementation.

With this change however, ambiguity arises when independently configuring log levels through .with_max_level() and env_filter::Filter. The former acts as an early performance save directly in log::log!() macros while the latter applies additional filtering inside android_logger. In short: log::max_level() must be at least as high as what Filter can process, otherwise messages get lost before they reach the Filter.

env_logger solved this ambiguity by hiding direct access to log::set_max_level() as an implementation detail and initializing it to the highest level that could possibly be processed by the Filter. Mimick this inside android_logger by removing the with_max_level() setter. In addition all internal is_enabled() filtering and related tests are removed because we rely on the log macros themselves to filter, and no longer track max_level inside. Keeping in mind that any user can techincally change log::set_max_level() to increase or decrease global verbosity.

The only complexity remains around __android_log_is_loggable_len() on Android 30 and above (if enabled on the crate by the user - not necessarily based on the actual minimum nor target SDK level yet!) which asks the Android system for env_filter-like per-module (tag) level overrides.

TODO

  • Make env_filter non-Optional?
    • Forward its Builder-constructors like filter_level() and filter_module(), just like env_logger.
    • Also create various constructors based on env_logger/env_filter's environment variables? These are less common to be set for Android apps.
    • Revisit the correct default. Error makes sense here.
  • Revise tests around level filters, which are now no longer implemented on the AndroidLogger level.
  • Update README and crate-documentation samples to match the new API (once finalized).

Documentation for `log` specifies that all implementations must have a
way to initialize `log::set_max_level()` (because it otherwise defaults
to `Off`), in our case via the `with_max_level()` builder - and by
calling `init_once()`.  Over time `android_logger` received `env_filter`
support for much more fine-grained control over filtering, making
`android_logger` similar to `env_logger` in its implementation.

With this change however, ambiguity arises when independently
configuring log levels through `.with_max_level()` and
`env_filter::Filter`.  The former acts as an early performance save
directly in `log::log!()` macros while the latter applies additional
filtering inside `android_logger`.  In short: `log::max_level()` must
be at least as high as what `Filter` can process, otherwise messages get
lost before they reach the `Filter`.

`env_logger` solved this ambiguity by hiding direct access to
`log::set_max_level()` as an implementation detail and initializing it
to the highest level that could possibly be processed by the `Filter`.
Mimick this inside `android_logger` by removing the `with_max_level()`
setter.  In addition all internal `is_enabled()` filtering and related
tests are removed because we rely on the `log` macros themselves to
filter, and no longer track `max_level` inside.  Keeping in mind that
any user can techincally change `log::set_max_level()` to increase or
decrease global verbosity.

The only complexity remains around `__android_log_is_loggable_len()`
on Android 30 and above (if enabled on the crate by the user - not
necessarily based on the actual minimum nor target SDK level yet!)
which asks the Android system for `env_filter`-like per-module (tag)
level overrides.
@MarijnS95
Copy link
Member Author

MarijnS95 commented Jul 4, 2025

@Nercury I'd love your input on the general direction (and specifically its justification in the description) of this PR removing max_level and the TODOs outlined, before I commit to finishing it (definitely don't need a full code-review just yet - I'll let you know by pulling it out of Draft status) 🙂

@MarijnS95 MarijnS95 requested a review from Nercury July 4, 2025 09:53
@MarijnS95 MarijnS95 force-pushed the replace-max_level-with-env_filter-max branch from 83576aa to 75643d2 Compare July 11, 2025 14:43
@MarijnS95 MarijnS95 force-pushed the replace-max_level-with-env_filter-max branch from 75643d2 to 7f89554 Compare July 11, 2025 14:49
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.

Clarify on relationship between FilterBuilder and max-level
1 participant