đŦ A couple of functions to make logging in Rust easier.
```toml
[dependencies] rust_logging = "1.0.0" ```
```rs use rust_logging::*;
fn main() { // With default settings let default_logger = Logger::new();
// With custom settings
let settings = LoggerOptions {
error_icon: "đĨ", // the icon for errors
warn_icon: "â ī¸", // the icon for warnings
info_icon: "âšī¸", // the icon for info
success_icon: "â
", // the icon for success
icon_connector: "=>", // the connector between the icon and the message
log_file: "log.txt", // the file to log to when logging to a file
highlight: true, // highlight text after ":" in the message
..Default::default() // the default values for the non specified settings
}
let custom_logger = settings.get_logger();
} ```
This code provides the following functions:
f
before the function name to print the message to the log fileEach function takes a single parameter, which is the message to be printed.
rust
logger.error("a command failed : hello");
logger.info("executing command : hello");
logger.warn("a command is about to fail : hello");
logger.success("a command succeeded : hello");
By default, the text after the colon is highlighted.
This can be disabled by setting the highlight
field to false
inside the custom settings.
```rs let settings = LoggerOptions { highlight: false, ..Default::default() }
let logger = settings.get_logger(); ```
By default, the icon and the message are separated by an arrow ->
You can change this by setting the icon_connector
field to something else.
```rs let settings = LoggerOptions { icon_connector: "=>", ..Default::default() }
let logger = settings.get_logger(); ```
By default, the following icons are used:
| Icon | Function | | ----- | --------- | | [ x ] | error() | | [ i ] | info() | | [ v ] | success() | | [ ! ] | warn() |
You can change this by setting the following fields inside the custom settings:
```rust let settings = LoggerOptions { erroricon: "đĨ", warnicon: "â ī¸", infoicon: "âšī¸", successicon: "â ", ..Default::default() }
let logger = settings.get_logger(); ```
You can specify the log file path to write the messages to with the log_file
field.
Use the f
prefix before the function name to print the message to the log file.
```rust let settings = LoggerOptions { log_file: "myProgram.log", ..Default::default() }
let logger = settings.get_logger();
// -----------------------------
logger.fwarning("This is a : warning message"); logger.fsuccess("This is a : success message"); logger.finfo("This is an : information message"); logger.ferror("This is an : error message"); ```
You can set environment variable LOG
to print
to also log to the terminal when logging to a file.
```bash LOG=print [program]
[Terminal output] :
[ x ] -> This is an : error message
```
If you have any problem, don't hesitate to open an issue
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.