Crates.io | udp-logger-rs |
lib.rs | udp-logger-rs |
version | 0.1.4 |
source | src |
created_at | 2021-08-09 07:29:42.309713 |
updated_at | 2021-08-24 18:27:57.926455 |
description | Log macro for log's kv-unstable backend and a UDP socket logger. |
homepage | |
repository | https://github.com/brucebrown/udp-logger-rs |
max_upload_size | |
id | 433555 |
size | 55,107 |
Log macro for log's kv-unstable backend.
I wanted to build key/value context into my structs and log it when logging within an impl fn. Beyond that, I also wanted to separate the responsiblity of logging from log file management and any associated business logic.
Consider an architecture with multiple containers running on the same host. One being a server, the other being a log processor. Through this separation, the server performs logging and is insulated from the processing of that logging. Meanwhile, the log processor determines how log messages are processed, which can itself become quite complex.
Additionally, I decided that one might want to map the log level to a source port, and/or destination port. The thought here is that it might simplify processing, as the source and/or destination port implies the log level.
Finally, by separating them, server development can use a different log processor than production and production log processing can be tested independent of the server.
use udp_logger_rs::info;
fn main() {
udp_logger_rs::UdpLogger::default().init().unwrap();
info!("hello");
info!("hello",);
info!("hello {}", "cats");
info!("hello {}", "cats",);
let ctx: Vec<(String, String)> = vec![
("cat_1".into(), "chashu".into()),
("cat_2".into(), "nori".into()),
];
info!(kvs: &ctx, "hello {}", "cats",);
}
The UDP client can be as trivial as:
use smol::Async;
use std::io::{self};
use std::net::UdpSocket;
fn main() -> io::Result<()> {
let _result = futures_lite::future::block_on(async {
let socket = Async::<UdpSocket>::bind(([127, 0, 0, 1], 4010))?;
let mut buf = [0u8; 1024 * 32];
loop {
let (len, addr) = socket.recv_from(&mut buf).await?;
let logmsg = std::str::from_utf8(&buf[..len]).expect("invalid utf8");
println!("{} from={}", logmsg, addr);
}
// This hack of unreachable code cements the return type
#[allow(unreachable_code)]
io::Result::Ok(())
});
Ok(())
}
$ cargo add udp-logger-rs
This crate uses #![forbid(unsafe_code)]
to ensure everything is implemented in
100% Safe Rust.
Want to join us? Check out our "Contributing" guide and take a look at some of these issues:
None.
MIT OR Apache-2.0