Crates.io | tlogger |
lib.rs | tlogger |
version | 0.1.4 |
source | src |
created_at | 2024-11-17 17:14:19.910398 |
updated_at | 2024-11-18 17:31:08.258157 |
description | A simple logging library with a neat style and customizablity. |
homepage | https://github.com/ThatOneToast/t-logger |
repository | https://github.com/ThatOneToast/t-logger |
max_upload_size | |
id | 1451412 |
size | 37,753 |
A versatile and stylish logging library for Rust applications that provides both console output and file logging capabilities with customization options.
๐ Multiple log levels with selective file logging
๐จ Rich text formatting
๐ฆ Flexible output formats
โฐ Time-based file management
๐ฏ Extensive customization options
๐ Development features
Add this to your Cargo.toml
:
[dependencies]
tlogger = "0.1.4"
Or use the cargo add
command:
cargo add tlogger
use tlogger::prelude::*;
fn main() {
// Initialize logger with hourly log files
init_logger("logs", LogInterval::OneHour).unwrap();
// Or use other intervals: ThreeHour, SixHour, NineHour, TwelveHour, OneDay
customize_colors(Colors {
info: ansi_rgb!(32, 80, 123),
debug: ansi_rgb!(60, 200, 30),
..Default::default()
});
customize_symbols(Symbols {
debug: "โ",
..Default::default()
});
customize_borders(Borders {
..Default::default()
});
info!("Server", "Starting");
success!("Login", "User {} connected", "Alice");
debug!("Processing", "Items in queue: {}", 42);
warn!("Memory", "Usage at {}%", 85);
error!("Database", "Connection failed");
info_box!("System", "Your super secure super system is starting up.");
warn_box!("Memory", "Memory usage is at {}%", 85);
error_box!("Database", "Database connection failed");
success_box!("Login", "User {} connected", "Alice");
debug_box!("Processing", "Items in queue: {}", 42);
}
Configure how frequently new log files are created:
init_logger("logs", LogInterval::OneHour).unwrap();
Available intervals:
LogInterval::OneHour
- New file every hour (e.g., 2024-02-20-14h-15h.log
)LogInterval::ThreeHour
- Every 3 hours (e.g., 2024-02-20-12h-15h.log
)LogInterval::SixHour
- Every 6 hours (e.g., 2024-02-20-12h-18h.log
)LogInterval::NineHour
- Every 9 hours (e.g., 2024-02-20-09h-18h.log
)LogInterval::TwelveHour
- Every 12 hours (e.g., 2024-02-20-12h-00h.log
)LogInterval::OneDay
- One file per day (e.g., 2024-02-20-00h-24h.log
)Control which types of logs are saved to files:
use tlogger::prelude::*;
// Initialize logger
init_logger("logs", LogInterval::OneHour).unwrap();
// Clear default log levels (by default all levels are saved)
clear_log_levels();
// Add only the log levels you want to save
add_log_levels!(
LogLevel::Debug,
LogLevel::Warn,
LogLevel::Error
);
// Now only Debug, Warn, and Error logs will be saved to files
// Info and Success logs will still show in console but won't be saved
Change the colors your logs use.
All log types (info, warn, error, success, debug)
will change the color of boxes in box style outputs,
or will change the title of single line outputs. The log types with _text
suffix (e.g., info_text
) will change all message text.
use tlogger::prelude::*;
customize_colors(Colors {
info: ansi_rgb!(32, 80, 123),
info_text: ansi_rgb!(60, 200, 30),
..Default::default()
});
You can style your text using simple markdown-like syntax:
**text**
for bold text*text*
for italic text_text_
for underlined text~text~
for strikethrough text@text@
for dimmed textThese styles can be nested and combined:
// Single styles
info!("Server", "This text is **bold**");
info!("Server", "This text is *italic*");
info!("Server", "This text is _underlined_");
info!("Server", "This text is ~strikethrough~");
info!("Server", "This text is @dimmed@");
// Combined styles
info!("Server", "This is *italic with _underlined_ text*");
info!("Server", "This is **bold with *italic* inside**");
info!("Server", "You can mix **bold** with @dimmed@ and ~strikethrough~");
// Works in box style too
info_box!("Server", "All _styling_ **works** in *boxes* too!");
If you need to style text programmatically, you can use the style_text!
macro:
let styled_text = style_text!("This is **bold** and *italic*", colors.info_text);
Debug messages can be disabled in production while still being logged to file:
set_debug(false); // Disables console output for debug messages
When initialized, logs are automatically saved to files based on the specified interval. All ANSI color codes are automatically stripped from the file output for better readability.
Contributions are welcome! Please feel free to submit a Pull Request.
โน 12:34:56.789 โ Server Starting up...
โ 12:34:56.790 โ Login User Alice connected
โ 12:34:56.791 โ Memory Usage at 85%
โ 12:34:56.792 โ Database Connection failed
Box Style Output:
โญโโLoginโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโณ 12:29:47โฎ
โ User Alice connected โ
โฐโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฏ