[go: up one dir, main page]

ftail 0.3.1

Ftail is simple logging implementation for the `log` crate with support for multiple channels.
Documentation
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
//! # Ftail
//!
//! **Ftail** is a simple logging implementation for the [`log`](https://crates.io/crates/log) crate with support for multiple channels, including console output, files, and custom loggers.
//!
//! ---
//!
//! ## Features
//!
//! * Multiple logging channels: console, formatted console, single file, daily file, and custom
//! * Level and target filtering
//! * Optional timestamp formatting and timezone support
//! * Automatic log rotation and retention
//! * Use `RUST_LOG` environment variable for dynamic log level control (use '_env_level' postfix)
//!
//! ---
//!
//! ## Quick Start
//!
//! Add Ftail to your `Cargo.toml`:
//!
//! ```toml
//! [dependencies]
//! ftail = "0.3"
//! ```
//!
//! Initialize Ftail in your `main.rs` or `lib.rs`:
//!
//! ```rust
//! use ftail::Ftail;
//! use log::LevelFilter;
//!
//! fn main() -> Result<(), Box<dyn std::error::Error>> {
//!     Ftail::new()
//!         .console(LevelFilter::Info)        // log to console
//!         .daily_file("logs", LevelFilter::Error) // log errors to daily files
//!         .init()?; // initialize logger
//!
//!     log::info!("Hello, Ftail!");
//!     log::error!("This is an error message");
//!     Ok(())
//! }
//! ```
//!
//! ---
//!
//! ## Configuration Options
//!
//! | Option               | Description                      | Notes                         |
//! | -------------------- | -------------------------------- | ----------------------------- |
//! | `.datetime_format()` | Set the datetime format          | Ex: `"%Y-%m-%d %H:%M:%S%.3f"` |
//! | `.timezone()`        | Set the timezone                 | Requires `timezone` feature   |
//! | `.max_file_size()`   | Maximum file size in MB          | Older logs renamed `.old{N}`  |
//! | `.retention_days()`  | Number of days to keep log files | Daily file only               |
//! | `.filter_levels()`   | Log only the specified levels    | `Vec<Level>`                  |
//! | `.filter_targets()`  | Log only the specified targets   | `Vec<&str>`                   |
//!
//! ---
//!
//! ## Channels
//!
//! ### Console
//!
//! Logs to the standard output without formatting.
//!
//! ```rust
//! Ftail::new()
//!     .console(LevelFilter::Trace)
//!     .init()?;
//! ```
//!
//! **Output:**
//!
//! ```text
//! 2024-09-13 17:35:18 TRACE console This is a trace message
//! 2024-09-13 17:35:18 DEBUG console This is a debug message
//! 2024-09-13 17:35:18 INFO foo bar
//! 2024-09-13 17:35:18 WARN console This is a warning message
//! 2024-09-13 17:35:18 ERROR console This is an error message
//! ```
//!
//! ---
//!
//! ### Formatted Console
//!
//! Logs with formatted and colored output.
//!
//! ```rust
//! Ftail::new()
//!     .formatted_console(LevelFilter::Trace)
//!     .init()?;
//! ```
//!
//! **Output:**
//!
//! ```text
//! 2024-09-13 17:35:37 · TRACE
//! This is a trace message
//! examples/formatted_console/src/main.rs:9
//!
//! 2024-09-13 17:35:37 · DEBUG
//! This is a debug message
//! examples/formatted_console/src/main.rs:11
//! ```
//!
//! ---
//!
//! ### Single File
//!
//! Logs to a single file (e.g., `logs/demo.log`).
//!
//! ```rust
//! Ftail::new()
//!     .single_file(Path::new("logs/demo.log"), true, LevelFilter::Trace)
//!     .init()?;
//! ```
//!
//! * `append = true` keeps existing logs, `false` overwrites.
//!
//! ---
//!
//! ### Daily File
//!
//! Logs to daily files in a directory (e.g., `logs/2025-09-25.log`).
//!
//! ```rust
//! Ftail::new()
//!     .daily_file(Path::new("logs"), LevelFilter::Trace)
//!     .init()?;
//! ```
//!
//! * Automatically rotates files per day.
//!
//! ---
//!
//! ### Custom Channel
//!
//! Create your own logger by implementing the `log::Log` trait:
//!
//! ```rust
//! Ftail::new()
//!     .custom(
//!         |config| Box::new(CustomLogger { config }) as Box<dyn Log + Send + Sync>,
//!         LevelFilter::Debug,
//!     )
//!     .datetime_format("%H:%M:%S%.3f")
//!     .init()?;
//!
//! struct CustomLogger {
//!     config: ftail::Config,
//! }
//!
//! impl log::Log for CustomLogger {
//!     fn enabled(&self, metadata: &log::Metadata) -> bool {
//!         metadata.level() <= self.config.level_filter
//!     }
//!
//!     fn log(&self, record: &log::Record) {
//!         if !self.enabled(record.metadata()) {
//!             return;
//!         }
//!         let time = chrono::Local::now()
//!             .format(&self.config.datetime_format)
//!             .to_string();
//!         println!("[{}] {}: {}", time, record.level(), record.args());
//!     }
//!
//!     fn flush(&self) {}
//! }
//! ```
//!
//! **Output:**
//!
//! ```text
//! 19:37:22.402 [DEBUG] This is a debug message
//! 19:37:22.403 [INFO] bar
//! 19:37:22.403 [WARN] This is a warning message
//! 19:37:22.403 [ERROR] This is an error message
//! ```
//!
//! ---
//!
//! ## Tips
//!
//! * Use `.console(LevelFilter::Debug)` for development.
//! * Use `.daily_file()` in production to organize logs by date.
//! * Combine `.max_file_size()` and `.retention_days()` to prevent disk bloat.
//! * Only enable the channels you need to reduce overhead by disabling default features in `Cargo.toml`.

