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
//! # Logger macro
//! This crate provides a simple and flexible logging system for Rust projects. It allows you to log messages of different types (error, warn, info, debug) using macros and choose whether to log messages to the console, a file, or both.
//!
//! # Features
//! - Log messages of different types (error, warn, info, debug) using macros
//! - Choose whether to log messages to the console, a file, or both
//! - Set the log level at runtime
//! - Automatically create and append to log files based on the current date
//! - Handle errors when writing to log files
//!
//! ## Usage
//! To use this crate in your project, add it as a dependency in your `Cargo.toml` file:
//!
//! ```env
//! [dependencies]
//! logger-rust = "0.2.10"
//! ```
//! Then, import the crate:
//! ```rust
//! use logger_rust::*;
//! ```
//! If you want to use them separately:
//! ```rust
//! // all possible crates
//! use logger_rust::log_debug;
//! use logger_rust::log_info;
//! use logger_rust::log_warn;
//! use logger_rust::log_error;
//! use logger_rust::set_log_path;
//! use logger_rust::set_log_level;
//! use logger_rust::LOG_PATH;
//! use logger_rust::LogLevel;
//! use logger_rust::current_time;
//! ```
//!
//! You can now use the `log_error!`, `log_warn!`, `log_info!`, and `log_debug!` macros to log messages of different types:
//!
//! ```rust
//! use logger_rust::*;
//!
//! fn main() {
//! log_error!("An error occurred: {}", "Something went wrong");
//! log_warn!("A warning occurred: {}", "Something might be wrong");
//! log_info!("An info message: {}", "Something happened");
//! log_debug!("A debug message: {}", "Something happened in detail");
//! }
//! ```
//!
//!
//! Also you can use this:
//! ```rust
//! use logger_rust::*;
//!
//! error(¤t_time(), "An error message");
//! warn(¤t_time(), "A warning message");
//! info(¤t_time(), "An info message");
//! ```
//! Output:
//! ```diff
//! - 2023-06-05 12:23:25 [ERROR] An error occurred: Something went wrong
//! - 2023-06-05 12:23:25 [WARN] A warning occurred: Something might be wrong
//! A warning occurred: Something might be wrong
//! + 2023-06-05 12:23:25 [INFO] An info message: Something happened
//! An info message: Something happened
//! + 2023-06-05 12:23:25 [DEBUG] A debug message: Something happened in detail
//! A debug message: Something happened in detail
//! ```
//!
//! # Custom logging
//! After 0.1.2 version, you can create a custom logging function with macros.
//!
//! 1. Create a `class` named as function that you needed (e.g. `trace`):
//!
//! ```rust
//! use logger_rust::*;
//!
//! pub fn trace(now: &str, message: &str) {
//! log_message("TRACE", now, message);
//! }
//! ```
//!
//!
//!
pub mod time;
pub mod config;
pub mod set_log;
pub mod log_file;
pub mod log_rotator;
pub use crate::set_log::*;
pub use crate::log_rotator::*;
pub use crate::config::LOG_PATH;
pub use crate::time::current_time;
pub use crate::log_file::log_message;
pub use crate::set_log::{set_log_level, set_log_path};
pub use crate::config::{LogVariables, LogVariablesImpl, LogLevel};
pub fn error(now: &str, message: &str) {
//! # Error
//! Outputs an error message via `log_error!` macros.
//! You can also use just an `error` method instead, but it requires you to define any variable as `&str`
//! > E.g:
//! ```rust
//! use logger_rust::error;
//! use logger_rust::current_time;
//! error(¤t_time(), "An error message");
//! ```
log_message("ERROR", now, message);
}
pub fn warn(now: &str, message: &str) {
//! # Warn
//! Outputs an warn message via `log_warn!` macros.
//! You can also use just an `warn` method instead, but it requires you to define any variable as `&str`
//! > E.g:
//! ```rust
//! use logger_rust::warn;
//! use logger_rust::current_time;
//! warn(¤t_time(), "A warn message");
//! ```
log_message("WARN", now, message);
}
pub fn info(now: &str, message: &str) {
//! # Info
//! Outputs an info message via `log_info!` macros.
//! You can also use just an `info` method instead, but it requires you to define any variable as `&str`
//! > E.g:
//! ```rust
//! use logger_rust::info;
//! use logger_rust::current_time;
//! info(¤t_time(), "An info message");
//! ```
log_message("INFO", now, message);
}
pub fn debug(now: &str, message: &str) {
//! # Debug
//! Outputs an debug message via `log_debug` macros.
//! You can also use just an `debug` method instead, but it requires you to define any variable as `&str`
//! > E.g:
//! ```rust
//! use logger_rust::debug;
//! use logger_rust::current_time;
//! debug(¤t_time(), "A debug message");
//! ```
log_message("DEBUG", now, message);
}
pub fn trace(now: &str, message: &str) {
//! # Debug
//! Outputs an debug message via `log_debug` macros.
//! You can also use just an `debug` method instead, but it requires you to define any variable as `&str`
//! > E.g:
//! ```rust
//! use logger_rust::debug;
//! use logger_rust::current_time;
//! debug(¤t_time(), "A debug message");
//! ```
log_message("TRACE", now, message);
}
trait Loggable {
fn log_behavior(&self) -> String;
}
#[macro_export]
/// ## Macro rules - log_error!
/// The log_error macro takes any number of arguments and formats them using the format! macro.
/// It then gets the current time using the current_time function from the crate and calls the error function from the crate with the current time and formatted message.
macro_rules! log_error {
($($arg:tt)*) => {{
let now = $crate::current_time();
$crate::error(&now, &format!($($arg)*));
}}
}
#[macro_export]
/// ## Macro rules - log_warn!
/// The log_warn macro takes any number of arguments and formats them using the format! macro.
/// It then gets the current time using the current_time function from the crate and calls the warn function from the crate with the current time and formatted message.
macro_rules! log_warn {
($($arg:tt)*) => {{
let now = $crate::current_time();
$crate::warn(&now, &format!($($arg)*));
}}
}
#[macro_export]
/// ## Macro rules - log_info!
/// The log_info macro takes any number of arguments and formats them using the format! macro.
/// It then gets the current time using the current_time function from the crate and calls the info function from the crate with the current time and formatted message.
macro_rules! log_info {
($($arg:tt)*) => {{
let now = $crate::current_time();
$crate::info(&now, &format!($($arg)*));
}}
}
#[macro_export]
/// ## Macro rules - log_debug!
/// The log_debug macro takes any number of arguments and formats them using the format! macro.
/// It then gets the current time using the current_time function from the crate and calls the debug function from the crate with the current time and formatted message.
macro_rules! log_debug {
($($arg:tt)*) => {{
let now = $crate::current_time();
$crate::debug(&now, &format!($($arg)*));
}}
}
#[macro_export]
/// ## Macro rules - log_trace!
macro_rules! log_trace {
($debug_object:expr) => {{
log_trace!($debug_object, "");
}};
($debug_object:expr, $context:expr) => {{
let now = $crate::current_time();
let line = line!();
let file = file!();
let thread_id = std::thread::current().id();
let timestamp = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap()
.as_micros();
let context_str = if $context.is_empty() {
String::new()
} else {
format!(" ->> Context: {}", $context)
};
let debug_info = format!(
"\x1b[34m{}:{} - used: \x1b[32m{}\x1b[36m ->> ({:?}): \x1b[31m{:?}->> Thread ID: {:?} Timestamp: {}{}\x1b[0m ",
file, line,
stringify!($debug_object),
&$debug_object,
$debug_object,
thread_id,
timestamp,
context_str
);
$crate::log_message("TRACE", &now, &debug_info);
}};
($($arg:tt)*) => {{
let now = $crate::current_time();
$crate::log_message("TRACE", &now, &format!($($arg)*));
}}
}