mirror of
https://github.com/tokio-rs/tokio.git
synced 2026-08-27 00:00:12 +02:00
54 lines
2.0 KiB
Rust
54 lines
2.0 KiB
Rust
//! A simple example demonstrating how one might implement a custom
|
|||
|
|
//! subscriber.
|
||
|
|
//!
|
||
|
|
//! This subscriber implements a tree-structured logger similar to
|
||
|
|
//! the "compact" formatter in [`slog-term`]. The demo mimicks the
|
||
|
|
//! example output in the screenshot in the [`slog` README].
|
||
|
|
//!
|
||
|
|
//! Note that this logger isn't ready for actual production use.
|
||
|
|
//! Several corners were cut to make the example simple.
|
||
|
|
//!
|
||
|
|
//! [`slog-term`]: https://docs.rs/slog-term/2.4.0/slog_term/
|
||
|
|
//! [`slog` README]: https://github.com/slog-rs/slog#terminal-output-example
|
||
|
|
#[macro_use]
|
||
|
|
extern crate tokio_trace;
|
||
|
|
|
||
|
|
use tokio_trace::field;
|
||
|
|
|
||
|
|
mod sloggish_subscriber;
|
||
|
|
use self::sloggish_subscriber::SloggishSubscriber;
|
||
|
|
|
||
|
|
fn main() {
|
||
|
|
let subscriber = SloggishSubscriber::new(2);
|
||
|
|
|
||
|
|
tokio_trace::dispatcher::with_default(tokio_trace::Dispatch::new(subscriber), || {
|
||
|
|
span!("", version = &field::display(5.0)).enter(|| {
|
||
|
|
span!("server", host = "localhost", port = 8080).enter(|| {
|
||
|
|
info!("starting");
|
||
|
|
info!("listening");
|
||
|
|
let mut peer1 = span!("conn", peer_addr = "82.9.9.9", port = 42381);
|
||
|
|
peer1.enter(|| {
|
||
|
|
debug!("connected");
|
||
|
|
debug!({ length = 2 }, "message received");
|
||
|
|
});
|
||
|
|
let mut peer2 = span!("conn", peer_addr = "8.8.8.8", port = 18230);
|
||
|
|
peer2.enter(|| {
|
||
|
|
debug!("connected");
|
||
|
|
});
|
||
|
|
peer1.enter(|| {
|
||
|
|
warn!({ algo = "xor" }, "weak encryption requested");
|
||
|
|
debug!({ length = 8 }, "response sent");
|
||
|
|
debug!("disconnected");
|
||
|
|
});
|
||
|
|
peer2.enter(|| {
|
||
|
|
debug!({ length = 5 }, "message received");
|
||
|
|
debug!({ length = 8 }, "response sent");
|
||
|
|
debug!("disconnected");
|
||
|
|
});
|
||
|
|
warn!("internal error");
|
||
|
|
info!("exit");
|
||
|
|
})
|
||
|
|
});
|
||
|
|
});
|
||
|
|
}
|