#[cfg(any(
    feature = "console",
    feature = "formatted_console",
    feature = "file_channels"
))]
use crate::helpers::get_env_log_level;
#[cfg(feature = "console")]
use channels::console::ConsoleLogger;
#[cfg(feature = "daily_file")]
use channels::daily_file::DailyFileLogger;
#[cfg(feature = "formatted_console")]
use channels::formatted_console::FormattedConsoleLogger;
#[cfg(feature = "single_file")]
use channels::single_file::SingleFileLogger;
#[cfg(feature = "timezone")]
pub use chrono_tz::Tz;
use error::FtailError;
use log::{Level, LevelFilter, Log};
#[cfg(feature = "file_channels")]
use std::path::Path;

/// Module containing the ANSI escape codes.
pub mod ansi_escape;
/// Module containing the channels.
pub mod channels;
/// Module containing the error type.
pub mod error;
mod formatters;
mod helpers;
#[cfg(test)]
mod tests;
mod writer;

/// The main struct for configuring the logger.
pub struct Ftail {
    channels: Vec<LogChannel>,
    initialized_channels: Vec<InitializedLogChannel>,
    config: Config,
}

unsafe impl Send for Ftail {}
unsafe impl Sync for Ftail {}

pub(crate) struct LogChannel {
    constructor: Box<dyn Fn(Config) -> Box<dyn Log + Send + Sync>>,
    level: log::LevelFilter,
}

pub(crate) struct InitializedLogChannel {
    channel: Box<dyn Log + Send + Sync>,
}

/// The configuration struct for the logger. Required for custom channels.
#[derive(Clone)]
pub struct Config {
    pub level_filter: LevelFilter,
    pub datetime_format: String,
    #[cfg(feature = "timezone")]
    pub timezone: chrono_tz::Tz,
    pub max_file_size: Option<u64>,
    pub retention_days: Option<u64>,
    pub levels: Option<Vec<Level>>,
    pub targets: Option<Vec<String>>,
}

impl Ftail {
    /// Create a new instance of `Ftail`.
    pub fn new() -> Self {
        Self {
            channels: Vec::new(),
            initialized_channels: Vec::new(),
            config: Config::new(),
        }
    }

    #[cfg(feature = "timezone")]
    /// Set the timezone for the logger.
    pub fn timezone(mut self, timezone: chrono_tz::Tz) -> Self {
        self.config.timezone = timezone;

        self
    }

    /// Set the datetime format for the logger.
    pub fn datetime_format(mut self, datetime_format: &str) -> Self {
        self.config.datetime_format = datetime_format.to_string();

        self
    }

    /// Set the maximum file size for the logger.
    pub fn max_file_size(mut self, max_file_size_in_mb: u64) -> Self {
        self.config.max_file_size = Some(max_file_size_in_mb * 1024 * 1024);

        self
    }

    /// Set the retention days for the logger (daily file logger only).
    pub fn retention_days(mut self, retention_days: u64) -> Self {
        self.config.retention_days = Some(retention_days);

        self
    }

    /// Only log messages with the specified levels. The default is to log all levels.
    pub fn filter_levels(mut self, levels: Vec<Level>) -> Self {
        self.config.levels = Some(levels);

        self
    }

    /// Only log messages with the specified targets. The default is to log all targets.
    pub fn filter_targets(mut self, targets: Vec<&str>) -> Self {
        self.config.targets = Some(targets.iter().map(|s| s.to_string()).collect());

        self
    }

    fn add_channel<F>(mut self, constructor: F, level: log::LevelFilter) -> Self
    where
        F: Fn(Config) -> Box<dyn Log + Send + Sync> + 'static,
    {
        self.channels.push(LogChannel::new(constructor, level));
        self
    }

