Target-has-prefix
Create a RecordPredicate that returns true when a record target starts with a given prefix. This helper is commonly used with with_filter(...), filter_sink(...), all_of(...), and routing logic.
Interface
pub fn target_has_prefix(prefix : String) -> RecordPredicate {}input
prefix : String- Prefix expected at the start ofrec.target.
output
RecordPredicate- Predicate that matches records whose target starts with the given prefix.
Explanation
Detailed rules explaining key parameters and behaviors
- Matching is based on
String::has_prefix(...)over the full target string. - This helper does not modify records; it only returns a reusable predicate.
- Prefix-based matching is especially useful when using hierarchical targets such as
app.worker.job. - It composes cleanly with
all_of(...),any_of(...), andnot_(...).
How to Use
Here are some specific examples provided.
When Filter A Target Namespace
When a logger should keep one target subtree:
let logger = Logger::new(console_sink(), target="app")
.with_filter(target_has_prefix("app.worker"))In this example, only records under app.worker... remain visible.
And child targets continue to compose naturally.
When Combine With Level Rules
When both namespace and severity matter:
let predicate = all_of([
target_has_prefix("service.api"),
level_at_least(Level::Warn),
])In this example, the predicate stays reusable across multiple loggers or sinks.
Error Case
e.g.:
If
prefixis empty, the predicate effectively matches every target because every string starts with an empty prefix.If no target matches the prefix, the predicate simply returns
falsefor those records.
Notes
This helper is most useful when targets are structured hierarchically.
Prefer this helper over ad hoc inline prefix logic for readability.