ralph_workflow/logger/loggable.rs
1//! Loggable trait for logger output destinations.
2//!
3//! This trait allows loggers to write to different destinations
4//! (console, files, test collectors) without hardcoding the specific destination.
5
6use crate::logger::Colors;
7
8/// Trait for logger output destinations.
9///
10/// This trait allows loggers to write to different destinations
11/// (console, files, test collectors) without hardcoding the specific destination.
12/// This makes loggers testable by allowing output capture.
13///
14/// This trait mirrors the `Printable` trait pattern used for printers,
15/// providing a unified interface for both production and test loggers.
16pub trait Loggable {
17 /// Write a log message to the sink.
18 ///
19 /// This is the core logging method that all loggers must implement.
20 /// For `Logger`, this writes to the configured log file (if any).
21 /// For `TestLogger`, this captures the message in memory for testing.
22 fn log(&self, msg: &str);
23
24 /// Log an informational message.
25 ///
26 /// Default implementation formats the message with [INFO] prefix
27 /// and delegates to the `log` method.
28 fn info(&self, msg: &str) {
29 self.log(&format!("[INFO] {msg}"));
30 }
31
32 /// Log a success message.
33 ///
34 /// Default implementation formats the message with `[OK]` prefix
35 /// and delegates to the `log` method.
36 fn success(&self, msg: &str) {
37 self.log(&format!("[OK] {msg}"));
38 }
39
40 /// Log a warning message.
41 ///
42 /// Default implementation formats the message with [WARN] prefix
43 /// and delegates to the `log` method.
44 fn warn(&self, msg: &str) {
45 self.log(&format!("[WARN] {msg}"));
46 }
47
48 /// Log an error message.
49 ///
50 /// Default implementation formats the message with `[ERROR]` prefix
51 /// and delegates to the `log` method.
52 fn error(&self, msg: &str) {
53 self.log(&format!("[ERROR] {msg}"));
54 }
55
56 /// Print a section header with box drawing.
57 ///
58 /// Default implementation does nothing (test loggers may not need headers).
59 /// Production loggers override this to display styled headers.
60 fn header(&self, _title: &str, _color_fn: fn(Colors) -> &'static str) {
61 // Default: no-op for test loggers
62 }
63}