    /// Add a channel that logs messages to the console.
    #[cfg(feature = "console")]
    pub fn console(self, level: log::LevelFilter) -> Self {
        let constructor =
            |config: Config| Box::new(ConsoleLogger::new(config)) as Box<dyn Log + Send + Sync>;

        self.add_channel(constructor, level)
    }

    // Add a channel that logs messages to the console with the log level set from the environment variable `RUST_LOG`.
    #[cfg(feature = "console")]
    pub fn console_env_level(self) -> Self {
        self.console(get_env_log_level())
    }

    /// Add a channel that logs formatted messages to the console.
    #[cfg(feature = "formatted_console")]
    pub fn formatted_console(self, level: log::LevelFilter) -> Self {
        let constructor = |config: Config| {
            Box::new(FormattedConsoleLogger::new(config)) as Box<dyn Log + Send + Sync>
        };

        self.add_channel(constructor, level)
    }

    /// Add a channel that logs formatted messages to the console with the log level set from the environment variable `RUST_LOG`.
    #[cfg(feature = "formatted_console")]
    pub fn formatted_console_env_level(self) -> Self {
        self.formatted_console(get_env_log_level())
    }

    /// Add a channel that logs messages to a single file.
    #[cfg(feature = "single_file")]
    pub fn single_file(self, path: &Path, append: bool, level: log::LevelFilter) -> Self {
        let path = path.to_owned();

        let constructor = move |config: Config| {
            Box::new(SingleFileLogger::new(&path, append, config).unwrap())
                as Box<dyn Log + Send + Sync>
        };

        self.add_channel(constructor, level)
    }

    /// Add a channel that logs messages to a single file with the log level set from the environment variable `RUST_LOG`.
    #[cfg(feature = "single_file")]
    pub fn single_file_env_level(self, path: &Path, append: bool) -> Self {
        self.single_file(path, append, get_env_log_level())
    }

    /// Add a channel that logs messages to a daily log file.
    #[cfg(feature = "daily_file")]
    pub fn daily_file(self, path: &Path, level: log::LevelFilter) -> Self {
        let path = path.to_owned();

        let constructor = move |config: Config| {
            Box::new(DailyFileLogger::new(&path, config).unwrap()) as Box<dyn Log + Send + Sync>
        };

        self.add_channel(constructor, level)
    }

    /// Add a channel that logs messages to a daily log file with the log level set from the environment variable `RUST_LOG`.
    #[cfg(feature = "daily_file")]
    pub fn daily_file_env_level(self, path: &Path) -> Self {
        self.daily_file(path, get_env_log_level())
    }

    /// Add a custom channel.
    pub fn custom<F>(self, constructor: F, level: log::LevelFilter) -> Self
    where
        F: Fn(Config) -> Box<dyn Log + Send + Sync> + 'static,
    {
        self.add_channel(constructor, level)
    }

    /// Initialize the logger.
    pub fn init(mut self) -> Result<(), FtailError> {
        if self.channels.is_empty() {
            return Err(FtailError::NoChannelsError);
        }

        let channels = std::mem::take(&mut self.channels);

        self.initialized_channels = channels
            .into_iter()
            .map(|channel| {
                let mut config = self.config.clone();
                config.level_filter = channel.level;

                channel.init(config)
            })
            .collect();

        log::set_max_level(log::LevelFilter::Trace);
        log::set_boxed_logger(Box::new(self)).map_err(FtailError::SetLoggerError)
    }
}

impl LogChannel {
    fn new<F>(constructor: F, level: log::LevelFilter) -> Self
    where
        F: Fn(Config) -> Box<dyn Log + Send + Sync> + 'static,
    {
        Self {
            constructor: Box::new(constructor),
            level,
        }
    }

    fn init(self, config: Config) -> InitializedLogChannel {
        InitializedLogChannel {
            channel: (self.constructor)(config),
        }
    }
}

impl Log for Ftail {
    fn enabled(&self, metadata: &log::Metadata) -> bool {
        if self.config.levels.is_some()
            && !self
                .config
                .levels
                .as_ref()
                .unwrap()
                .contains(&metadata.level())
        {
            return false;
        }

        if self.config.targets.is_some()
            && !self
                .config
                .targets
                .as_ref()
                .unwrap()
                .contains(&metadata.target().to_string())
        {
            return false;
        }

        true
    }

    fn log(&self, record: &log::Record) {
        if !self.enabled(record.metadata()) {
            return;
        }

        for channel in &self.initialized_channels {
            channel.channel.log(record);
        }
    }

    fn flush(&self) {
        for channel in &self.initialized_channels {
            channel.channel.flush();
        }
    }
}

impl Default for Ftail {
    fn default() -> Self {
        Self::new()
    }
}