Log Crate

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

Usage

To use this crate in your project, add it as a dependency in your Cargo.toml file:

env [dependencies] logger-rust = "0.1.0" ```` Then, import the crate: rust

[macro_use]

extern crate loggerrust; use loggerrust::{setloglevel, LogLevel}; You can now use the `log_error!`, `log_warn!`, `log_info!`, and `log_debug!` macros to log messages of different types: rust fn main() { logerror!("An error occurred: {}", "Something went wrong"); logwarn!("A warning occurred: {}", "Something might be wrong"); loginfo!("An info message: {}", "Something happened"); logdebug!("A debug message: {}", "Something happened in detail"); } By default, log messages are printed to the console. You can use the `set_log_level` function to specify where log messages should be written: rust use logcrate::{setlog_level, LogLevel};

fn main() { setloglevel(LogLevel::Both);

// ...

} ```

The set_log_level function takes a LogLevel as an argument. You can pass one of the following variants to specify where log messages should be written:

Examples

Here’s an example that shows how to use this crate in a Rust project: ```rust

[macro_use]

extern crate loggerrust; use logcrate::{setloglevel, LogLevel};

fn main() { setloglevel(LogLevel::Both);

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");

} 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 ``` This code sets the log level to LogLevel::Both, which means that log messages will be printed to the console and written to a file. It then uses the logging macros to log messages of different types.