Expand description
§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:
[dependencies]
logger-rust = "0.2.12"
Then, import the crate:
use logger_rust::*;
If you want to use them separately:
// 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:
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:
use logger_rust::*;
error(¤t_time(), "An error message");
warn(¤t_time(), "A warning message");
info(¤t_time(), "An info message");
Output:
- 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.
- Create a
class
named as function that you needed (e.g.trace
):
use logger_rust::*;
pub fn trace(now: &str, message: &str) {
log_message("TRACE", now, message);
}
Re-exports§
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;
pub use crate::set_log::set_log_path;
pub use crate::config::LogVariables;
pub use crate::config::LogVariablesImpl;
pub use crate::config::LogLevel;
pub use crate::set_log::*;
pub use crate::log_rotator::*;
pub use crate::tracer_config::*;
Modules§
- config
- log_
file - log_
rotator - set_log
- time
- tracer_
config - It is not already used, it just an template for future using in log_trace! macro.
Macros§
- log_
debug - Macro rules - log_debug!
- log_
error - Macro rules - log_error!
- log_
info - Macro rules - log_info!
- log_
trace log_trace!
is a macro that logs trace-level messages.- log_
warn - Macro rules - log_warn!