Crates.io | winston |
lib.rs | winston |
version | 0.1.3 |
source | src |
created_at | 2024-08-23 04:47:52.185901 |
updated_at | 2024-10-16 18:50:07.271926 |
description | winston for rust |
homepage | |
repository | https://github.com/ifeanyi-ugwu/winston_rs |
max_upload_size | |
id | 1348660 |
size | 58,730 |
A customizable, multithreaded logging library for Rust, inspired by the flexibility of Winston.js. This logger supports custom log levels, multiple transports, real-time filtering, and format customization.
Add the following to your Cargo.toml
:
[dependencies]
winston = "0.1"
or with
cargo add winston
Start by configuring the default logger:
use winston::{LoggerOptions, Logger, log_info, log_warn, log_error, configure};
fn main() {
let new_options = LoggerOptions::new()
.level("debug").add_transport(Console::new(None));
configure(Some(new_options));
log_info!("This is an info message.");
log_warn!("This is a warning.");
log_error!("This is an error.");
Logger::shutdown();
}
You can configure the logger with custom log levels, transports, and formats:
use winston::{transports::Console, Logger, LoggerOptions, log_warn, format::json};
fn main() {
let options = LoggerOptions::new()
.level("info")
.format(json())
.add_transport(Console::new(None));
let logger = Logger::new(Some(options)); // or with Logger::builder().level("info").format(json()).add_transport(Console::new(None)).build();
logger.warn("Custom logger warning!");
}
You can query the logs that are in queryable transports like files:
use winston::{Logger, LogQuery, format};
fn main() {
let logger = Logger::builder()
.add_transport(
transports::File::builder()
.filename(temp_path.clone())
.build(),
)
.format(format::combine(vec![format::timestamp(), format::json()]))
.build();
// Log some messages
logger.info("Test message 1");
logger.error("Test error message");
logger.warn("Test warning");
// For testing purpose, Sleep for a short duration to ensure logs are flushed to the file so the query will retrieve them
std::thread::sleep(std::time::Duration::from_secs(1));
let query = LogQuery::new()
.levels(vec!["error"]);
let results = logger.query(&query).unwrap();
for entry in results {
println!("{:?}", entry);
}
}
Ensure all log entries are processed before your application exits(this is only necessary for the default logger since statics do not call drop):
use winston::Logger;
fn main() {
// Your code here
// Gracefully shut down the logger
Logger::shutdown();
}
A global logger is provided for convenience. You can log messages using macros:
use winston::{log_info, log_warn, log_error};
fn main() {
log_info!("Global info log");
log_warn!("Global warning log");
log_error!("Global error log");
}
You can reconfigure the logger during runtime:
use winston::{Logger, LoggerOptions};
fn main() {
let logger = Logger::new(None);
let new_options = LoggerOptions::new()
.level("debug").add_transport(Console::new(None));
logger.configure(Some(new_options));
logger.debug("This is a debug message after reconfiguration.");
}
Feel free to contribute to this project by submitting issues or pull requests.
This project is licensed under the MIT License.