webdav_server/logger/loggable.rs
1use crate::logger::{Level, Module};
2
3/// Allows an error type to declare its own log severity and module routing.
4///
5/// Implementing this trait on a domain error type decentralizes the decision of how
6/// to categorize an error for telemetry purposes. The error itself knows best whether
7/// it represents a minor client mistake (a `Warning`) or a critical infrastructure
8/// failure (a `Fatal`). This avoids the need for a centralized `match` statement in
9/// the middleware every time a new error variant is added.
10///
11/// ## Implementation contract
12///
13/// Implementations must be pure and infallible. They inspect `&self` and return a
14/// static classification. They must not allocate or perform any I/O.
15///
16/// ## Usage
17///
18/// This trait is consumed by [`crate::Error::into_response`], which calls `log_level`
19/// and `log_module` on the top-level error *before* consuming it (since `into_response`
20/// takes `self` by value). The resulting values are packed into a [`crate::logger::Entry`]
21/// and placed into the response extension backpack for the logging middleware to pick up.
22///
23/// ```rust
24/// use webdav_server::logger::{Level, Module, Loggable};
25///
26/// struct MyError;
27///
28/// impl Loggable for MyError {
29/// fn log_level(&self) -> Level {
30/// Level::Error
31/// }
32///
33/// fn log_module(&self) -> Module {
34/// Module::Api
35/// }
36/// }
37/// ```
38pub trait Loggable {
39 /// Returns the severity level that should be assigned to this error in the log.
40 fn log_level(&self) -> Level;
41
42 /// Returns the server module responsible for this error.
43 ///
44 /// This value is used by the admin CLI to filter and group entries. Most domain
45 /// errors return a fixed module (e.g., `Module::Storage` for all `storage::Error`
46 /// variants), while the top-level `crate::Error` delegates to the inner error.
47 fn log_module(&self) -> Module;
48}