1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
//! Small, composable backend initialization for [`tracing`].
//!
//! `piquel-log` is intended for applications that want a straightforward
//! way to install a `tracing` backend without exposing a large custom API.
//! The crate enables console output by default, allows the console sink to be
//! disabled, and can optionally support file output and `log` crate
//! interoperability behind Cargo features.
//!
//! # Quick start
//!
//! ```rust
//! use piquel_log::Logger;
//!
//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
//! Logger::new().init()?;
//! tracing::info!("hello from tracing");
//! # Ok(())
//! # }
//! ```
//!
//! # Disable console output
//!
//! ```rust
//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
//! use piquel_log::Logger;
//!
//! Logger::new().with_console(false).init()?;
//! # Ok(())
//! # }
//! ```
//!
//! # Existing subscriber stacks
//!
//! If your application already builds a `tracing_subscriber` registry, use
//! [`Logger::build`] and attach the returned [`BackendLayer`] yourself.
//!
//! ```rust
//! use piquel_log::Logger;
//! use tracing_subscriber::{filter::LevelFilter, prelude::*, Registry};
//!
//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
//! let backend = Logger::new().build()?;
//! let subscriber = Registry::default().with(LevelFilter::INFO).with(backend);
//! let _guard = tracing::subscriber::set_default(subscriber);
//! tracing::info!("hello from a custom stack");
//! # Ok(())
//! # }
//! ```
//!
//! # `log` interoperability
//!
//! When the `log` feature is enabled, [`Logger::with_log_bridge`] can install
//! `tracing_log::LogTracer` during [`Logger::init`] so that `log` records are
//! re-emitted as `tracing` events.
//!
//! ```rust
//! # #[cfg(feature = "log")]
//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
//! use piquel_log::Logger;
//!
//! Logger::new().with_log_bridge(true).init()?;
//! log::warn!("bridged from log");
//! # Ok(())
//! # }
//! # #[cfg(not(feature = "log"))]
//! # fn main() {}
//! ```
//!
//! # Feature matrix
//!
//! - default: console backend only
//! - `Logger::with_console(false)`: disable the console sink
//! - `file`: configurable file output
//! - `Logger::add_file_backend(...)`: add a file backend at runtime
//! - `log`: explicit `log` to `tracing` bridge during `init`
//! - `full`: enables `file` and `log`
//!
//! # Non-goals for v0.1
//!
//! - query APIs
//! - target allowlists or message filters
//! - file rotation or retention policies
//! - exposing individual internal sink/layer types
//!
//! # Runtime backend updates
//!
//! A [`Logger`] can keep the same backend layer attached while adding new
//! sinks later. For example, a file backend can be added after startup:
//!
//! ```rust
//! # #[cfg(feature = "file")]
//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
//! use piquel_log::{FileConfig, Logger};
//!
//! let logger = Logger::new();
//! logger.init()?;
//! logger.add_file_backend(FileConfig::new("logs"))?;
//! tracing::info!("also written to the file backend");
//! # Ok(())
//! # }
//! # #[cfg(not(feature = "file"))]
//! # fn main() {}
//! ```
use fmt;
pub use crateLogger;
pub use crate;
pub use crateBackendLayer;
pub use crateFileConfig;
/// Severity level of a log entry.
///
/// Variants are ordered by severity so that comparisons work intuitively:
/// `Error` is the **most** severe and `Trace` is the **least**.
///
/// ```rust
/// use piquel_log::LogLevel;
///
/// assert!(LogLevel::Error < LogLevel::Warn);
/// assert!(LogLevel::Warn < LogLevel::Info);
/// assert!(LogLevel::Info < LogLevel::Debug);
/// assert!(LogLevel::Debug < LogLevel::Trace);
/// ```
///
/// This ordering is what powers level-threshold filtering: passing
/// `LogLevel::Warn` keeps `Error` and `Warn` (both ≤ `Warn` in severity).