timber_rust/logger/
silent.rs

1// SPDX-License-Identifier: Apache-2.0
2// Copyright 2026 Dante Doménech Martinez dante19031999@gmail.com
3
4use crate::{LoggerImpl, Message, LoggerStatus};
5use std::any::Any;
6
7/// A no-op (no-operation) implementation of [`LoggerImpl`].
8///
9/// [`SilentLogger`][`Silent`] satisfies the logger interface while effectively discarding all
10/// log messages. This is useful for:
11/// - **Testing**: Disabling log output during unit tests.
12/// - **Defaults**: Providing a safe, "do-nothing" fallback logger.
13/// - **Performance**: Avoiding conditional "if logger.is_some()" checks in hot paths.
14pub struct Silent;
15
16impl LoggerImpl for Silent {
17    /// Always returns [`LoggerStatus::Running`].
18    ///
19    /// Since the [`SilentLogger`][`Silent`] cannot fail in its mission to do nothing, it
20    /// is perpetually in a healthy state.
21    fn status(&self) -> LoggerStatus {
22        LoggerStatus::Running
23    }
24
25    /// Discards the incoming [`Message`] immediately.
26    ///
27    /// This method is essentially a "black hole." The underscore in `_message`
28    /// ensures the compiler does not warn about the unused variable while
29    /// the message is dropped at the end of the scope.
30    fn log(&self, _message: Message) {}
31
32    /// Returns the [`SilentLogger`][`Silent`] instance as a [dyn Any].
33    fn as_any(&self) -> &dyn Any {
34        self
35    }
36}
37
38impl Silent {
39    /// Creates a new, heap-allocated instance of [`SilentLogger`][`Silent`].
40    ///
41    /// This is a convenience constructor that returns the logger inside a [`Box`],
42    /// making it directly compatible with [`Logger::new()`][crate::Logger::new()].
43    ///
44    /// # Example
45    /// ```
46    /// # use timber_rust::Logger;
47    /// # use timber_rust::SilentLogger;
48    /// let logger = Logger::new(SilentLogger::new());
49    /// ```
50    pub fn new() -> Box<Self> {
51        Box::new(Silent {})
52    }
53}