speak_easy/
lib.rs

1#![allow(clippy::style)]
2
3//! # speak-easy
4//!
5//! Logging functionalities with different levels and rotation options built on top of tracing and compatible with of tokio-rs
6//!
7//! ## Features
8//!| Feature                          | Status |
9//!|----------------------------------|--------|
10//!| Console logging                  | ✅      |
11//!| File logging                     | ✅      |
12//!| Rotation options                 | ✅      |
13//!| Dispatch logs to multiple destinations | ❌  |
14//!
15//! ## Modules
16//!
17//! - `formatter`: This module contains the functionality for formatting the logs.
18//! - `log_levels`: This module defines the different levels of logging.
19//! - `speak_easy`: This module contains the functionality for the SpeakEasy logging system.
20//!
21//! ## Enums
22//!
23//! - `Rotation`: Defines the rotation options for the logs. Options are `Minutely`, `Hourly`, `Daily`, and `Never`.
24//!
25//! ## Structs
26//!
27//! - `SpeakConfig`: Defines the configuration for the SpeakEasy logging system. It includes the rotation interval, the directory path for the logs, and the prefix for the log files.
28//!
29//! ## Usage
30//!
31//! Add the following to your `Cargo.toml`:
32//!
33//! ```toml
34//! [dependencies]
35//! speak-easy = { version = "0.1" }
36//! tokio = { features = ["macros", "rt-multi-thread"], version = "1.37.0" }
37//! ```
38//! 
39//! **Note**
40//! 
41//! If you want to use Speak-Easy without tokio, you must disable default features:
42//! 
43//! ```toml
44//! [dependencies]
45//! speak-easy = { version = "0.1", default-features = false }
46//! ```
47//!
48//! Your main function should look like this:
49//!
50//! ```rust
51//! #[tokio::main]
52//! async fn main() {
53//!    // Your code here
54//! }
55//! ```
56//!
57//! Then add this to your crate root:
58//!
59//! ```rust
60//! use speak_easy::{debug, error, info, trace, warn};
61//! use speak_easy::{speak_easy::SpeakEasy, Level, Rotation, SpeakConfig};
62//! ```
63//!
64//! You can then use the logging functionality as follows:
65//!
66//! ```rust
67//! # use speak_easy::{debug, error, info, trace, warn};
68//! # use speak_easy::{speak_easy::SpeakEasy, Level, Rotation, SpeakConfig};
69//! let speak_config = SpeakConfig::new(Rotation::Minutely, "./logs".to_string(), "my_log".to_string());
70//! SpeakEasy::init(
71//!     Level::INFO,
72//!     Some(speak_config),
73//! );
74//!
75//! info!("This is an info log");
76//! debug!("This is a debug log");
77//! warn!("This is a warning log");
78//! error!("This is an error log");
79//! trace!("This is a trace log");
80//! ```
81//!
82//! This will create logs with different levels and rotate them hourly.
83//!
84//!
85//! ### With Cleanup
86//!
87//! You can also set up log cleanup with the `with_cleanup` method:
88//!
89//! ```rust
90//! # use speak_easy::{debug, error, info, trace, warn};
91//! # use speak_easy::{speak_easy::SpeakEasy, Level, Rotation, SpeakConfig};
92//! let speak_config = SpeakConfig::new(Rotation::Minutely, "./logs".to_string(), "my_log".to_string())
93//!    .with_cleanup(24 * 60 * 60, 5);
94//! SpeakEasy::init(
95//!    Level::INFO,
96//!   Some(speak_config),
97//! );
98//! ```
99//!
100//! This will create logs with different levels, rotate them minutely, and clean up the logs every 24 hours, keeping the last 5 logs.
101//!
102//! ## License
103//!
104//! This project is licensed under the MIT license.
105//!
106//! ## Contributing
107//! Feel free to open issues and send PRs. We will evaluate them together in the comment section.
108//!
109//!
110
111mod formatter;
112mod processor;
113pub use tracing::{debug, error, info, trace, warn, Level};
114pub mod speak_easy;
115
116#[derive(Clone, Debug)]
117pub enum Rotation {
118    Minutely,
119    Hourly,
120    Daily,
121    Never,
122}
123
124#[derive(Clone, Debug)]
125pub struct SpeakConfig {
126    pub interval: Rotation,
127    pub directory_path: String,
128    pub prefix: String,
129    pub cleanup: bool,
130    pub cleanup_interval: u64,
131    pub keep_last: usize,
132}
133
134impl Default for SpeakConfig {
135    fn default() -> Self {
136        Self {
137            interval: Rotation::Never,
138            directory_path: "./logs".to_string(),
139            prefix: "log".to_string(),
140            cleanup: false,
141            cleanup_interval: 24 * 60 * 60, // 24 hours
142            keep_last: 5,
143        }
144    }
145}
146
147impl SpeakConfig {
148    /// Creates a new `SpeakConfig` with the specified rotation interval, directory path, and log file prefix.
149    ///
150    /// # Arguments
151    ///
152    /// * `interval` - The rotation interval for the logs. Options are `Minutely`, `Hourly`, `Daily`, and `Never`.
153    /// * `directory_path` - The directory path where the log files will be stored.
154    /// * `prefix` - The prefix for the log files.
155    ///
156    /// # Example
157    ///
158    /// ```rust
159    /// use speak_easy::{Rotation, SpeakConfig};
160    ///
161    /// let speak_config = SpeakConfig::new(Rotation::Minutely, "./logs".to_string(), "my_log".to_string());
162    /// ```
163    pub fn new(interval: Rotation, directory_path: String, prefix: String) -> Self {
164        Self {
165            interval,
166            directory_path,
167            prefix,
168            cleanup: SpeakConfig::default().cleanup,
169            cleanup_interval: SpeakConfig::default().cleanup_interval,
170            keep_last: SpeakConfig::default().keep_last,
171        }
172    }
173
174    /// Sets the cleanup options for the log files.
175    ///
176    /// # Arguments
177    ///
178    /// * `cleanup_interval` - The interval (in seconds) at which the log files should be cleaned up.
179    /// * `keep_last` - The number of log files to keep.
180    ///
181    /// # Example
182    ///
183    /// ```rust
184    /// use speak_easy::{Rotation, SpeakConfig};
185    ///
186    /// let speak_config = SpeakConfig::new(Rotation::Minutely, "./logs".to_string(), "my_log".to_string())
187    ///     .with_cleanup(24 * 60 * 60, 5);
188    /// ```
189    pub fn with_cleanup(mut self, cleanup_interval: u64, keep_last: usize) -> Self {
190        self.cleanup = true;
191        self.cleanup_interval = cleanup_interval;
192        self.keep_last = keep_last;
193        self
194    }
195}
196
197impl SpeakConfig {}