From e88e94d77a46c213c9045b1ebdd69e8e54b0b58a Mon Sep 17 00:00:00 2001 From: kamuik16 Date: Sun, 8 Dec 2024 16:48:20 +0530 Subject: [PATCH 1/9] chore: introduce libp2p-logging --- Cargo.lock | 63 +++++++++++-------- Cargo.toml | 6 +- examples/autonatv2/Cargo.toml | 2 +- misc/logging/CHANGELOG.md | 4 ++ misc/logging/Cargo.toml | 17 +++++ misc/logging/src/lib.rs | 15 +++++ misc/multistream-select/Cargo.toml | 1 + misc/multistream-select/src/dialer_select.rs | 12 ++-- misc/server/Cargo.toml | 2 +- misc/server/src/main.rs | 5 +- muxers/mplex/Cargo.toml | 2 +- muxers/mplex/benches/split_send_size.rs | 5 +- muxers/mplex/src/io.rs | 10 +-- protocols/autonat/Cargo.toml | 2 +- protocols/autonat/tests/autonatv2.rs | 17 ++--- protocols/dcutr/Cargo.toml | 2 +- protocols/dcutr/tests/lib.rs | 5 +- protocols/gossipsub/Cargo.toml | 2 +- protocols/gossipsub/src/behaviour/tests.rs | 5 +- protocols/gossipsub/tests/smoke.rs | 5 +- protocols/identify/Cargo.toml | 2 +- protocols/identify/tests/smoke.rs | 29 +++------ protocols/kad/Cargo.toml | 2 +- protocols/kad/src/behaviour/test.rs | 4 +- protocols/kad/src/handler.rs | 6 +- protocols/kad/tests/client_mode.rs | 17 ++--- protocols/mdns/Cargo.toml | 2 +- protocols/mdns/tests/use-async-std.rs | 17 ++--- protocols/mdns/tests/use-tokio.rs | 13 +--- protocols/perf/Cargo.toml | 2 +- protocols/perf/src/bin/perf.rs | 5 +- protocols/perf/tests/lib.rs | 5 +- protocols/relay/Cargo.toml | 3 +- protocols/relay/tests/lib.rs | 29 +++------ protocols/rendezvous/Cargo.toml | 2 +- protocols/rendezvous/tests/rendezvous.rs | 37 +++-------- protocols/request-response/Cargo.toml | 2 +- .../request-response/tests/error_reporting.rs | 30 +++------ .../request-response/tests/peer_address.rs | 5 +- protocols/request-response/tests/ping.rs | 5 +- protocols/stream/Cargo.toml | 1 + protocols/stream/tests/lib.rs | 15 ++--- swarm/Cargo.toml | 2 +- swarm/src/connection.rs | 13 +--- swarm/src/lib.rs | 4 +- transports/dns/Cargo.toml | 2 +- transports/dns/src/lib.rs | 4 +- transports/noise/Cargo.toml | 2 +- transports/noise/tests/smoke.rs | 5 +- transports/plaintext/Cargo.toml | 2 +- transports/plaintext/tests/smoke.rs | 5 +- transports/quic/Cargo.toml | 2 +- transports/quic/tests/smoke.rs | 41 +++--------- transports/tcp/Cargo.toml | 2 +- transports/tcp/src/lib.rs | 28 +++------ transports/webrtc/Cargo.toml | 3 +- transports/webrtc/tests/smoke.rs | 9 +-- 57 files changed, 193 insertions(+), 346 deletions(-) create mode 100644 misc/logging/CHANGELOG.md create mode 100644 misc/logging/Cargo.toml create mode 100644 misc/logging/src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index 45f185d9780..2ef3af92d98 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2623,6 +2623,7 @@ dependencies = [ "libp2p-core", "libp2p-identify", "libp2p-identity", + "libp2p-logging", "libp2p-request-response", "libp2p-swarm", "libp2p-swarm-test", @@ -2633,7 +2634,6 @@ dependencies = [ "thiserror 2.0.3", "tokio", "tracing", - "tracing-subscriber", "web-time 1.1.0", ] @@ -2697,6 +2697,7 @@ dependencies = [ "libp2p-dns", "libp2p-identify", "libp2p-identity", + "libp2p-logging", "libp2p-noise", "libp2p-ping", "libp2p-plaintext", @@ -2712,7 +2713,6 @@ dependencies = [ "thiserror 2.0.3", "tokio", "tracing", - "tracing-subscriber", "web-time 1.1.0", ] @@ -2727,11 +2727,11 @@ dependencies = [ "hickory-resolver", "libp2p-core", "libp2p-identity", + "libp2p-logging", "parking_lot", "smallvec", "tokio", "tracing", - "tracing-subscriber", ] [[package]] @@ -2772,6 +2772,7 @@ dependencies = [ "hex_fmt", "libp2p-core", "libp2p-identity", + "libp2p-logging", "libp2p-noise", "libp2p-swarm", "libp2p-swarm-test", @@ -2787,7 +2788,6 @@ dependencies = [ "smallvec", "tokio", "tracing", - "tracing-subscriber", "web-time 1.1.0", ] @@ -2803,6 +2803,7 @@ dependencies = [ "futures-timer", "libp2p-core", "libp2p-identity", + "libp2p-logging", "libp2p-swarm", "libp2p-swarm-test", "lru", @@ -2811,7 +2812,6 @@ dependencies = [ "smallvec", "thiserror 2.0.3", "tracing", - "tracing-subscriber", ] [[package]] @@ -2858,6 +2858,7 @@ dependencies = [ "libp2p-core", "libp2p-identify", "libp2p-identity", + "libp2p-logging", "libp2p-noise", "libp2p-swarm", "libp2p-swarm-test", @@ -2871,11 +2872,17 @@ dependencies = [ "smallvec", "thiserror 2.0.3", "tracing", - "tracing-subscriber", "uint", "web-time 1.1.0", ] +[[package]] +name = "libp2p-logging" +version = "0.1.0" +dependencies = [ + "tracing-subscriber", +] + [[package]] name = "libp2p-mdns" version = "0.46.0" @@ -2888,6 +2895,7 @@ dependencies = [ "if-watch", "libp2p-core", "libp2p-identity", + "libp2p-logging", "libp2p-noise", "libp2p-swarm", "libp2p-swarm-test", @@ -2898,7 +2906,6 @@ dependencies = [ "socket2 0.5.7", "tokio", "tracing", - "tracing-subscriber", ] [[package]] @@ -2948,6 +2955,7 @@ dependencies = [ "futures", "libp2p-core", "libp2p-identity", + "libp2p-logging", "libp2p-muxer-test-harness", "libp2p-plaintext", "libp2p-tcp", @@ -2957,7 +2965,6 @@ dependencies = [ "rand 0.8.5", "smallvec", "tracing", - "tracing-subscriber", "unsigned-varint 0.8.0", ] @@ -2983,6 +2990,7 @@ dependencies = [ "futures_ringbuf", "libp2p-core", "libp2p-identity", + "libp2p-logging", "multiaddr", "multihash", "once_cell", @@ -2994,7 +3002,6 @@ dependencies = [ "static_assertions", "thiserror 2.0.3", "tracing", - "tracing-subscriber", "x25519-dalek", "zeroize", ] @@ -3012,6 +3019,7 @@ dependencies = [ "libp2p-core", "libp2p-dns", "libp2p-identity", + "libp2p-logging", "libp2p-quic", "libp2p-swarm", "libp2p-swarm-test", @@ -3024,7 +3032,6 @@ dependencies = [ "thiserror 2.0.3", "tokio", "tracing", - "tracing-subscriber", "web-time 1.1.0", ] @@ -3057,12 +3064,12 @@ dependencies = [ "futures_ringbuf", "libp2p-core", "libp2p-identity", + "libp2p-logging", "quick-protobuf", "quick-protobuf-codec", "quickcheck-ext", "rand 0.8.5", "tracing", - "tracing-subscriber", ] [[package]] @@ -3097,6 +3104,7 @@ dependencies = [ "if-watch", "libp2p-core", "libp2p-identity", + "libp2p-logging", "libp2p-muxer-test-harness", "libp2p-noise", "libp2p-tcp", @@ -3112,7 +3120,6 @@ dependencies = [ "thiserror 2.0.3", "tokio", "tracing", - "tracing-subscriber", ] [[package]] @@ -3127,6 +3134,7 @@ dependencies = [ "futures-timer", "libp2p-core", "libp2p-identity", + "libp2p-logging", "libp2p-ping", "libp2p-plaintext", "libp2p-swarm", @@ -3139,7 +3147,6 @@ dependencies = [ "static_assertions", "thiserror 2.0.3", "tracing", - "tracing-subscriber", "web-time 1.1.0", ] @@ -3155,6 +3162,7 @@ dependencies = [ "libp2p-core", "libp2p-identify", "libp2p-identity", + "libp2p-logging", "libp2p-noise", "libp2p-ping", "libp2p-request-response", @@ -3168,7 +3176,6 @@ dependencies = [ "thiserror 2.0.3", "tokio", "tracing", - "tracing-subscriber", "web-time 1.1.0", ] @@ -3186,6 +3193,7 @@ dependencies = [ "futures_ringbuf", "libp2p-core", "libp2p-identity", + "libp2p-logging", "libp2p-noise", "libp2p-swarm", "libp2p-swarm-test", @@ -3196,7 +3204,6 @@ dependencies = [ "serde_json", "smallvec", "tracing", - "tracing-subscriber", "web-time 1.1.0", ] @@ -3210,13 +3217,13 @@ dependencies = [ "futures", "futures-timer", "libp2p", + "libp2p-logging", "prometheus-client", "serde", "serde_derive", "serde_json", "tokio", "tracing", - "tracing-subscriber", "zeroize", ] @@ -3227,6 +3234,7 @@ dependencies = [ "futures", "libp2p-core", "libp2p-identity", + "libp2p-logging", "libp2p-swarm", "libp2p-swarm-test", "rand 0.8.5", @@ -3250,6 +3258,7 @@ dependencies = [ "libp2p-identify", "libp2p-identity", "libp2p-kad", + "libp2p-logging", "libp2p-ping", "libp2p-plaintext", "libp2p-swarm-derive", @@ -3263,7 +3272,6 @@ dependencies = [ "smallvec", "tokio", "tracing", - "tracing-subscriber", "trybuild", "wasm-bindgen-futures", "web-time 1.1.0", @@ -3308,10 +3316,10 @@ dependencies = [ "libc", "libp2p-core", "libp2p-identity", + "libp2p-logging", "socket2 0.5.7", "tokio", "tracing", - "tracing-subscriber", ] [[package]] @@ -3373,6 +3381,7 @@ dependencies = [ "if-watch", "libp2p-core", "libp2p-identity", + "libp2p-logging", "libp2p-noise", "libp2p-webrtc-utils", "multihash", @@ -3386,7 +3395,6 @@ dependencies = [ "tokio", "tokio-util", "tracing", - "tracing-subscriber", "webrtc", ] @@ -3798,6 +3806,7 @@ dependencies = [ "bytes", "futures", "futures_ringbuf", + "libp2p-logging", "pin-project", "quickcheck-ext", "rand 0.8.5", @@ -6225,9 +6234,9 @@ checksum = "b6bc1c9ce2b5135ac7f93c72918fc37feb872bdc6a5533a8b85eb4b86bfdae52" [[package]] name = "tracing" -version = "0.1.40" +version = "0.1.41" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c3523ab5a71916ccf420eebdf5521fcef02141234bbc0b8a49f2fdc4544364ef" +checksum = "784e0ac535deb450455cbfa28a6f0df145ea1bb7ae51b821cf5e7927fdcfbdd0" dependencies = [ "log", "pin-project-lite", @@ -6237,9 +6246,9 @@ dependencies = [ [[package]] name = "tracing-attributes" -version = "0.1.27" +version = "0.1.28" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "34704c8d6ebcbc939824180af020566b01a7c01f80641264eba0999f6c2b6be7" +checksum = "395ae124c09f9e6918a2310af6038fba074bcf474ac352496d5910dd59a2226d" dependencies = [ "proc-macro2", "quote", @@ -6248,9 +6257,9 @@ dependencies = [ [[package]] name = "tracing-core" -version = "0.1.32" +version = "0.1.33" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c06d3da6113f116aaee68e4d601191614c9053067f9ab7f6edbcb161237daa54" +checksum = "e672c95779cf947c5311f83787af4fa8fffd12fb27e4993211a84bdfd9610f9c" dependencies = [ "once_cell", "valuable", @@ -6305,9 +6314,9 @@ dependencies = [ [[package]] name = "tracing-subscriber" -version = "0.3.18" +version = "0.3.19" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ad0f048c97dbd9faa9b7df56362b8ebcaa52adb06b498c050d2f4e32f90a7a8b" +checksum = "e8189decb5ac0fa7bc8b96b7cb9b2701d60d48805aca84a238004d665fcc4008" dependencies = [ "matchers", "nu-ansi-term", diff --git a/Cargo.toml b/Cargo.toml index 7f7b601ab82..f2e30af28c2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -23,6 +23,7 @@ members = [ "misc/allow-block-list", "misc/connection-limits", "misc/keygen", + "misc/logging", "misc/memory-connection-limits", "misc/metrics", "misc/multistream-select", @@ -113,6 +114,7 @@ libp2p-websocket = { version = "0.44.1", path = "transports/websocket" } libp2p-websocket-websys = { version = "0.4.1", path = "transports/websocket-websys" } libp2p-webtransport-websys = { version = "0.4.1", path = "transports/webtransport-websys" } libp2p-yamux = { version = "0.46.0", path = "muxers/yamux" } +libp2p-logging = {version = "0.1.0", path = "misc/logging" } # External dependencies asynchronous-codec = { version = "0.7.0" } @@ -130,8 +132,8 @@ ring = "0.17.8" rw-stream-sink = { version = "0.4.0", path = "misc/rw-stream-sink" } thiserror = "2" tokio = { version = "1.38", default-features = false } -tracing = "0.1.37" -tracing-subscriber = "0.3" +tracing = "0.1.41" +tracing-subscriber = "0.3.19" unsigned-varint = { version = "0.8.0" } web-time = "1.1.0" diff --git a/examples/autonatv2/Cargo.toml b/examples/autonatv2/Cargo.toml index 6c862ee22e4..a943174a8d5 100644 --- a/examples/autonatv2/Cargo.toml +++ b/examples/autonatv2/Cargo.toml @@ -19,7 +19,7 @@ libp2p = { workspace = true, features = ["macros", "tokio", "tcp", "noise", "yam clap = { version = "4.4.18", features = ["derive"] } tokio = { version = "1.35.1", features = ["macros", "rt-multi-thread"] } tracing = "0.1.40" -tracing-subscriber = { version = "0.3.18", features = ["env-filter"] } +tracing-subscriber = { workspace = true, features = ["env-filter"] } rand = "0.8.5" opentelemetry = { version = "0.21.0", optional = true } opentelemetry_sdk = { version = "0.21.1", optional = true, features = ["rt-tokio"] } diff --git a/misc/logging/CHANGELOG.md b/misc/logging/CHANGELOG.md new file mode 100644 index 00000000000..20b377027c1 --- /dev/null +++ b/misc/logging/CHANGELOG.md @@ -0,0 +1,4 @@ +## 0.1.0 + +- Introduce 'libp2p-logging` crate. + See [PR xxxx](https://github.com/libp2p/rust-libp2p/pull/xxxx). \ No newline at end of file diff --git a/misc/logging/Cargo.toml b/misc/logging/Cargo.toml new file mode 100644 index 00000000000..149843d6022 --- /dev/null +++ b/misc/logging/Cargo.toml @@ -0,0 +1,17 @@ +[package] +name = "libp2p-logging" +version = "0.1.0" +edition = "2021" +authors = ["Krishang Shah "] +license = "MIT" +repository = "https://github.com/libp2p/rust-libp2p" +publish = false + +[package.metadata.release] +release = false + +[dependencies] +tracing-subscriber = { workspace = true, features = ["env-filter"] } + +[lints] +workspace = true diff --git a/misc/logging/src/lib.rs b/misc/logging/src/lib.rs new file mode 100644 index 00000000000..2d0c5c6da18 --- /dev/null +++ b/misc/logging/src/lib.rs @@ -0,0 +1,15 @@ +use tracing_subscriber::EnvFilter; + +pub fn init_tracing_subscriber_with_default_env_filter() { + let _ = tracing_subscriber::fmt() + .with_env_filter(EnvFilter::from_default_env()) + .with_writer(|| std::io::stderr()) + .try_init(); +} + +pub fn init_tracing_subscriber_with_env_filter(filter: impl Into) { + let _ = tracing_subscriber::fmt() + .with_env_filter(filter) + .with_writer(|| std::io::stderr()) + .try_init(); +} diff --git a/misc/multistream-select/Cargo.toml b/misc/multistream-select/Cargo.toml index 1bbe3642477..ca3b4e47e03 100644 --- a/misc/multistream-select/Cargo.toml +++ b/misc/multistream-select/Cargo.toml @@ -25,6 +25,7 @@ quickcheck = { workspace = true } rand = "0.8" rw-stream-sink = { workspace = true } tracing-subscriber = { workspace = true, features = ["env-filter"] } +libp2p-logging = { workspace = true } # Passing arguments to the docsrs builder in order to properly document cfg's. # More information: https://docs.rs/about/builds#cross-compiling diff --git a/misc/multistream-select/src/dialer_select.rs b/misc/multistream-select/src/dialer_select.rs index 1d13e94910d..654877ed756 100644 --- a/misc/multistream-select/src/dialer_select.rs +++ b/misc/multistream-select/src/dialer_select.rs @@ -275,13 +275,11 @@ mod tests { ListenerProtos(listen_protos): ListenerProtos, DialPayload(dial_payload): DialPayload, ) { - let _ = tracing_subscriber::fmt() - .with_env_filter( - EnvFilter::builder() - .with_default_directive(LevelFilter::DEBUG.into()) - .from_env_lossy(), - ) - .try_init(); + libp2p_logging::init_tracing_subscriber_with_env_filter( + EnvFilter::builder() + .with_default_directive(LevelFilter::DEBUG.into()) + .from_env_lossy(), + ); async_std::task::block_on(async move { let listener = TcpListener::bind("0.0.0.0:0").await.unwrap(); diff --git a/misc/server/Cargo.toml b/misc/server/Cargo.toml index 0954e2f38d8..7e2538e6fdb 100644 --- a/misc/server/Cargo.toml +++ b/misc/server/Cargo.toml @@ -39,7 +39,7 @@ serde_derive = "1.0.125" serde_json = "1.0" tokio = { workspace = true, features = ["rt-multi-thread", "macros"] } tracing = { workspace = true } -tracing-subscriber = { workspace = true, features = ["env-filter"] } +libp2p-logging = { workspace = true } zeroize = "1" [lints] diff --git a/misc/server/src/main.rs b/misc/server/src/main.rs index a633a80207e..aeda806ad47 100644 --- a/misc/server/src/main.rs +++ b/misc/server/src/main.rs @@ -13,7 +13,6 @@ use libp2p::{ tcp, yamux, }; use prometheus_client::{metrics::info::Info, registry::Registry}; -use tracing_subscriber::EnvFilter; use zeroize::Zeroizing; mod behaviour; @@ -42,9 +41,7 @@ struct Opts { #[tokio::main] async fn main() -> Result<(), Box> { - let _ = tracing_subscriber::fmt() - .with_env_filter(EnvFilter::from_default_env()) - .try_init(); + libp2p_logging::init_tracing_subscriber_with_default_env_filter(); let opt = Opts::parse(); diff --git a/muxers/mplex/Cargo.toml b/muxers/mplex/Cargo.toml index 7f887c8b3b8..114091506e1 100644 --- a/muxers/mplex/Cargo.toml +++ b/muxers/mplex/Cargo.toml @@ -32,7 +32,7 @@ libp2p-muxer-test-harness = { path = "../test-harness" } libp2p-plaintext = { workspace = true } libp2p-tcp = { workspace = true, features = ["async-io"] } quickcheck = { workspace = true } -tracing-subscriber = { workspace = true, features = ["env-filter"] } +libp2p-logging = { workspace = true } [[bench]] name = "split_send_size" diff --git a/muxers/mplex/benches/split_send_size.rs b/muxers/mplex/benches/split_send_size.rs index b0dd4babff7..fa4e770592b 100644 --- a/muxers/mplex/benches/split_send_size.rs +++ b/muxers/mplex/benches/split_send_size.rs @@ -38,7 +38,6 @@ use libp2p_identity as identity; use libp2p_identity::PeerId; use libp2p_mplex as mplex; use libp2p_plaintext as plaintext; -use tracing_subscriber::EnvFilter; type BenchTransport = transport::Boxed<(PeerId, muxing::StreamMuxerBox)>; @@ -55,9 +54,7 @@ const BENCH_SIZES: [usize; 8] = [ ]; fn prepare(c: &mut Criterion) { - let _ = tracing_subscriber::fmt() - .with_env_filter(EnvFilter::from_default_env()) - .try_init(); + libp2p_logging::init_tracing_subscriber_with_default_env_filter(); let payload: Vec = vec![1; 1024 * 1024]; diff --git a/muxers/mplex/src/io.rs b/muxers/mplex/src/io.rs index ac93fd3865e..76b8edafbba 100644 --- a/muxers/mplex/src/io.rs +++ b/muxers/mplex/src/io.rs @@ -1231,10 +1231,7 @@ mod tests { #[test] fn max_buffer_behaviour() { - use tracing_subscriber::EnvFilter; - let _ = tracing_subscriber::fmt() - .with_env_filter(EnvFilter::from_default_env()) - .try_init(); + libp2p_logging::init_tracing_subscriber_with_default_env_filter(); fn prop(cfg: MplexConfig, overflow: NonZeroU8) { let mut r_buf = BytesMut::new(); @@ -1369,10 +1366,7 @@ mod tests { #[test] fn close_on_error() { - use tracing_subscriber::EnvFilter; - let _ = tracing_subscriber::fmt() - .with_env_filter(EnvFilter::from_default_env()) - .try_init(); + libp2p_logging::init_tracing_subscriber_with_default_env_filter(); fn prop(cfg: MplexConfig, num_streams: NonZeroU8) { let num_streams = cmp::min(cfg.max_substreams, num_streams.get() as usize); diff --git a/protocols/autonat/Cargo.toml b/protocols/autonat/Cargo.toml index 92ca163d8ec..ecab2ba5fb5 100644 --- a/protocols/autonat/Cargo.toml +++ b/protocols/autonat/Cargo.toml @@ -38,7 +38,7 @@ thiserror = { workspace = true, optional = true } [dev-dependencies] tokio = { workspace = true, features = ["macros", "rt", "sync"] } libp2p-swarm-test = { path = "../../swarm-test" } -tracing-subscriber = { version = "0.3", features = ["env-filter"] } +libp2p-logging = { workspace = true } libp2p-identify = { workspace = true } libp2p-swarm = { workspace = true, features = ["macros"] } diff --git a/protocols/autonat/tests/autonatv2.rs b/protocols/autonat/tests/autonatv2.rs index 49866a9adb5..42f18e74c9a 100644 --- a/protocols/autonat/tests/autonatv2.rs +++ b/protocols/autonat/tests/autonatv2.rs @@ -11,13 +11,10 @@ use libp2p_swarm::{ use libp2p_swarm_test::SwarmExt; use rand_core::OsRng; use tokio::sync::oneshot; -use tracing_subscriber::EnvFilter; #[tokio::test] async fn confirm_successful() { - let _ = tracing_subscriber::fmt() - .with_env_filter(EnvFilter::from_default_env()) - .try_init(); + libp2p_logging::init_tracing_subscriber_with_default_env_filter(); let (mut alice, mut bob) = start_and_connect().await; let cor_server_peer = *alice.local_peer_id(); @@ -128,9 +125,7 @@ async fn confirm_successful() { #[tokio::test] async fn dial_back_to_unsupported_protocol() { - let _ = tracing_subscriber::fmt() - .with_env_filter(EnvFilter::from_default_env()) - .try_init(); + libp2p_logging::init_tracing_subscriber_with_default_env_filter(); let (mut alice, mut bob) = bootstrap().await; let alice_peer_id = *alice.local_peer_id(); @@ -226,9 +221,7 @@ async fn dial_back_to_unsupported_protocol() { #[tokio::test] async fn dial_back_to_non_libp2p() { - let _ = tracing_subscriber::fmt() - .with_env_filter(EnvFilter::from_default_env()) - .try_init(); + libp2p_logging::init_tracing_subscriber_with_default_env_filter(); let (mut alice, mut bob) = bootstrap().await; let alice_peer_id = *alice.local_peer_id(); @@ -314,9 +307,7 @@ async fn dial_back_to_non_libp2p() { #[tokio::test] async fn dial_back_to_not_supporting() { - let _ = tracing_subscriber::fmt() - .with_env_filter(EnvFilter::from_default_env()) - .try_init(); + libp2p_logging::init_tracing_subscriber_with_default_env_filter(); let (mut alice, mut bob) = bootstrap().await; let alice_peer_id = *alice.local_peer_id(); diff --git a/protocols/dcutr/Cargo.toml b/protocols/dcutr/Cargo.toml index a47f5400488..4e160700d9b 100644 --- a/protocols/dcutr/Cargo.toml +++ b/protocols/dcutr/Cargo.toml @@ -39,7 +39,7 @@ libp2p-swarm-test = { path = "../../swarm-test" } libp2p-tcp = { workspace = true, features = ["async-io"] } libp2p-yamux = { workspace = true } rand = "0.8" -tracing-subscriber = { workspace = true, features = ["env-filter"] } +libp2p-logging = { workspace = true } tokio = { workspace = true, features = ["rt", "macros"] } # Passing arguments to the docsrs builder in order to properly document cfg's. diff --git a/protocols/dcutr/tests/lib.rs b/protocols/dcutr/tests/lib.rs index a35c9a50cfe..4ecccd4e172 100644 --- a/protocols/dcutr/tests/lib.rs +++ b/protocols/dcutr/tests/lib.rs @@ -32,13 +32,10 @@ use libp2p_plaintext as plaintext; use libp2p_relay as relay; use libp2p_swarm::{Config, NetworkBehaviour, Swarm, SwarmEvent}; use libp2p_swarm_test::SwarmExt as _; -use tracing_subscriber::EnvFilter; #[tokio::test] async fn connect() { - let _ = tracing_subscriber::fmt() - .with_env_filter(EnvFilter::from_default_env()) - .try_init(); + libp2p_logging::init_tracing_subscriber_with_default_env_filter(); let mut relay = build_relay(); let mut dst = build_client(); diff --git a/protocols/gossipsub/Cargo.toml b/protocols/gossipsub/Cargo.toml index c09286c8aa0..b4a098f93f2 100644 --- a/protocols/gossipsub/Cargo.toml +++ b/protocols/gossipsub/Cargo.toml @@ -48,7 +48,7 @@ libp2p-yamux = { workspace = true } libp2p-noise = { workspace = true } libp2p-swarm-test = { path = "../../swarm-test" } quickcheck = { workspace = true } -tracing-subscriber = { workspace = true, features = ["env-filter"] } +libp2p-logging = { workspace = true } tokio = { workspace = true, features = ["rt", "rt-multi-thread", "time", "macros"] } # Passing arguments to the docsrs builder in order to properly document cfg's. diff --git a/protocols/gossipsub/src/behaviour/tests.rs b/protocols/gossipsub/src/behaviour/tests.rs index eaa983d214d..c5c1920571e 100644 --- a/protocols/gossipsub/src/behaviour/tests.rs +++ b/protocols/gossipsub/src/behaviour/tests.rs @@ -4761,10 +4761,7 @@ fn test_limit_number_of_message_ids_inside_ihave() { #[test] fn test_iwant_penalties() { - // use tracing_subscriber::EnvFilter; - // let _ = tracing_subscriber::fmt() - // .with_env_filter(EnvFilter::from_default_env()) - // .try_init(); + libp2p_logging::init_tracing_subscriber_with_default_env_filter(); let config = ConfigBuilder::default() .iwant_followup_time(Duration::from_secs(4)) .build() diff --git a/protocols/gossipsub/tests/smoke.rs b/protocols/gossipsub/tests/smoke.rs index 85038665b4d..fdd0381711c 100644 --- a/protocols/gossipsub/tests/smoke.rs +++ b/protocols/gossipsub/tests/smoke.rs @@ -31,7 +31,6 @@ use libp2p_swarm_test::SwarmExt as _; use quickcheck::{QuickCheck, TestResult}; use rand::{seq::SliceRandom, SeedableRng}; use tokio::{runtime::Runtime, time}; -use tracing_subscriber::EnvFilter; struct Graph { nodes: SelectAll>, @@ -132,9 +131,7 @@ async fn build_node() -> Swarm { #[test] fn multi_hop_propagation() { - let _ = tracing_subscriber::fmt() - .with_env_filter(EnvFilter::from_default_env()) - .try_init(); + libp2p_logging::init_tracing_subscriber_with_default_env_filter(); fn prop(num_nodes: u8, seed: u64) -> TestResult { if !(2..=50).contains(&num_nodes) { diff --git a/protocols/identify/Cargo.toml b/protocols/identify/Cargo.toml index d7f6b6eca76..54113bcc78a 100644 --- a/protocols/identify/Cargo.toml +++ b/protocols/identify/Cargo.toml @@ -30,7 +30,7 @@ either = "1.12.0" async-std = { version = "1.6.2", features = ["attributes"] } libp2p-swarm-test = { path = "../../swarm-test" } libp2p-swarm = { workspace = true, features = ["macros"] } -tracing-subscriber = { workspace = true, features = ["env-filter"] } +libp2p-logging = { workspace = true } # Passing arguments to the docsrs builder in order to properly document cfg's. # More information: https://docs.rs/about/builds#cross-compiling diff --git a/protocols/identify/tests/smoke.rs b/protocols/identify/tests/smoke.rs index dd48b314173..686d7a49ea6 100644 --- a/protocols/identify/tests/smoke.rs +++ b/protocols/identify/tests/smoke.rs @@ -8,13 +8,10 @@ use futures::StreamExt; use libp2p_identify as identify; use libp2p_swarm::{Swarm, SwarmEvent}; use libp2p_swarm_test::SwarmExt; -use tracing_subscriber::EnvFilter; #[async_std::test] async fn periodic_identify() { - let _ = tracing_subscriber::fmt() - .with_env_filter(EnvFilter::from_default_env()) - .try_init(); + libp2p_logging::init_tracing_subscriber_with_default_env_filter(); let mut swarm1 = Swarm::new_ephemeral(|identity| { identify::Behaviour::new( @@ -83,9 +80,7 @@ async fn periodic_identify() { } #[async_std::test] async fn only_emits_address_candidate_once_per_connection() { - let _ = tracing_subscriber::fmt() - .with_env_filter(EnvFilter::from_default_env()) - .try_init(); + libp2p_logging::init_tracing_subscriber_with_default_env_filter(); let mut swarm1 = Swarm::new_ephemeral(|identity| { identify::Behaviour::new( @@ -155,9 +150,7 @@ async fn only_emits_address_candidate_once_per_connection() { #[async_std::test] async fn emits_unique_listen_addresses() { - let _ = tracing_subscriber::fmt() - .with_env_filter(EnvFilter::from_default_env()) - .try_init(); + libp2p_logging::init_tracing_subscriber_with_default_env_filter(); let mut swarm1 = Swarm::new_ephemeral(|identity| { identify::Behaviour::new( @@ -227,9 +220,7 @@ async fn emits_unique_listen_addresses() { #[async_std::test] async fn hides_listen_addresses() { - let _ = tracing_subscriber::fmt() - .with_env_filter(EnvFilter::from_default_env()) - .try_init(); + libp2p_logging::init_tracing_subscriber_with_default_env_filter(); let mut swarm1 = Swarm::new_ephemeral(|identity| { identify::Behaviour::new( @@ -298,9 +289,7 @@ async fn hides_listen_addresses() { #[async_std::test] async fn identify_push() { - let _ = tracing_subscriber::fmt() - .with_env_filter(EnvFilter::from_default_env()) - .try_init(); + libp2p_logging::init_tracing_subscriber_with_default_env_filter(); let mut swarm1 = Swarm::new_ephemeral(|identity| { identify::Behaviour::new(identify::Config::new("a".to_string(), identity.public())) @@ -350,9 +339,7 @@ async fn identify_push() { #[async_std::test] async fn discover_peer_after_disconnect() { - let _ = tracing_subscriber::fmt() - .with_env_filter(EnvFilter::from_default_env()) - .try_init(); + libp2p_logging::init_tracing_subscriber_with_default_env_filter(); let mut swarm1 = Swarm::new_ephemeral(|identity| { identify::Behaviour::new(identify::Config::new("a".to_string(), identity.public())) @@ -403,9 +390,7 @@ async fn discover_peer_after_disconnect() { #[async_std::test] async fn configured_interval_starts_after_first_identify() { - let _ = tracing_subscriber::fmt() - .with_env_filter(EnvFilter::from_default_env()) - .try_init(); + libp2p_logging::init_tracing_subscriber_with_default_env_filter(); let identify_interval = Duration::from_secs(5); diff --git a/protocols/kad/Cargo.toml b/protocols/kad/Cargo.toml index cd97c91b2bf..237897a4c16 100644 --- a/protocols/kad/Cargo.toml +++ b/protocols/kad/Cargo.toml @@ -42,7 +42,7 @@ libp2p-swarm = { path = "../../swarm", features = ["macros"] } libp2p-swarm-test = { path = "../../swarm-test" } libp2p-yamux = { workspace = true } quickcheck = { workspace = true } -tracing-subscriber = { workspace = true, features = ["env-filter"] } +libp2p-logging = { workspace = true } [features] serde = ["dep:serde", "bytes/serde"] diff --git a/protocols/kad/src/behaviour/test.rs b/protocols/kad/src/behaviour/test.rs index 82749ffb5fd..c94451e56f4 100644 --- a/protocols/kad/src/behaviour/test.rs +++ b/protocols/kad/src/behaviour/test.rs @@ -324,9 +324,7 @@ fn query_iter() { #[test] fn unresponsive_not_returned_direct() { - let _ = tracing_subscriber::fmt() - .with_env_filter(tracing_subscriber::EnvFilter::from_default_env()) - .try_init(); + libp2p_logging::init_tracing_subscriber_with_default_env_filter(); // Build one node. It contains fake addresses to non-existing nodes. We ask it to find a // random peer. We make sure that no fake address is returned. diff --git a/protocols/kad/src/handler.rs b/protocols/kad/src/handler.rs index 6b4e944e2b0..0bfcc1d54df 100644 --- a/protocols/kad/src/handler.rs +++ b/protocols/kad/src/handler.rs @@ -1068,8 +1068,6 @@ fn process_kad_response(event: KadResponseMsg, query_id: QueryId) -> HandlerEven #[cfg(test)] mod tests { use quickcheck::{Arbitrary, Gen}; - use tracing_subscriber::EnvFilter; - use super::*; impl Arbitrary for ProtocolStatus { @@ -1083,9 +1081,7 @@ mod tests { #[test] fn compute_next_protocol_status_test() { - let _ = tracing_subscriber::fmt() - .with_env_filter(EnvFilter::from_default_env()) - .try_init(); + libp2p_logging::init_tracing_subscriber_with_default_env_filter(); fn prop(now_supported: bool, current: Option) { let new = compute_new_protocol_status(now_supported, current); diff --git a/protocols/kad/tests/client_mode.rs b/protocols/kad/tests/client_mode.rs index 3275c525890..591ebb62dc9 100644 --- a/protocols/kad/tests/client_mode.rs +++ b/protocols/kad/tests/client_mode.rs @@ -3,15 +3,12 @@ use libp2p_identity as identity; use libp2p_kad::{store::MemoryStore, Behaviour, Config, Event, Mode}; use libp2p_swarm::{Swarm, SwarmEvent}; use libp2p_swarm_test::SwarmExt; -use tracing_subscriber::EnvFilter; use Event::*; use MyBehaviourEvent::*; #[async_std::test] async fn server_gets_added_to_routing_table_by_client() { - let _ = tracing_subscriber::fmt() - .with_env_filter(EnvFilter::from_default_env()) - .try_init(); + libp2p_logging::init_tracing_subscriber_with_default_env_filter(); let mut client = Swarm::new_ephemeral(MyBehaviour::new); let mut server = Swarm::new_ephemeral(MyBehaviour::new); @@ -41,9 +38,7 @@ async fn server_gets_added_to_routing_table_by_client() { #[async_std::test] async fn two_servers_add_each_other_to_routing_table() { - let _ = tracing_subscriber::fmt() - .with_env_filter(EnvFilter::from_default_env()) - .try_init(); + libp2p_logging::init_tracing_subscriber_with_default_env_filter(); let mut server1 = Swarm::new_ephemeral(MyBehaviour::new); let mut server2 = Swarm::new_ephemeral(MyBehaviour::new); @@ -82,9 +77,7 @@ async fn two_servers_add_each_other_to_routing_table() { #[async_std::test] async fn adding_an_external_addresses_activates_server_mode_on_existing_connections() { - let _ = tracing_subscriber::fmt() - .with_env_filter(EnvFilter::from_default_env()) - .try_init(); + libp2p_logging::init_tracing_subscriber_with_default_env_filter(); let mut client = Swarm::new_ephemeral(MyBehaviour::new); let mut server = Swarm::new_ephemeral(MyBehaviour::new); @@ -120,9 +113,7 @@ async fn adding_an_external_addresses_activates_server_mode_on_existing_connecti #[async_std::test] async fn set_client_to_server_mode() { - let _ = tracing_subscriber::fmt() - .with_env_filter(EnvFilter::from_default_env()) - .try_init(); + libp2p_logging::init_tracing_subscriber_with_default_env_filter(); let mut client = Swarm::new_ephemeral(MyBehaviour::new); client.behaviour_mut().kad.set_mode(Some(Mode::Client)); diff --git a/protocols/mdns/Cargo.toml b/protocols/mdns/Cargo.toml index 338501aa896..2e7e94e017d 100644 --- a/protocols/mdns/Cargo.toml +++ b/protocols/mdns/Cargo.toml @@ -38,7 +38,7 @@ libp2p-tcp = { workspace = true, features = ["tokio", "async-io"] } libp2p-yamux = { workspace = true } tokio = { workspace = true, features = ["macros", "rt", "rt-multi-thread", "time"] } libp2p-swarm-test = { path = "../../swarm-test" } -tracing-subscriber = { workspace = true, features = ["env-filter"] } +libp2p-logging = { workspace = true } [[test]] name = "use-async-std" diff --git a/protocols/mdns/tests/use-async-std.rs b/protocols/mdns/tests/use-async-std.rs index df08b39af07..db7639a9cc9 100644 --- a/protocols/mdns/tests/use-async-std.rs +++ b/protocols/mdns/tests/use-async-std.rs @@ -24,22 +24,17 @@ use futures::future::Either; use libp2p_mdns::{async_io::Behaviour, Config, Event}; use libp2p_swarm::{Swarm, SwarmEvent}; use libp2p_swarm_test::SwarmExt as _; -use tracing_subscriber::EnvFilter; #[async_std::test] async fn test_discovery_async_std_ipv4() { - let _ = tracing_subscriber::fmt() - .with_env_filter(EnvFilter::from_default_env()) - .try_init(); + libp2p_logging::init_tracing_subscriber_with_default_env_filter(); run_discovery_test(Config::default()).await } #[async_std::test] async fn test_discovery_async_std_ipv6() { - let _ = tracing_subscriber::fmt() - .with_env_filter(EnvFilter::from_default_env()) - .try_init(); + libp2p_logging::init_tracing_subscriber_with_default_env_filter(); let config = Config { enable_ipv6: true, @@ -50,9 +45,7 @@ async fn test_discovery_async_std_ipv6() { #[async_std::test] async fn test_expired_async_std() { - let _ = tracing_subscriber::fmt() - .with_env_filter(EnvFilter::from_default_env()) - .try_init(); + libp2p_logging::init_tracing_subscriber_with_default_env_filter(); let config = Config { ttl: Duration::from_secs(1), @@ -85,9 +78,7 @@ async fn test_expired_async_std() { #[async_std::test] async fn test_no_expiration_on_close_async_std() { - let _ = tracing_subscriber::fmt() - .with_env_filter(EnvFilter::from_default_env()) - .try_init(); + libp2p_logging::init_tracing_subscriber_with_default_env_filter(); let config = Config { ttl: Duration::from_secs(120), query_interval: Duration::from_secs(10), diff --git a/protocols/mdns/tests/use-tokio.rs b/protocols/mdns/tests/use-tokio.rs index 0ec90a52b90..ff27fb6c047 100644 --- a/protocols/mdns/tests/use-tokio.rs +++ b/protocols/mdns/tests/use-tokio.rs @@ -23,22 +23,17 @@ use futures::future::Either; use libp2p_mdns::{tokio::Behaviour, Config, Event}; use libp2p_swarm::{Swarm, SwarmEvent}; use libp2p_swarm_test::SwarmExt as _; -use tracing_subscriber::EnvFilter; #[tokio::test] async fn test_discovery_tokio_ipv4() { - let _ = tracing_subscriber::fmt() - .with_env_filter(EnvFilter::from_default_env()) - .try_init(); + libp2p_logging::init_tracing_subscriber_with_default_env_filter(); run_discovery_test(Config::default()).await } #[tokio::test] async fn test_discovery_tokio_ipv6() { - let _ = tracing_subscriber::fmt() - .with_env_filter(EnvFilter::from_default_env()) - .try_init(); + libp2p_logging::init_tracing_subscriber_with_default_env_filter(); let config = Config { enable_ipv6: true, @@ -49,9 +44,7 @@ async fn test_discovery_tokio_ipv6() { #[tokio::test] async fn test_expired_tokio() { - let _ = tracing_subscriber::fmt() - .with_env_filter(EnvFilter::from_default_env()) - .try_init(); + libp2p_logging::init_tracing_subscriber_with_default_env_filter(); let config = Config { ttl: Duration::from_secs(1), diff --git a/protocols/perf/Cargo.toml b/protocols/perf/Cargo.toml index cd499a8c949..649183566fc 100644 --- a/protocols/perf/Cargo.toml +++ b/protocols/perf/Cargo.toml @@ -30,7 +30,7 @@ serde = { version = "1.0", features = ["derive"] } serde_json = "1.0" thiserror = { workspace = true } tracing = { workspace = true } -tracing-subscriber = { workspace = true, features = ["env-filter"] } +libp2p-logging = { workspace = true } tokio = { workspace = true, features = ["macros", "rt", "rt-multi-thread"] } [dev-dependencies] diff --git a/protocols/perf/src/bin/perf.rs b/protocols/perf/src/bin/perf.rs index 506455f081a..d1c22911c80 100644 --- a/protocols/perf/src/bin/perf.rs +++ b/protocols/perf/src/bin/perf.rs @@ -31,7 +31,6 @@ use libp2p::{ }; use libp2p_perf::{client, server, Final, Intermediate, Run, RunParams, RunUpdate}; use serde::{Deserialize, Serialize}; -use tracing_subscriber::EnvFilter; use web_time::{Duration, Instant}; #[derive(Debug, Parser)] @@ -72,9 +71,7 @@ impl FromStr for Transport { #[tokio::main] async fn main() -> Result<()> { - let _ = tracing_subscriber::fmt() - .with_env_filter(EnvFilter::from_default_env()) - .try_init(); + libp2p_logging::init_tracing_subscriber_with_default_env_filter(); let opts = Opts::parse(); match opts { diff --git a/protocols/perf/tests/lib.rs b/protocols/perf/tests/lib.rs index 017d475befd..1b9b3b08889 100644 --- a/protocols/perf/tests/lib.rs +++ b/protocols/perf/tests/lib.rs @@ -24,13 +24,10 @@ use libp2p_perf::{ }; use libp2p_swarm::{Swarm, SwarmEvent}; use libp2p_swarm_test::SwarmExt; -use tracing_subscriber::EnvFilter; #[tokio::test] async fn perf() { - let _ = tracing_subscriber::fmt() - .with_env_filter(EnvFilter::from_default_env()) - .try_init(); + libp2p_logging::init_tracing_subscriber_with_default_env_filter(); let mut server = Swarm::new_ephemeral(|_| server::Behaviour::new()); let server_peer_id = *server.local_peer_id(); diff --git a/protocols/relay/Cargo.toml b/protocols/relay/Cargo.toml index 6c2c7b90304..12d454071d1 100644 --- a/protocols/relay/Cargo.toml +++ b/protocols/relay/Cargo.toml @@ -36,8 +36,7 @@ libp2p-swarm = { workspace = true, features = ["macros", "async-std"] } libp2p-swarm-test = { workspace = true } libp2p-yamux = { workspace = true } quickcheck = { workspace = true } -tracing-subscriber = { workspace = true, features = ["env-filter"] } - +libp2p-logging = { workspace = true } # Passing arguments to the docsrs builder in order to properly document cfg's. # More information: https://docs.rs/about/builds#cross-compiling diff --git a/protocols/relay/tests/lib.rs b/protocols/relay/tests/lib.rs index 125f0dbb4ad..b13894fe062 100644 --- a/protocols/relay/tests/lib.rs +++ b/protocols/relay/tests/lib.rs @@ -40,13 +40,10 @@ use libp2p_plaintext as plaintext; use libp2p_relay as relay; use libp2p_swarm::{dial_opts::DialOpts, Config, DialError, NetworkBehaviour, Swarm, SwarmEvent}; use libp2p_swarm_test::SwarmExt; -use tracing_subscriber::EnvFilter; #[test] fn reservation() { - let _ = tracing_subscriber::fmt() - .with_env_filter(EnvFilter::from_default_env()) - .try_init(); + libp2p_logging::init_tracing_subscriber_with_default_env_filter(); let mut pool = LocalPool::new(); let relay_addr = Multiaddr::empty().with(Protocol::Memory(rand::random::())); @@ -87,9 +84,7 @@ fn reservation() { #[test] fn new_reservation_to_same_relay_replaces_old() { - let _ = tracing_subscriber::fmt() - .with_env_filter(EnvFilter::from_default_env()) - .try_init(); + libp2p_logging::init_tracing_subscriber_with_default_env_filter(); let mut pool = LocalPool::new(); let relay_addr = Multiaddr::empty().with(Protocol::Memory(rand::random::())); @@ -186,9 +181,7 @@ fn new_reservation_to_same_relay_replaces_old() { #[test] fn connect() { - let _ = tracing_subscriber::fmt() - .with_env_filter(EnvFilter::from_default_env()) - .try_init(); + libp2p_logging::init_tracing_subscriber_with_default_env_filter(); let mut pool = LocalPool::new(); let relay_addr = Multiaddr::empty().with(Protocol::Memory(rand::random::())); @@ -272,9 +265,7 @@ async fn connection_established_to( #[test] fn handle_dial_failure() { - let _ = tracing_subscriber::fmt() - .with_env_filter(EnvFilter::from_default_env()) - .try_init(); + libp2p_logging::init_tracing_subscriber_with_default_env_filter(); let mut pool = LocalPool::new(); let relay_addr = Multiaddr::empty().with(Protocol::Memory(rand::random::())); @@ -293,9 +284,7 @@ fn handle_dial_failure() { #[test] fn propagate_reservation_error_to_listener() { - let _ = tracing_subscriber::fmt() - .with_env_filter(EnvFilter::from_default_env()) - .try_init(); + libp2p_logging::init_tracing_subscriber_with_default_env_filter(); let mut pool = LocalPool::new(); let relay_addr = Multiaddr::empty().with(Protocol::Memory(rand::random::())); @@ -342,9 +331,7 @@ fn propagate_reservation_error_to_listener() { #[test] fn propagate_connect_error_to_unknown_peer_to_dialer() { - let _ = tracing_subscriber::fmt() - .with_env_filter(EnvFilter::from_default_env()) - .try_init(); + libp2p_logging::init_tracing_subscriber_with_default_env_filter(); let mut pool = LocalPool::new(); let relay_addr = Multiaddr::empty().with(Protocol::Memory(rand::random::())); @@ -398,9 +385,7 @@ fn propagate_connect_error_to_unknown_peer_to_dialer() { #[test] fn reuse_connection() { - let _ = tracing_subscriber::fmt() - .with_env_filter(EnvFilter::from_default_env()) - .try_init(); + libp2p_logging::init_tracing_subscriber_with_default_env_filter(); let mut pool = LocalPool::new(); let relay_addr = Multiaddr::empty().with(Protocol::Memory(rand::random::())); diff --git a/protocols/rendezvous/Cargo.toml b/protocols/rendezvous/Cargo.toml index 5fa40c3785b..d2935c26fe0 100644 --- a/protocols/rendezvous/Cargo.toml +++ b/protocols/rendezvous/Cargo.toml @@ -37,7 +37,7 @@ libp2p-tcp = { workspace = true, features = ["tokio"] } libp2p-yamux = { workspace = true } rand = "0.8" tokio = { workspace = true, features = [ "rt-multi-thread", "time", "macros", "sync", "process", "fs", "net" ] } -tracing-subscriber = { workspace = true, features = ["env-filter"] } +libp2p-logging = { workspace = true } # Passing arguments to the docsrs builder in order to properly document cfg's. # More information: https://docs.rs/about/builds#cross-compiling diff --git a/protocols/rendezvous/tests/rendezvous.rs b/protocols/rendezvous/tests/rendezvous.rs index 2305c2ef412..617b2afafa1 100644 --- a/protocols/rendezvous/tests/rendezvous.rs +++ b/protocols/rendezvous/tests/rendezvous.rs @@ -27,13 +27,10 @@ use libp2p_rendezvous as rendezvous; use libp2p_rendezvous::client::RegisterError; use libp2p_swarm::{DialError, Swarm, SwarmEvent}; use libp2p_swarm_test::SwarmExt; -use tracing_subscriber::EnvFilter; #[tokio::test] async fn given_successful_registration_then_successful_discovery() { - let _ = tracing_subscriber::fmt() - .with_env_filter(EnvFilter::from_default_env()) - .try_init(); + libp2p_logging::init_tracing_subscriber_with_default_env_filter(); let namespace = rendezvous::Namespace::from_static("some-namespace"); let ([mut alice, mut bob], mut robert) = new_server_with_connected_clients(rendezvous::server::Config::default()).await; @@ -86,9 +83,7 @@ async fn given_successful_registration_then_successful_discovery() { #[tokio::test] async fn should_return_error_when_no_external_addresses() { - let _ = tracing_subscriber::fmt() - .with_env_filter(EnvFilter::from_default_env()) - .try_init(); + libp2p_logging::init_tracing_subscriber_with_default_env_filter(); let namespace = rendezvous::Namespace::from_static("some-namespace"); let server = new_server(rendezvous::server::Config::default()).await; let mut client = Swarm::new_ephemeral(rendezvous::client::Behaviour::new); @@ -103,9 +98,7 @@ async fn should_return_error_when_no_external_addresses() { #[tokio::test] async fn given_successful_registration_then_refresh_ttl() { - let _ = tracing_subscriber::fmt() - .with_env_filter(EnvFilter::from_default_env()) - .try_init(); + libp2p_logging::init_tracing_subscriber_with_default_env_filter(); let namespace = rendezvous::Namespace::from_static("some-namespace"); let ([mut alice, mut bob], mut robert) = new_server_with_connected_clients(rendezvous::server::Config::default()).await; @@ -171,9 +164,7 @@ async fn given_successful_registration_then_refresh_ttl() { #[tokio::test] async fn given_successful_registration_then_refresh_external_addrs() { - let _ = tracing_subscriber::fmt() - .with_env_filter(EnvFilter::from_default_env()) - .try_init(); + libp2p_logging::init_tracing_subscriber_with_default_env_filter(); let namespace = rendezvous::Namespace::from_static("some-namespace"); let ([mut alice], mut robert) = new_server_with_connected_clients(rendezvous::server::Config::default()).await; @@ -224,9 +215,7 @@ async fn given_successful_registration_then_refresh_external_addrs() { #[tokio::test] async fn given_invalid_ttl_then_unsuccessful_registration() { - let _ = tracing_subscriber::fmt() - .with_env_filter(EnvFilter::from_default_env()) - .try_init(); + libp2p_logging::init_tracing_subscriber_with_default_env_filter(); let namespace = rendezvous::Namespace::from_static("some-namespace"); let ([mut alice], mut robert) = new_server_with_connected_clients(rendezvous::server::Config::default()).await; @@ -253,9 +242,7 @@ async fn given_invalid_ttl_then_unsuccessful_registration() { #[tokio::test] async fn discover_allows_for_dial_by_peer_id() { - let _ = tracing_subscriber::fmt() - .with_env_filter(EnvFilter::from_default_env()) - .try_init(); + libp2p_logging::init_tracing_subscriber_with_default_env_filter(); let namespace = rendezvous::Namespace::from_static("some-namespace"); let ([mut alice, mut bob], robert) = new_server_with_connected_clients(rendezvous::server::Config::default()).await; @@ -310,9 +297,7 @@ async fn discover_allows_for_dial_by_peer_id() { #[tokio::test] async fn eve_cannot_register() { - let _ = tracing_subscriber::fmt() - .with_env_filter(EnvFilter::from_default_env()) - .try_init(); + libp2p_logging::init_tracing_subscriber_with_default_env_filter(); let namespace = rendezvous::Namespace::from_static("some-namespace"); let mut robert = new_server(rendezvous::server::Config::default()).await; let mut eve = new_impersonating_client().await; @@ -338,9 +323,7 @@ async fn eve_cannot_register() { // test if charlie can operate as client and server simultaneously #[tokio::test] async fn can_combine_client_and_server() { - let _ = tracing_subscriber::fmt() - .with_env_filter(EnvFilter::from_default_env()) - .try_init(); + libp2p_logging::init_tracing_subscriber_with_default_env_filter(); let namespace = rendezvous::Namespace::from_static("some-namespace"); let ([mut alice], mut robert) = new_server_with_connected_clients(rendezvous::server::Config::default()).await; @@ -376,9 +359,7 @@ async fn can_combine_client_and_server() { #[tokio::test] async fn registration_on_clients_expire() { - let _ = tracing_subscriber::fmt() - .with_env_filter(EnvFilter::from_default_env()) - .try_init(); + libp2p_logging::init_tracing_subscriber_with_default_env_filter(); let namespace = rendezvous::Namespace::from_static("some-namespace"); let ([mut alice, mut bob], robert) = new_server_with_connected_clients(rendezvous::server::Config::default().with_min_ttl(1)) diff --git a/protocols/request-response/Cargo.toml b/protocols/request-response/Cargo.toml index b2e6fd0b0ac..906d1da61c2 100644 --- a/protocols/request-response/Cargo.toml +++ b/protocols/request-response/Cargo.toml @@ -40,7 +40,7 @@ rand = "0.8" libp2p-swarm-test = { path = "../../swarm-test" } futures_ringbuf = "0.4.0" serde = { version = "1.0", features = ["derive"] } -tracing-subscriber = { workspace = true, features = ["env-filter"] } +libp2p-logging = { workspace = true } # Passing arguments to the docsrs builder in order to properly document cfg's. # More information: https://docs.rs/about/builds#cross-compiling diff --git a/protocols/request-response/tests/error_reporting.rs b/protocols/request-response/tests/error_reporting.rs index d1f26378a77..bda13fb5ec5 100644 --- a/protocols/request-response/tests/error_reporting.rs +++ b/protocols/request-response/tests/error_reporting.rs @@ -12,13 +12,10 @@ use libp2p_swarm_test::SwarmExt; use request_response::{ Codec, InboundFailure, InboundRequestId, OutboundFailure, OutboundRequestId, ResponseChannel, }; -use tracing_subscriber::EnvFilter; #[async_std::test] async fn report_outbound_failure_on_read_response() { - let _ = tracing_subscriber::fmt() - .with_env_filter(EnvFilter::from_default_env()) - .try_init(); + libp2p_logging::init_tracing_subscriber_with_default_env_filter(); let (peer1_id, mut swarm1) = new_swarm(); let (peer2_id, mut swarm2) = new_swarm(); @@ -72,10 +69,7 @@ async fn report_outbound_failure_on_read_response() { #[async_std::test] async fn report_outbound_failure_on_write_request() { - let _ = tracing_subscriber::fmt() - .with_env_filter(EnvFilter::from_default_env()) - .try_init(); - + libp2p_logging::init_tracing_subscriber_with_default_env_filter(); let (peer1_id, mut swarm1) = new_swarm(); let (_peer2_id, mut swarm2) = new_swarm(); @@ -115,9 +109,7 @@ async fn report_outbound_failure_on_write_request() { #[async_std::test] async fn report_outbound_timeout_on_read_response() { - let _ = tracing_subscriber::fmt() - .with_env_filter(EnvFilter::from_default_env()) - .try_init(); + libp2p_logging::init_tracing_subscriber_with_default_env_filter(); // `swarm1` needs to have a bigger timeout to avoid racing let (peer1_id, mut swarm1) = new_swarm_with_timeout(Duration::from_millis(200)); @@ -162,9 +154,7 @@ async fn report_outbound_timeout_on_read_response() { #[async_std::test] async fn report_outbound_failure_on_max_streams() { - let _ = tracing_subscriber::fmt() - .with_env_filter(EnvFilter::from_default_env()) - .try_init(); + libp2p_logging::init_tracing_subscriber_with_default_env_filter(); // `swarm2` will be able to handle only 1 stream per time. let swarm2_config = request_response::Config::default() @@ -214,9 +204,7 @@ async fn report_outbound_failure_on_max_streams() { #[async_std::test] async fn report_inbound_failure_on_read_request() { - let _ = tracing_subscriber::fmt() - .with_env_filter(EnvFilter::from_default_env()) - .try_init(); + libp2p_logging::init_tracing_subscriber_with_default_env_filter(); let (peer1_id, mut swarm1) = new_swarm(); let (_peer2_id, mut swarm2) = new_swarm(); @@ -251,9 +239,7 @@ async fn report_inbound_failure_on_read_request() { #[async_std::test] async fn report_inbound_failure_on_write_response() { - let _ = tracing_subscriber::fmt() - .with_env_filter(EnvFilter::from_default_env()) - .try_init(); + libp2p_logging::init_tracing_subscriber_with_default_env_filter(); let (peer1_id, mut swarm1) = new_swarm(); let (peer2_id, mut swarm2) = new_swarm(); @@ -317,9 +303,7 @@ async fn report_inbound_failure_on_write_response() { #[async_std::test] async fn report_inbound_timeout_on_write_response() { - let _ = tracing_subscriber::fmt() - .with_env_filter(EnvFilter::from_default_env()) - .try_init(); + libp2p_logging::init_tracing_subscriber_with_default_env_filter(); // `swarm2` needs to have a bigger timeout to avoid racing let (peer1_id, mut swarm1) = new_swarm_with_timeout(Duration::from_millis(100)); diff --git a/protocols/request-response/tests/peer_address.rs b/protocols/request-response/tests/peer_address.rs index 603e2d09dc0..ac0ee44ab01 100644 --- a/protocols/request-response/tests/peer_address.rs +++ b/protocols/request-response/tests/peer_address.rs @@ -6,14 +6,11 @@ use libp2p_request_response::ProtocolSupport; use libp2p_swarm::{StreamProtocol, Swarm, SwarmEvent}; use libp2p_swarm_test::SwarmExt; use serde::{Deserialize, Serialize}; -use tracing_subscriber::EnvFilter; #[async_std::test] #[cfg(feature = "cbor")] async fn dial_succeeds_after_adding_peers_address() { - let _ = tracing_subscriber::fmt() - .with_env_filter(EnvFilter::from_default_env()) - .try_init(); + libp2p_logging::init_tracing_subscriber_with_default_env_filter(); let protocols = iter::once((StreamProtocol::new("/ping/1"), ProtocolSupport::Full)); let config = request_response::Config::default(); diff --git a/protocols/request-response/tests/ping.rs b/protocols/request-response/tests/ping.rs index e53fe99d6cf..b863fe0251a 100644 --- a/protocols/request-response/tests/ping.rs +++ b/protocols/request-response/tests/ping.rs @@ -30,14 +30,11 @@ use libp2p_swarm::{StreamProtocol, Swarm, SwarmEvent}; use libp2p_swarm_test::SwarmExt; use rand::Rng; use serde::{Deserialize, Serialize}; -use tracing_subscriber::EnvFilter; #[async_std::test] #[cfg(feature = "cbor")] async fn is_response_outbound() { - let _ = tracing_subscriber::fmt() - .with_env_filter(EnvFilter::from_default_env()) - .try_init(); + libp2p_logging::init_tracing_subscriber_with_default_env_filter(); let ping = Ping("ping".to_string().into_bytes()); let offline_peer = PeerId::random(); diff --git a/protocols/stream/Cargo.toml b/protocols/stream/Cargo.toml index d9c9276cb12..173089948b4 100644 --- a/protocols/stream/Cargo.toml +++ b/protocols/stream/Cargo.toml @@ -21,6 +21,7 @@ rand = "0.8" libp2p-swarm-test = { workspace = true } tokio = { workspace = true, features = ["full"] } tracing-subscriber = { workspace = true, features = ["env-filter"] } +libp2p-logging = { workspace = true } [lints] workspace = true diff --git a/protocols/stream/tests/lib.rs b/protocols/stream/tests/lib.rs index cd6caaced5e..bc37428e7c5 100644 --- a/protocols/stream/tests/lib.rs +++ b/protocols/stream/tests/lib.rs @@ -13,15 +13,12 @@ const PROTOCOL: StreamProtocol = StreamProtocol::new("/test"); #[tokio::test] async fn dropping_incoming_streams_deregisters() { - let _ = tracing_subscriber::fmt() - .with_env_filter( - EnvFilter::builder() - .with_default_directive(LevelFilter::DEBUG.into()) - .from_env() - .unwrap(), - ) - .with_test_writer() - .try_init(); + libp2p_logging::init_tracing_subscriber_with_env_filter( + EnvFilter::builder() + .with_default_directive(LevelFilter::DEBUG.into()) + .from_env() + .unwrap(), + ); let mut swarm1 = Swarm::new_ephemeral(|_| stream::Behaviour::new()); let mut swarm2 = Swarm::new_ephemeral(|_| stream::Behaviour::new()); diff --git a/swarm/Cargo.toml b/swarm/Cargo.toml index 4c3b8821ed6..a8173c565af 100644 --- a/swarm/Cargo.toml +++ b/swarm/Cargo.toml @@ -55,7 +55,7 @@ criterion = { version = "0.5", features = ["async_tokio"] } once_cell = "1.19.0" trybuild = "1.0.95" tokio = { workspace = true, features = ["time", "rt", "macros", "rt-multi-thread"] } -tracing-subscriber = { workspace = true, features = ["env-filter"] } +libp2p-logging = { workspace = true } [[test]] name = "swarm_derive" diff --git a/swarm/src/connection.rs b/swarm/src/connection.rs index 32cae54a5ef..f4295802276 100644 --- a/swarm/src/connection.rs +++ b/swarm/src/connection.rs @@ -797,16 +797,13 @@ mod tests { StreamMuxer, }; use quickcheck::*; - use tracing_subscriber::EnvFilter; use super::*; use crate::dummy; #[test] fn max_negotiating_inbound_streams() { - let _ = tracing_subscriber::fmt() - .with_env_filter(EnvFilter::from_default_env()) - .try_init(); + libp2p_logging::init_tracing_subscriber_with_default_env_filter(); fn prop(max_negotiating_inbound_streams: u8) { let max_negotiating_inbound_streams: usize = max_negotiating_inbound_streams.into(); @@ -974,9 +971,7 @@ mod tests { #[test] fn checked_add_fraction_can_add_u64_max() { - let _ = tracing_subscriber::fmt() - .with_env_filter(tracing_subscriber::EnvFilter::from_default_env()) - .try_init(); + libp2p_logging::init_tracing_subscriber_with_default_env_filter(); let start = Instant::now(); let duration = checked_add_fraction(start, Duration::from_secs(u64::MAX)); @@ -986,9 +981,7 @@ mod tests { #[test] fn compute_new_shutdown_does_not_panic() { - let _ = tracing_subscriber::fmt() - .with_env_filter(EnvFilter::from_default_env()) - .try_init(); + libp2p_logging::init_tracing_subscriber_with_default_env_filter(); #[derive(Debug)] struct ArbitraryShutdown(Shutdown); diff --git a/swarm/src/lib.rs b/swarm/src/lib.rs index 639906a1a09..16443133df5 100644 --- a/swarm/src/lib.rs +++ b/swarm/src/lib.rs @@ -2294,9 +2294,7 @@ mod tests { #[tokio::test] async fn aborting_pending_connection_surfaces_error() { - let _ = tracing_subscriber::fmt() - .with_env_filter(tracing_subscriber::EnvFilter::from_default_env()) - .try_init(); + libp2p_logging::init_tracing_subscriber_with_default_env_filter(); let mut dialer = new_test_swarm(Config::with_tokio_executor()); let mut listener = new_test_swarm(Config::with_tokio_executor()); diff --git a/transports/dns/Cargo.toml b/transports/dns/Cargo.toml index 707b67fc935..96ceb219592 100644 --- a/transports/dns/Cargo.toml +++ b/transports/dns/Cargo.toml @@ -25,7 +25,7 @@ tracing = { workspace = true } libp2p-identity = { workspace = true, features = ["rand"] } tokio = { workspace = true, features = ["rt", "time"] } async-std-crate = { package = "async-std", version = "1.6" } -tracing-subscriber = { workspace = true, features = ["env-filter"] } +libp2p-logging = { workspace = true } [features] async-std = ["async-std-resolver"] diff --git a/transports/dns/src/lib.rs b/transports/dns/src/lib.rs index d47f1e464db..8ae886251a7 100644 --- a/transports/dns/src/lib.rs +++ b/transports/dns/src/lib.rs @@ -629,9 +629,7 @@ mod tests { #[test] fn basic_resolve() { - let _ = tracing_subscriber::fmt() - .with_env_filter(tracing_subscriber::EnvFilter::from_default_env()) - .try_init(); + libp2p_logging::init_tracing_subscriber_with_default_env_filter(); #[derive(Clone)] struct CustomTransport; diff --git a/transports/noise/Cargo.toml b/transports/noise/Cargo.toml index 8824adcc50c..94d76affe66 100644 --- a/transports/noise/Cargo.toml +++ b/transports/noise/Cargo.toml @@ -36,7 +36,7 @@ snow = { version = "0.9.5", features = ["default-resolver"], default-features = [dev-dependencies] futures_ringbuf = "0.4.0" quickcheck = { workspace = true } -tracing-subscriber = { workspace = true, features = ["env-filter"] } +libp2p-logging = { workspace = true } libp2p-identity = { workspace = true, features = ["rand"] } # Passing arguments to the docsrs builder in order to properly document cfg's. diff --git a/transports/noise/tests/smoke.rs b/transports/noise/tests/smoke.rs index abc5a038f93..5e8ba2b2e3c 100644 --- a/transports/noise/tests/smoke.rs +++ b/transports/noise/tests/smoke.rs @@ -29,7 +29,6 @@ use libp2p_core::{ use libp2p_identity as identity; use libp2p_noise as noise; use quickcheck::*; -use tracing_subscriber::EnvFilter; #[allow(dead_code)] fn core_upgrade_compat() { @@ -44,9 +43,7 @@ fn core_upgrade_compat() { #[test] fn xx() { - let _ = tracing_subscriber::fmt() - .with_env_filter(EnvFilter::from_default_env()) - .try_init(); + libp2p_logging::init_tracing_subscriber_with_default_env_filter(); fn prop(mut messages: Vec) -> bool { messages.truncate(5); let server_id = identity::Keypair::generate_ed25519(); diff --git a/transports/plaintext/Cargo.toml b/transports/plaintext/Cargo.toml index 47a3191baa9..e378265ecf6 100644 --- a/transports/plaintext/Cargo.toml +++ b/transports/plaintext/Cargo.toml @@ -25,7 +25,7 @@ libp2p-identity = { workspace = true, features = ["ed25519", "rand"] } quickcheck = { workspace = true } rand = "0.8" futures_ringbuf = "0.4.0" -tracing-subscriber = { workspace = true, features = ["env-filter"] } +libp2p-logging = { workspace = true } # Passing arguments to the docsrs builder in order to properly document cfg's. # More information: https://docs.rs/about/builds#cross-compiling diff --git a/transports/plaintext/tests/smoke.rs b/transports/plaintext/tests/smoke.rs index f77f23d3ad3..871ca3b7a7b 100644 --- a/transports/plaintext/tests/smoke.rs +++ b/transports/plaintext/tests/smoke.rs @@ -23,13 +23,10 @@ use libp2p_core::upgrade::InboundConnectionUpgrade; use libp2p_identity as identity; use libp2p_plaintext as plaintext; use quickcheck::QuickCheck; -use tracing_subscriber::EnvFilter; #[test] fn variable_msg_length() { - let _ = tracing_subscriber::fmt() - .with_env_filter(EnvFilter::from_default_env()) - .try_init(); + libp2p_logging::init_tracing_subscriber_with_default_env_filter(); fn prop(msg: Vec) { let msg_to_send = msg.clone(); diff --git a/transports/quic/Cargo.toml b/transports/quic/Cargo.toml index 17d5014b974..b7c0ede0b66 100644 --- a/transports/quic/Cargo.toml +++ b/transports/quic/Cargo.toml @@ -45,7 +45,7 @@ libp2p-tcp = { workspace = true, features = ["async-io"] } libp2p-yamux = { workspace = true } quickcheck = "1" tokio = { workspace = true, features = ["macros", "rt-multi-thread", "time"] } -tracing-subscriber = { workspace = true, features = ["env-filter"] } +libp2p-logging = { workspace = true } [[test]] name = "stream_compliance" diff --git a/transports/quic/tests/smoke.rs b/transports/quic/tests/smoke.rs index 5fbef84649e..92af236f22f 100644 --- a/transports/quic/tests/smoke.rs +++ b/transports/quic/tests/smoke.rs @@ -33,7 +33,6 @@ use libp2p_tcp as tcp; use libp2p_yamux as yamux; use quic::Provider; use rand::RngCore; -use tracing_subscriber::EnvFilter; #[cfg(feature = "tokio")] #[tokio::test] @@ -50,9 +49,7 @@ async fn async_std_smoke() { #[cfg(feature = "tokio")] #[tokio::test] async fn endpoint_reuse() { - let _ = tracing_subscriber::fmt() - .with_env_filter(EnvFilter::from_default_env()) - .try_init(); + libp2p_logging::init_tracing_subscriber_with_default_env_filter(); let (_, mut a_transport) = create_default_transport::(); let (_, mut b_transport) = create_default_transport::(); @@ -77,9 +74,7 @@ async fn endpoint_reuse() { #[cfg(feature = "async-std")] #[async_std::test] async fn ipv4_dial_ipv6() { - let _ = tracing_subscriber::fmt() - .with_env_filter(EnvFilter::from_default_env()) - .try_init(); + libp2p_logging::init_tracing_subscriber_with_default_env_filter(); let (a_peer_id, mut a_transport) = create_default_transport::(); let (b_peer_id, mut b_transport) = create_default_transport::(); @@ -99,9 +94,7 @@ async fn ipv4_dial_ipv6() { async fn wrapped_with_delay() { use libp2p_core::transport::DialOpts; - let _ = tracing_subscriber::fmt() - .with_env_filter(EnvFilter::from_default_env()) - .try_init(); + libp2p_logging::init_tracing_subscriber_with_default_env_filter(); struct DialDelay(Arc>>); @@ -271,9 +264,7 @@ async fn tcp_and_quic() { #[cfg(feature = "async-std")] #[test] fn concurrent_connections_and_streams_async_std() { - let _ = tracing_subscriber::fmt() - .with_env_filter(EnvFilter::from_default_env()) - .try_init(); + libp2p_logging::init_tracing_subscriber_with_default_env_filter(); quickcheck::QuickCheck::new() .min_tests_passed(1) @@ -284,9 +275,7 @@ fn concurrent_connections_and_streams_async_std() { #[cfg(feature = "tokio")] #[test] fn concurrent_connections_and_streams_tokio() { - let _ = tracing_subscriber::fmt() - .with_env_filter(EnvFilter::from_default_env()) - .try_init(); + libp2p_logging::init_tracing_subscriber_with_default_env_filter(); let rt = tokio::runtime::Runtime::new().unwrap(); let _guard = rt.enter(); @@ -303,9 +292,7 @@ async fn draft_29_support() { use futures::{future::poll_fn, select}; use libp2p_core::transport::TransportError; - let _ = tracing_subscriber::fmt() - .with_env_filter(EnvFilter::from_default_env()) - .try_init(); + libp2p_logging::init_tracing_subscriber_with_default_env_filter(); let (_, mut a_transport) = create_transport::(|cfg| cfg.support_draft_29 = true); @@ -380,9 +367,7 @@ async fn draft_29_support() { #[cfg(feature = "async-std")] #[async_std::test] async fn backpressure() { - let _ = tracing_subscriber::fmt() - .with_env_filter(EnvFilter::from_default_env()) - .try_init(); + libp2p_logging::init_tracing_subscriber_with_default_env_filter(); let max_stream_data = quic::Config::new(&generate_tls_keypair()).max_stream_data; let (mut stream_a, mut stream_b) = build_streams::().await; @@ -406,9 +391,7 @@ async fn backpressure() { #[cfg(feature = "async-std")] #[async_std::test] async fn read_after_peer_dropped_stream() { - let _ = tracing_subscriber::fmt() - .with_env_filter(EnvFilter::from_default_env()) - .try_init(); + libp2p_logging::init_tracing_subscriber_with_default_env_filter(); let (mut stream_a, mut stream_b) = build_streams::().await; let data = vec![0; 10]; @@ -428,9 +411,7 @@ async fn read_after_peer_dropped_stream() { #[async_std::test] #[should_panic] async fn write_after_peer_dropped_stream() { - let _ = tracing_subscriber::fmt() - .with_env_filter(EnvFilter::from_default_env()) - .try_init(); + libp2p_logging::init_tracing_subscriber_with_default_env_filter(); let (stream_a, mut stream_b) = build_streams::().await; drop(stream_a); futures_timer::Delay::new(Duration::from_millis(100)).await; @@ -484,9 +465,7 @@ async fn test_local_listener_reuse() { } async fn smoke() { - let _ = tracing_subscriber::fmt() - .with_env_filter(EnvFilter::from_default_env()) - .try_init(); + libp2p_logging::init_tracing_subscriber_with_default_env_filter(); let (a_peer_id, mut a_transport) = create_default_transport::

(); let (b_peer_id, mut b_transport) = create_default_transport::

(); diff --git a/transports/tcp/Cargo.toml b/transports/tcp/Cargo.toml index 03e7fac491c..045b0d598fd 100644 --- a/transports/tcp/Cargo.toml +++ b/transports/tcp/Cargo.toml @@ -30,7 +30,7 @@ async-io = ["dep:async-io", "if-watch/smol"] async-std = { version = "1.6.5", features = ["attributes"] } libp2p-identity = { workspace = true, features = ["rand"] } tokio = { workspace = true, features = ["full"] } -tracing-subscriber = { workspace = true, features = ["env-filter"] } +libp2p-logging = { workspace = true } # Passing arguments to the docsrs builder in order to properly document cfg's. # More information: https://docs.rs/about/builds#cross-compiling diff --git a/transports/tcp/src/lib.rs b/transports/tcp/src/lib.rs index fefa18fb431..44b8c304f00 100644 --- a/transports/tcp/src/lib.rs +++ b/transports/tcp/src/lib.rs @@ -762,9 +762,7 @@ mod tests { #[test] fn communicating_between_dialer_and_listener() { - let _ = tracing_subscriber::fmt() - .with_env_filter(tracing_subscriber::EnvFilter::from_default_env()) - .try_init(); + libp2p_logging::init_tracing_subscriber_with_default_env_filter(); async fn listener(addr: Multiaddr, mut ready_tx: mpsc::Sender) { let mut tcp = Transport::::default().boxed(); @@ -843,9 +841,7 @@ mod tests { #[test] fn wildcard_expansion() { - let _ = tracing_subscriber::fmt() - .with_env_filter(tracing_subscriber::EnvFilter::from_default_env()) - .try_init(); + libp2p_logging::init_tracing_subscriber_with_default_env_filter(); async fn listener(addr: Multiaddr, mut ready_tx: mpsc::Sender) { let mut tcp = Transport::::default().boxed(); @@ -923,9 +919,7 @@ mod tests { #[test] fn port_reuse_dialing() { - let _ = tracing_subscriber::fmt() - .with_env_filter(tracing_subscriber::EnvFilter::from_default_env()) - .try_init(); + libp2p_logging::init_tracing_subscriber_with_default_env_filter(); async fn listener( addr: Multiaddr, @@ -1042,9 +1036,7 @@ mod tests { #[test] fn port_reuse_listening() { - let _ = tracing_subscriber::fmt() - .with_env_filter(tracing_subscriber::EnvFilter::from_default_env()) - .try_init(); + libp2p_logging::init_tracing_subscriber_with_default_env_filter(); async fn listen_twice(addr: Multiaddr) { let mut tcp = Transport::::new(Config::new()); @@ -1098,9 +1090,7 @@ mod tests { #[test] fn listen_port_0() { - let _ = tracing_subscriber::fmt() - .with_env_filter(tracing_subscriber::EnvFilter::from_default_env()) - .try_init(); + libp2p_logging::init_tracing_subscriber_with_default_env_filter(); async fn listen(addr: Multiaddr) -> Multiaddr { let mut tcp = Transport::::default().boxed(); @@ -1135,9 +1125,7 @@ mod tests { #[test] fn listen_invalid_addr() { - let _ = tracing_subscriber::fmt() - .with_env_filter(tracing_subscriber::EnvFilter::from_default_env()) - .try_init(); + libp2p_logging::init_tracing_subscriber_with_default_env_filter(); fn test(addr: Multiaddr) { #[cfg(feature = "async-io")] @@ -1158,9 +1146,7 @@ mod tests { #[test] fn test_remove_listener() { - let _ = tracing_subscriber::fmt() - .with_env_filter(tracing_subscriber::EnvFilter::from_default_env()) - .try_init(); + libp2p_logging::init_tracing_subscriber_with_default_env_filter(); async fn cycle_listeners() -> bool { let mut tcp = Transport::::default().boxed(); diff --git a/transports/webrtc/Cargo.toml b/transports/webrtc/Cargo.toml index 4197a9419d8..8bbad71d16b 100644 --- a/transports/webrtc/Cargo.toml +++ b/transports/webrtc/Cargo.toml @@ -41,8 +41,7 @@ pem = ["webrtc?/pem"] libp2p-identity = { workspace = true, features = ["rand"] } tokio = { workspace = true, features = ["full"] } quickcheck = "1.0.3" -tracing-subscriber = { workspace = true, features = ["env-filter"] } - +libp2p-logging = { workspace = true } [[test]] name = "smoke" diff --git a/transports/webrtc/tests/smoke.rs b/transports/webrtc/tests/smoke.rs index 5f67c09d962..74ca496fafc 100644 --- a/transports/webrtc/tests/smoke.rs +++ b/transports/webrtc/tests/smoke.rs @@ -42,13 +42,10 @@ use libp2p_core::{ use libp2p_identity::PeerId; use libp2p_webrtc as webrtc; use rand::{thread_rng, RngCore}; -use tracing_subscriber::EnvFilter; #[tokio::test] async fn smoke() { - let _ = tracing_subscriber::fmt() - .with_env_filter(EnvFilter::from_default_env()) - .try_init(); + libp2p_logging::init_tracing_subscriber_with_default_env_filter(); let (a_peer_id, mut a_transport) = create_transport(); let (b_peer_id, mut b_transport) = create_transport(); @@ -65,9 +62,7 @@ async fn smoke() { // Note: This test should likely be ported to the muxer compliance test suite. #[test] fn concurrent_connections_and_streams_tokio() { - let _ = tracing_subscriber::fmt() - .with_env_filter(EnvFilter::from_default_env()) - .try_init(); + libp2p_logging::init_tracing_subscriber_with_default_env_filter(); let rt = tokio::runtime::Runtime::new().unwrap(); let _guard = rt.enter(); From 4fa6bfc59ee9d439f9845bd490d5cdf2719de579 Mon Sep 17 00:00:00 2001 From: kamuik16 Date: Sun, 8 Dec 2024 16:59:39 +0530 Subject: [PATCH 2/9] chore: clippy --- misc/logging/src/lib.rs | 4 ++-- protocols/kad/src/handler.rs | 1 + 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/misc/logging/src/lib.rs b/misc/logging/src/lib.rs index 2d0c5c6da18..d0173f76acd 100644 --- a/misc/logging/src/lib.rs +++ b/misc/logging/src/lib.rs @@ -3,13 +3,13 @@ use tracing_subscriber::EnvFilter; pub fn init_tracing_subscriber_with_default_env_filter() { let _ = tracing_subscriber::fmt() .with_env_filter(EnvFilter::from_default_env()) - .with_writer(|| std::io::stderr()) + .with_writer(std::io::stderr) .try_init(); } pub fn init_tracing_subscriber_with_env_filter(filter: impl Into) { let _ = tracing_subscriber::fmt() .with_env_filter(filter) - .with_writer(|| std::io::stderr()) + .with_writer(std::io::stderr) .try_init(); } diff --git a/protocols/kad/src/handler.rs b/protocols/kad/src/handler.rs index 0bfcc1d54df..26f45b08a40 100644 --- a/protocols/kad/src/handler.rs +++ b/protocols/kad/src/handler.rs @@ -1068,6 +1068,7 @@ fn process_kad_response(event: KadResponseMsg, query_id: QueryId) -> HandlerEven #[cfg(test)] mod tests { use quickcheck::{Arbitrary, Gen}; + use super::*; impl Arbitrary for ProtocolStatus { From 9785a9facf656b388e426f149a116d4a201426cc Mon Sep 17 00:00:00 2001 From: kamuik16 Date: Mon, 9 Dec 2024 17:08:19 +0530 Subject: [PATCH 3/9] chore: minor nits --- Cargo.toml | 2 +- misc/logging/CHANGELOG.md | 2 +- rustfmt.toml | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index f2e30af28c2..b5a2ef864bc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -114,7 +114,7 @@ libp2p-websocket = { version = "0.44.1", path = "transports/websocket" } libp2p-websocket-websys = { version = "0.4.1", path = "transports/websocket-websys" } libp2p-webtransport-websys = { version = "0.4.1", path = "transports/webtransport-websys" } libp2p-yamux = { version = "0.46.0", path = "muxers/yamux" } -libp2p-logging = {version = "0.1.0", path = "misc/logging" } +libp2p-logging = { version = "0.1.0", path = "misc/logging" } # External dependencies asynchronous-codec = { version = "0.7.0" } diff --git a/misc/logging/CHANGELOG.md b/misc/logging/CHANGELOG.md index 20b377027c1..bde32c68155 100644 --- a/misc/logging/CHANGELOG.md +++ b/misc/logging/CHANGELOG.md @@ -1,4 +1,4 @@ ## 0.1.0 - Introduce 'libp2p-logging` crate. - See [PR xxxx](https://github.com/libp2p/rust-libp2p/pull/xxxx). \ No newline at end of file + See [PR 5725](https://github.com/libp2p/rust-libp2p/pull/5725). \ No newline at end of file diff --git a/rustfmt.toml b/rustfmt.toml index 1e61bc16abf..fe1850ee986 100644 --- a/rustfmt.toml +++ b/rustfmt.toml @@ -1,5 +1,5 @@ # Imports -reorder_imports = true +reorder_imports = true imports_granularity = "Crate" group_imports = "StdExternalCrate" From 1b12221198e00dc80b1005b2d7f38fb61a21221d Mon Sep 17 00:00:00 2001 From: kamuik16 Date: Tue, 10 Dec 2024 08:30:17 +0530 Subject: [PATCH 4/9] chore: suggestions --- misc/logging/Cargo.toml | 1 - misc/logging/src/lib.rs | 5 +---- misc/server/CHANGELOG.md | 3 +++ protocols/perf/CHANGELOG.md | 3 +++ 4 files changed, 7 insertions(+), 5 deletions(-) diff --git a/misc/logging/Cargo.toml b/misc/logging/Cargo.toml index 149843d6022..5c8662735d7 100644 --- a/misc/logging/Cargo.toml +++ b/misc/logging/Cargo.toml @@ -5,7 +5,6 @@ edition = "2021" authors = ["Krishang Shah "] license = "MIT" repository = "https://github.com/libp2p/rust-libp2p" -publish = false [package.metadata.release] release = false diff --git a/misc/logging/src/lib.rs b/misc/logging/src/lib.rs index d0173f76acd..f8f4aa9d4d8 100644 --- a/misc/logging/src/lib.rs +++ b/misc/logging/src/lib.rs @@ -1,10 +1,7 @@ use tracing_subscriber::EnvFilter; pub fn init_tracing_subscriber_with_default_env_filter() { - let _ = tracing_subscriber::fmt() - .with_env_filter(EnvFilter::from_default_env()) - .with_writer(std::io::stderr) - .try_init(); + init_tracing_subscriber_with_env_filter(EnvFilter::from_default_env()); } pub fn init_tracing_subscriber_with_env_filter(filter: impl Into) { diff --git a/misc/server/CHANGELOG.md b/misc/server/CHANGELOG.md index fe48de0f553..8fff27a624a 100644 --- a/misc/server/CHANGELOG.md +++ b/misc/server/CHANGELOG.md @@ -1,3 +1,6 @@ +- Introduce 'libp2p-logging` crate. + See [PR 5725](https://github.com/libp2p/rust-libp2p/pull/5725). + ## 0.12.8 ### Changed diff --git a/protocols/perf/CHANGELOG.md b/protocols/perf/CHANGELOG.md index c5eda88d97d..3c11cb26e35 100644 --- a/protocols/perf/CHANGELOG.md +++ b/protocols/perf/CHANGELOG.md @@ -1,3 +1,6 @@ +- Introduce 'libp2p-logging` crate. + See [PR 5725](https://github.com/libp2p/rust-libp2p/pull/5725). + ## 0.4.0 From 790e974f170e88afa0c8b2c40a4325c0fe922ece Mon Sep 17 00:00:00 2001 From: kamuik16 Date: Sat, 14 Dec 2024 12:59:16 +0530 Subject: [PATCH 5/9] chore: nits --- Cargo.lock | 4 ++-- libp2p/src/builder/phase.rs | 7 +++---- misc/logging/Cargo.toml | 1 + misc/logging/src/lib.rs | 8 ++++---- misc/multistream-select/src/dialer_select.rs | 2 +- misc/server/CHANGELOG.md | 3 --- misc/server/Cargo.toml | 2 +- misc/server/src/main.rs | 5 ++++- muxers/mplex/benches/split_send_size.rs | 2 +- muxers/mplex/src/io.rs | 4 ++-- protocols/autonat/tests/autonatv2.rs | 8 ++++---- protocols/dcutr/tests/lib.rs | 2 +- protocols/gossipsub/src/behaviour/tests.rs | 2 +- protocols/gossipsub/tests/smoke.rs | 2 +- protocols/identify/tests/smoke.rs | 14 ++++++------- protocols/kad/src/behaviour/test.rs | 2 +- protocols/kad/src/handler.rs | 2 +- protocols/kad/tests/client_mode.rs | 8 ++++---- protocols/mdns/tests/use-async-std.rs | 8 ++++---- protocols/mdns/tests/use-tokio.rs | 6 +++--- protocols/perf/CHANGELOG.md | 3 --- protocols/perf/Cargo.toml | 2 +- protocols/perf/src/bin/perf.rs | 5 ++++- protocols/perf/tests/lib.rs | 2 +- protocols/relay/tests/lib.rs | 14 ++++++------- protocols/rendezvous/tests/rendezvous.rs | 18 ++++++++--------- .../request-response/tests/error_reporting.rs | 14 ++++++------- .../request-response/tests/peer_address.rs | 2 +- protocols/request-response/tests/ping.rs | 2 +- protocols/stream/tests/lib.rs | 2 +- swarm/src/connection.rs | 6 +++--- swarm/src/lib.rs | 13 +++++++----- transports/dns/src/lib.rs | 4 ++-- transports/noise/tests/smoke.rs | 2 +- transports/plaintext/tests/smoke.rs | 2 +- transports/quic/tests/smoke.rs | 20 +++++++++---------- transports/tcp/src/lib.rs | 14 ++++++------- transports/webrtc/tests/smoke.rs | 4 ++-- 38 files changed, 112 insertions(+), 109 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 6cf05ab9658..3a688bd5094 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3126,7 +3126,6 @@ dependencies = [ "libp2p-core", "libp2p-dns", "libp2p-identity", - "libp2p-logging", "libp2p-quic", "libp2p-swarm", "libp2p-swarm-test", @@ -3139,6 +3138,7 @@ dependencies = [ "thiserror 2.0.3", "tokio", "tracing", + "tracing-subscriber", "web-time 1.1.0", ] @@ -3324,13 +3324,13 @@ dependencies = [ "futures", "futures-timer", "libp2p", - "libp2p-logging", "prometheus-client", "serde", "serde_derive", "serde_json", "tokio", "tracing", + "tracing-subscriber", "zeroize", ] diff --git a/libp2p/src/builder/phase.rs b/libp2p/src/builder/phase.rs index 5bb0d948fc1..f8f1672f952 100644 --- a/libp2p/src/builder/phase.rs +++ b/libp2p/src/builder/phase.rs @@ -16,23 +16,22 @@ mod websocket; use bandwidth_logging::*; use bandwidth_metrics::*; +pub use behaviour::BehaviourError; use behaviour::*; use build::*; use dns::*; use libp2p_core::{muxing::StreamMuxerBox, Transport}; use libp2p_identity::Keypair; +pub use other_transport::TransportError; use other_transport::*; use provider::*; use quic::*; use relay::*; use swarm::*; use tcp::*; -use websocket::*; - -pub use behaviour::BehaviourError; -pub use other_transport::TransportError; #[cfg(all(not(target_arch = "wasm32"), feature = "websocket"))] pub use websocket::WebsocketError; +use websocket::*; use super::{ select_muxer::SelectMuxerUpgrade, select_security::SelectSecurityUpgrade, SwarmBuilder, diff --git a/misc/logging/Cargo.toml b/misc/logging/Cargo.toml index 5c8662735d7..149843d6022 100644 --- a/misc/logging/Cargo.toml +++ b/misc/logging/Cargo.toml @@ -5,6 +5,7 @@ edition = "2021" authors = ["Krishang Shah "] license = "MIT" repository = "https://github.com/libp2p/rust-libp2p" +publish = false [package.metadata.release] release = false diff --git a/misc/logging/src/lib.rs b/misc/logging/src/lib.rs index f8f4aa9d4d8..96f068dde29 100644 --- a/misc/logging/src/lib.rs +++ b/misc/logging/src/lib.rs @@ -1,10 +1,10 @@ -use tracing_subscriber::EnvFilter; +pub use tracing_subscriber::EnvFilter; -pub fn init_tracing_subscriber_with_default_env_filter() { - init_tracing_subscriber_with_env_filter(EnvFilter::from_default_env()); +pub fn with_default_env_filter() { + with_env_filter(EnvFilter::from_default_env()); } -pub fn init_tracing_subscriber_with_env_filter(filter: impl Into) { +pub fn with_env_filter(filter: impl Into) { let _ = tracing_subscriber::fmt() .with_env_filter(filter) .with_writer(std::io::stderr) diff --git a/misc/multistream-select/src/dialer_select.rs b/misc/multistream-select/src/dialer_select.rs index 654877ed756..c95bd90c069 100644 --- a/misc/multistream-select/src/dialer_select.rs +++ b/misc/multistream-select/src/dialer_select.rs @@ -275,7 +275,7 @@ mod tests { ListenerProtos(listen_protos): ListenerProtos, DialPayload(dial_payload): DialPayload, ) { - libp2p_logging::init_tracing_subscriber_with_env_filter( + libp2p_logging::with_env_filter( EnvFilter::builder() .with_default_directive(LevelFilter::DEBUG.into()) .from_env_lossy(), diff --git a/misc/server/CHANGELOG.md b/misc/server/CHANGELOG.md index 8fff27a624a..fe48de0f553 100644 --- a/misc/server/CHANGELOG.md +++ b/misc/server/CHANGELOG.md @@ -1,6 +1,3 @@ -- Introduce 'libp2p-logging` crate. - See [PR 5725](https://github.com/libp2p/rust-libp2p/pull/5725). - ## 0.12.8 ### Changed diff --git a/misc/server/Cargo.toml b/misc/server/Cargo.toml index 7e2538e6fdb..0954e2f38d8 100644 --- a/misc/server/Cargo.toml +++ b/misc/server/Cargo.toml @@ -39,7 +39,7 @@ serde_derive = "1.0.125" serde_json = "1.0" tokio = { workspace = true, features = ["rt-multi-thread", "macros"] } tracing = { workspace = true } -libp2p-logging = { workspace = true } +tracing-subscriber = { workspace = true, features = ["env-filter"] } zeroize = "1" [lints] diff --git a/misc/server/src/main.rs b/misc/server/src/main.rs index aeda806ad47..a633a80207e 100644 --- a/misc/server/src/main.rs +++ b/misc/server/src/main.rs @@ -13,6 +13,7 @@ use libp2p::{ tcp, yamux, }; use prometheus_client::{metrics::info::Info, registry::Registry}; +use tracing_subscriber::EnvFilter; use zeroize::Zeroizing; mod behaviour; @@ -41,7 +42,9 @@ struct Opts { #[tokio::main] async fn main() -> Result<(), Box> { - libp2p_logging::init_tracing_subscriber_with_default_env_filter(); + let _ = tracing_subscriber::fmt() + .with_env_filter(EnvFilter::from_default_env()) + .try_init(); let opt = Opts::parse(); diff --git a/muxers/mplex/benches/split_send_size.rs b/muxers/mplex/benches/split_send_size.rs index fa4e770592b..6d656faed61 100644 --- a/muxers/mplex/benches/split_send_size.rs +++ b/muxers/mplex/benches/split_send_size.rs @@ -54,7 +54,7 @@ const BENCH_SIZES: [usize; 8] = [ ]; fn prepare(c: &mut Criterion) { - libp2p_logging::init_tracing_subscriber_with_default_env_filter(); + libp2p_logging::with_default_env_filter(); let payload: Vec = vec![1; 1024 * 1024]; diff --git a/muxers/mplex/src/io.rs b/muxers/mplex/src/io.rs index 76b8edafbba..2454cfeb477 100644 --- a/muxers/mplex/src/io.rs +++ b/muxers/mplex/src/io.rs @@ -1231,7 +1231,7 @@ mod tests { #[test] fn max_buffer_behaviour() { - libp2p_logging::init_tracing_subscriber_with_default_env_filter(); + libp2p_logging::with_default_env_filter(); fn prop(cfg: MplexConfig, overflow: NonZeroU8) { let mut r_buf = BytesMut::new(); @@ -1366,7 +1366,7 @@ mod tests { #[test] fn close_on_error() { - libp2p_logging::init_tracing_subscriber_with_default_env_filter(); + libp2p_logging::with_default_env_filter(); fn prop(cfg: MplexConfig, num_streams: NonZeroU8) { let num_streams = cmp::min(cfg.max_substreams, num_streams.get() as usize); diff --git a/protocols/autonat/tests/autonatv2.rs b/protocols/autonat/tests/autonatv2.rs index 42f18e74c9a..d6d5e72a04e 100644 --- a/protocols/autonat/tests/autonatv2.rs +++ b/protocols/autonat/tests/autonatv2.rs @@ -14,7 +14,7 @@ use tokio::sync::oneshot; #[tokio::test] async fn confirm_successful() { - libp2p_logging::init_tracing_subscriber_with_default_env_filter(); + libp2p_logging::with_default_env_filter(); let (mut alice, mut bob) = start_and_connect().await; let cor_server_peer = *alice.local_peer_id(); @@ -125,7 +125,7 @@ async fn confirm_successful() { #[tokio::test] async fn dial_back_to_unsupported_protocol() { - libp2p_logging::init_tracing_subscriber_with_default_env_filter(); + libp2p_logging::with_default_env_filter(); let (mut alice, mut bob) = bootstrap().await; let alice_peer_id = *alice.local_peer_id(); @@ -221,7 +221,7 @@ async fn dial_back_to_unsupported_protocol() { #[tokio::test] async fn dial_back_to_non_libp2p() { - libp2p_logging::init_tracing_subscriber_with_default_env_filter(); + libp2p_logging::with_default_env_filter(); let (mut alice, mut bob) = bootstrap().await; let alice_peer_id = *alice.local_peer_id(); @@ -307,7 +307,7 @@ async fn dial_back_to_non_libp2p() { #[tokio::test] async fn dial_back_to_not_supporting() { - libp2p_logging::init_tracing_subscriber_with_default_env_filter(); + libp2p_logging::with_default_env_filter(); let (mut alice, mut bob) = bootstrap().await; let alice_peer_id = *alice.local_peer_id(); diff --git a/protocols/dcutr/tests/lib.rs b/protocols/dcutr/tests/lib.rs index 4ecccd4e172..44a168aad25 100644 --- a/protocols/dcutr/tests/lib.rs +++ b/protocols/dcutr/tests/lib.rs @@ -35,7 +35,7 @@ use libp2p_swarm_test::SwarmExt as _; #[tokio::test] async fn connect() { - libp2p_logging::init_tracing_subscriber_with_default_env_filter(); + libp2p_logging::with_default_env_filter(); let mut relay = build_relay(); let mut dst = build_client(); diff --git a/protocols/gossipsub/src/behaviour/tests.rs b/protocols/gossipsub/src/behaviour/tests.rs index c5c1920571e..f45ad55e6ca 100644 --- a/protocols/gossipsub/src/behaviour/tests.rs +++ b/protocols/gossipsub/src/behaviour/tests.rs @@ -4761,7 +4761,7 @@ fn test_limit_number_of_message_ids_inside_ihave() { #[test] fn test_iwant_penalties() { - libp2p_logging::init_tracing_subscriber_with_default_env_filter(); + libp2p_logging::with_default_env_filter(); let config = ConfigBuilder::default() .iwant_followup_time(Duration::from_secs(4)) .build() diff --git a/protocols/gossipsub/tests/smoke.rs b/protocols/gossipsub/tests/smoke.rs index fdd0381711c..3475ee49212 100644 --- a/protocols/gossipsub/tests/smoke.rs +++ b/protocols/gossipsub/tests/smoke.rs @@ -131,7 +131,7 @@ async fn build_node() -> Swarm { #[test] fn multi_hop_propagation() { - libp2p_logging::init_tracing_subscriber_with_default_env_filter(); + libp2p_logging::with_default_env_filter(); fn prop(num_nodes: u8, seed: u64) -> TestResult { if !(2..=50).contains(&num_nodes) { diff --git a/protocols/identify/tests/smoke.rs b/protocols/identify/tests/smoke.rs index 3b16c2a6b47..8ebc9fdb459 100644 --- a/protocols/identify/tests/smoke.rs +++ b/protocols/identify/tests/smoke.rs @@ -12,7 +12,7 @@ use libp2p_swarm_test::SwarmExt; #[async_std::test] async fn periodic_identify() { - libp2p_logging::init_tracing_subscriber_with_default_env_filter(); + libp2p_logging::with_default_env_filter(); let mut swarm1 = Swarm::new_ephemeral(|identity| { identify::Behaviour::new( @@ -81,7 +81,7 @@ async fn periodic_identify() { } #[async_std::test] async fn only_emits_address_candidate_once_per_connection() { - libp2p_logging::init_tracing_subscriber_with_default_env_filter(); + libp2p_logging::with_default_env_filter(); let mut swarm1 = Swarm::new_ephemeral(|identity| { identify::Behaviour::new( @@ -151,7 +151,7 @@ async fn only_emits_address_candidate_once_per_connection() { #[async_std::test] async fn emits_unique_listen_addresses() { - libp2p_logging::init_tracing_subscriber_with_default_env_filter(); + libp2p_logging::with_default_env_filter(); let mut swarm1 = Swarm::new_ephemeral(|identity| { identify::Behaviour::new( @@ -221,7 +221,7 @@ async fn emits_unique_listen_addresses() { #[async_std::test] async fn hides_listen_addresses() { - libp2p_logging::init_tracing_subscriber_with_default_env_filter(); + libp2p_logging::with_default_env_filter(); let mut swarm1 = Swarm::new_ephemeral(|identity| { identify::Behaviour::new( @@ -290,7 +290,7 @@ async fn hides_listen_addresses() { #[async_std::test] async fn identify_push() { - libp2p_logging::init_tracing_subscriber_with_default_env_filter(); + libp2p_logging::with_default_env_filter(); let mut swarm1 = Swarm::new_ephemeral(|identity| { identify::Behaviour::new(identify::Config::new("a".to_string(), identity.public())) @@ -340,7 +340,7 @@ async fn identify_push() { #[async_std::test] async fn discover_peer_after_disconnect() { - libp2p_logging::init_tracing_subscriber_with_default_env_filter(); + libp2p_logging::with_default_env_filter(); let mut swarm1 = Swarm::new_ephemeral(|identity| { identify::Behaviour::new(identify::Config::new("a".to_string(), identity.public())) @@ -391,7 +391,7 @@ async fn discover_peer_after_disconnect() { #[async_std::test] async fn configured_interval_starts_after_first_identify() { - libp2p_logging::init_tracing_subscriber_with_default_env_filter(); + libp2p_logging::with_default_env_filter(); let identify_interval = Duration::from_secs(5); diff --git a/protocols/kad/src/behaviour/test.rs b/protocols/kad/src/behaviour/test.rs index 99095a321df..c2de5982933 100644 --- a/protocols/kad/src/behaviour/test.rs +++ b/protocols/kad/src/behaviour/test.rs @@ -323,7 +323,7 @@ fn query_iter() { #[test] fn unresponsive_not_returned_direct() { - libp2p_logging::init_tracing_subscriber_with_default_env_filter(); + libp2p_logging::with_default_env_filter(); // Build one node. It contains fake addresses to non-existing nodes. We ask it to find a // random peer. We make sure that no fake address is returned. diff --git a/protocols/kad/src/handler.rs b/protocols/kad/src/handler.rs index 26f45b08a40..cd7b82c4703 100644 --- a/protocols/kad/src/handler.rs +++ b/protocols/kad/src/handler.rs @@ -1082,7 +1082,7 @@ mod tests { #[test] fn compute_next_protocol_status_test() { - libp2p_logging::init_tracing_subscriber_with_default_env_filter(); + libp2p_logging::with_default_env_filter(); fn prop(now_supported: bool, current: Option) { let new = compute_new_protocol_status(now_supported, current); diff --git a/protocols/kad/tests/client_mode.rs b/protocols/kad/tests/client_mode.rs index 591ebb62dc9..42af83debe8 100644 --- a/protocols/kad/tests/client_mode.rs +++ b/protocols/kad/tests/client_mode.rs @@ -8,7 +8,7 @@ use MyBehaviourEvent::*; #[async_std::test] async fn server_gets_added_to_routing_table_by_client() { - libp2p_logging::init_tracing_subscriber_with_default_env_filter(); + libp2p_logging::with_default_env_filter(); let mut client = Swarm::new_ephemeral(MyBehaviour::new); let mut server = Swarm::new_ephemeral(MyBehaviour::new); @@ -38,7 +38,7 @@ async fn server_gets_added_to_routing_table_by_client() { #[async_std::test] async fn two_servers_add_each_other_to_routing_table() { - libp2p_logging::init_tracing_subscriber_with_default_env_filter(); + libp2p_logging::with_default_env_filter(); let mut server1 = Swarm::new_ephemeral(MyBehaviour::new); let mut server2 = Swarm::new_ephemeral(MyBehaviour::new); @@ -77,7 +77,7 @@ async fn two_servers_add_each_other_to_routing_table() { #[async_std::test] async fn adding_an_external_addresses_activates_server_mode_on_existing_connections() { - libp2p_logging::init_tracing_subscriber_with_default_env_filter(); + libp2p_logging::with_default_env_filter(); let mut client = Swarm::new_ephemeral(MyBehaviour::new); let mut server = Swarm::new_ephemeral(MyBehaviour::new); @@ -113,7 +113,7 @@ async fn adding_an_external_addresses_activates_server_mode_on_existing_connecti #[async_std::test] async fn set_client_to_server_mode() { - libp2p_logging::init_tracing_subscriber_with_default_env_filter(); + libp2p_logging::with_default_env_filter(); let mut client = Swarm::new_ephemeral(MyBehaviour::new); client.behaviour_mut().kad.set_mode(Some(Mode::Client)); diff --git a/protocols/mdns/tests/use-async-std.rs b/protocols/mdns/tests/use-async-std.rs index db7639a9cc9..1861f53ea1b 100644 --- a/protocols/mdns/tests/use-async-std.rs +++ b/protocols/mdns/tests/use-async-std.rs @@ -27,14 +27,14 @@ use libp2p_swarm_test::SwarmExt as _; #[async_std::test] async fn test_discovery_async_std_ipv4() { - libp2p_logging::init_tracing_subscriber_with_default_env_filter(); + libp2p_logging::with_default_env_filter(); run_discovery_test(Config::default()).await } #[async_std::test] async fn test_discovery_async_std_ipv6() { - libp2p_logging::init_tracing_subscriber_with_default_env_filter(); + libp2p_logging::with_default_env_filter(); let config = Config { enable_ipv6: true, @@ -45,7 +45,7 @@ async fn test_discovery_async_std_ipv6() { #[async_std::test] async fn test_expired_async_std() { - libp2p_logging::init_tracing_subscriber_with_default_env_filter(); + libp2p_logging::with_default_env_filter(); let config = Config { ttl: Duration::from_secs(1), @@ -78,7 +78,7 @@ async fn test_expired_async_std() { #[async_std::test] async fn test_no_expiration_on_close_async_std() { - libp2p_logging::init_tracing_subscriber_with_default_env_filter(); + libp2p_logging::with_default_env_filter(); let config = Config { ttl: Duration::from_secs(120), query_interval: Duration::from_secs(10), diff --git a/protocols/mdns/tests/use-tokio.rs b/protocols/mdns/tests/use-tokio.rs index ff27fb6c047..bbdbe3864d4 100644 --- a/protocols/mdns/tests/use-tokio.rs +++ b/protocols/mdns/tests/use-tokio.rs @@ -26,14 +26,14 @@ use libp2p_swarm_test::SwarmExt as _; #[tokio::test] async fn test_discovery_tokio_ipv4() { - libp2p_logging::init_tracing_subscriber_with_default_env_filter(); + libp2p_logging::with_default_env_filter(); run_discovery_test(Config::default()).await } #[tokio::test] async fn test_discovery_tokio_ipv6() { - libp2p_logging::init_tracing_subscriber_with_default_env_filter(); + libp2p_logging::with_default_env_filter(); let config = Config { enable_ipv6: true, @@ -44,7 +44,7 @@ async fn test_discovery_tokio_ipv6() { #[tokio::test] async fn test_expired_tokio() { - libp2p_logging::init_tracing_subscriber_with_default_env_filter(); + libp2p_logging::with_default_env_filter(); let config = Config { ttl: Duration::from_secs(1), diff --git a/protocols/perf/CHANGELOG.md b/protocols/perf/CHANGELOG.md index 3c11cb26e35..c5eda88d97d 100644 --- a/protocols/perf/CHANGELOG.md +++ b/protocols/perf/CHANGELOG.md @@ -1,6 +1,3 @@ -- Introduce 'libp2p-logging` crate. - See [PR 5725](https://github.com/libp2p/rust-libp2p/pull/5725). - ## 0.4.0 diff --git a/protocols/perf/Cargo.toml b/protocols/perf/Cargo.toml index 649183566fc..cd499a8c949 100644 --- a/protocols/perf/Cargo.toml +++ b/protocols/perf/Cargo.toml @@ -30,7 +30,7 @@ serde = { version = "1.0", features = ["derive"] } serde_json = "1.0" thiserror = { workspace = true } tracing = { workspace = true } -libp2p-logging = { workspace = true } +tracing-subscriber = { workspace = true, features = ["env-filter"] } tokio = { workspace = true, features = ["macros", "rt", "rt-multi-thread"] } [dev-dependencies] diff --git a/protocols/perf/src/bin/perf.rs b/protocols/perf/src/bin/perf.rs index d1c22911c80..506455f081a 100644 --- a/protocols/perf/src/bin/perf.rs +++ b/protocols/perf/src/bin/perf.rs @@ -31,6 +31,7 @@ use libp2p::{ }; use libp2p_perf::{client, server, Final, Intermediate, Run, RunParams, RunUpdate}; use serde::{Deserialize, Serialize}; +use tracing_subscriber::EnvFilter; use web_time::{Duration, Instant}; #[derive(Debug, Parser)] @@ -71,7 +72,9 @@ impl FromStr for Transport { #[tokio::main] async fn main() -> Result<()> { - libp2p_logging::init_tracing_subscriber_with_default_env_filter(); + let _ = tracing_subscriber::fmt() + .with_env_filter(EnvFilter::from_default_env()) + .try_init(); let opts = Opts::parse(); match opts { diff --git a/protocols/perf/tests/lib.rs b/protocols/perf/tests/lib.rs index 1b9b3b08889..cbdecf18608 100644 --- a/protocols/perf/tests/lib.rs +++ b/protocols/perf/tests/lib.rs @@ -27,7 +27,7 @@ use libp2p_swarm_test::SwarmExt; #[tokio::test] async fn perf() { - libp2p_logging::init_tracing_subscriber_with_default_env_filter(); + libp2p_logging::with_default_env_filter(); let mut server = Swarm::new_ephemeral(|_| server::Behaviour::new()); let server_peer_id = *server.local_peer_id(); diff --git a/protocols/relay/tests/lib.rs b/protocols/relay/tests/lib.rs index d94ac5dfda2..d5b19e81849 100644 --- a/protocols/relay/tests/lib.rs +++ b/protocols/relay/tests/lib.rs @@ -43,7 +43,7 @@ use libp2p_swarm_test::SwarmExt; #[test] fn reservation() { - libp2p_logging::init_tracing_subscriber_with_default_env_filter(); + libp2p_logging::with_default_env_filter(); let mut pool = LocalPool::new(); let relay_addr = Multiaddr::empty().with(Protocol::Memory(rand::random::())); @@ -84,7 +84,7 @@ fn reservation() { #[test] fn new_reservation_to_same_relay_replaces_old() { - libp2p_logging::init_tracing_subscriber_with_default_env_filter(); + libp2p_logging::with_default_env_filter(); let mut pool = LocalPool::new(); let relay_addr = Multiaddr::empty().with(Protocol::Memory(rand::random::())); @@ -181,7 +181,7 @@ fn new_reservation_to_same_relay_replaces_old() { #[test] fn connect() { - libp2p_logging::init_tracing_subscriber_with_default_env_filter(); + libp2p_logging::with_default_env_filter(); let mut pool = LocalPool::new(); let relay_addr = Multiaddr::empty().with(Protocol::Memory(rand::random::())); @@ -265,7 +265,7 @@ async fn connection_established_to( #[test] fn handle_dial_failure() { - libp2p_logging::init_tracing_subscriber_with_default_env_filter(); + libp2p_logging::with_default_env_filter(); let mut pool = LocalPool::new(); let relay_addr = Multiaddr::empty().with(Protocol::Memory(rand::random::())); @@ -284,7 +284,7 @@ fn handle_dial_failure() { #[test] fn propagate_reservation_error_to_listener() { - libp2p_logging::init_tracing_subscriber_with_default_env_filter(); + libp2p_logging::with_default_env_filter(); let mut pool = LocalPool::new(); let relay_addr = Multiaddr::empty().with(Protocol::Memory(rand::random::())); @@ -331,7 +331,7 @@ fn propagate_reservation_error_to_listener() { #[test] fn propagate_connect_error_to_unknown_peer_to_dialer() { - libp2p_logging::init_tracing_subscriber_with_default_env_filter(); + libp2p_logging::with_default_env_filter(); let mut pool = LocalPool::new(); let relay_addr = Multiaddr::empty().with(Protocol::Memory(rand::random::())); @@ -385,7 +385,7 @@ fn propagate_connect_error_to_unknown_peer_to_dialer() { #[test] fn reuse_connection() { - libp2p_logging::init_tracing_subscriber_with_default_env_filter(); + libp2p_logging::with_default_env_filter(); let mut pool = LocalPool::new(); let relay_addr = Multiaddr::empty().with(Protocol::Memory(rand::random::())); diff --git a/protocols/rendezvous/tests/rendezvous.rs b/protocols/rendezvous/tests/rendezvous.rs index 617b2afafa1..f7314d0ec1a 100644 --- a/protocols/rendezvous/tests/rendezvous.rs +++ b/protocols/rendezvous/tests/rendezvous.rs @@ -30,7 +30,7 @@ use libp2p_swarm_test::SwarmExt; #[tokio::test] async fn given_successful_registration_then_successful_discovery() { - libp2p_logging::init_tracing_subscriber_with_default_env_filter(); + libp2p_logging::with_default_env_filter(); let namespace = rendezvous::Namespace::from_static("some-namespace"); let ([mut alice, mut bob], mut robert) = new_server_with_connected_clients(rendezvous::server::Config::default()).await; @@ -83,7 +83,7 @@ async fn given_successful_registration_then_successful_discovery() { #[tokio::test] async fn should_return_error_when_no_external_addresses() { - libp2p_logging::init_tracing_subscriber_with_default_env_filter(); + libp2p_logging::with_default_env_filter(); let namespace = rendezvous::Namespace::from_static("some-namespace"); let server = new_server(rendezvous::server::Config::default()).await; let mut client = Swarm::new_ephemeral(rendezvous::client::Behaviour::new); @@ -98,7 +98,7 @@ async fn should_return_error_when_no_external_addresses() { #[tokio::test] async fn given_successful_registration_then_refresh_ttl() { - libp2p_logging::init_tracing_subscriber_with_default_env_filter(); + libp2p_logging::with_default_env_filter(); let namespace = rendezvous::Namespace::from_static("some-namespace"); let ([mut alice, mut bob], mut robert) = new_server_with_connected_clients(rendezvous::server::Config::default()).await; @@ -164,7 +164,7 @@ async fn given_successful_registration_then_refresh_ttl() { #[tokio::test] async fn given_successful_registration_then_refresh_external_addrs() { - libp2p_logging::init_tracing_subscriber_with_default_env_filter(); + libp2p_logging::with_default_env_filter(); let namespace = rendezvous::Namespace::from_static("some-namespace"); let ([mut alice], mut robert) = new_server_with_connected_clients(rendezvous::server::Config::default()).await; @@ -215,7 +215,7 @@ async fn given_successful_registration_then_refresh_external_addrs() { #[tokio::test] async fn given_invalid_ttl_then_unsuccessful_registration() { - libp2p_logging::init_tracing_subscriber_with_default_env_filter(); + libp2p_logging::with_default_env_filter(); let namespace = rendezvous::Namespace::from_static("some-namespace"); let ([mut alice], mut robert) = new_server_with_connected_clients(rendezvous::server::Config::default()).await; @@ -242,7 +242,7 @@ async fn given_invalid_ttl_then_unsuccessful_registration() { #[tokio::test] async fn discover_allows_for_dial_by_peer_id() { - libp2p_logging::init_tracing_subscriber_with_default_env_filter(); + libp2p_logging::with_default_env_filter(); let namespace = rendezvous::Namespace::from_static("some-namespace"); let ([mut alice, mut bob], robert) = new_server_with_connected_clients(rendezvous::server::Config::default()).await; @@ -297,7 +297,7 @@ async fn discover_allows_for_dial_by_peer_id() { #[tokio::test] async fn eve_cannot_register() { - libp2p_logging::init_tracing_subscriber_with_default_env_filter(); + libp2p_logging::with_default_env_filter(); let namespace = rendezvous::Namespace::from_static("some-namespace"); let mut robert = new_server(rendezvous::server::Config::default()).await; let mut eve = new_impersonating_client().await; @@ -323,7 +323,7 @@ async fn eve_cannot_register() { // test if charlie can operate as client and server simultaneously #[tokio::test] async fn can_combine_client_and_server() { - libp2p_logging::init_tracing_subscriber_with_default_env_filter(); + libp2p_logging::with_default_env_filter(); let namespace = rendezvous::Namespace::from_static("some-namespace"); let ([mut alice], mut robert) = new_server_with_connected_clients(rendezvous::server::Config::default()).await; @@ -359,7 +359,7 @@ async fn can_combine_client_and_server() { #[tokio::test] async fn registration_on_clients_expire() { - libp2p_logging::init_tracing_subscriber_with_default_env_filter(); + libp2p_logging::with_default_env_filter(); let namespace = rendezvous::Namespace::from_static("some-namespace"); let ([mut alice, mut bob], robert) = new_server_with_connected_clients(rendezvous::server::Config::default().with_min_ttl(1)) diff --git a/protocols/request-response/tests/error_reporting.rs b/protocols/request-response/tests/error_reporting.rs index ad73e16a426..fd0ebd15ee7 100644 --- a/protocols/request-response/tests/error_reporting.rs +++ b/protocols/request-response/tests/error_reporting.rs @@ -15,7 +15,7 @@ use request_response::{ #[async_std::test] async fn report_outbound_failure_on_read_response() { - libp2p_logging::init_tracing_subscriber_with_default_env_filter(); + libp2p_logging::with_default_env_filter(); let (peer1_id, mut swarm1) = new_swarm(); let (peer2_id, mut swarm2) = new_swarm(); @@ -69,7 +69,7 @@ async fn report_outbound_failure_on_read_response() { #[async_std::test] async fn report_outbound_failure_on_write_request() { - libp2p_logging::init_tracing_subscriber_with_default_env_filter(); + libp2p_logging::with_default_env_filter(); let (peer1_id, mut swarm1) = new_swarm(); let (_peer2_id, mut swarm2) = new_swarm(); @@ -109,7 +109,7 @@ async fn report_outbound_failure_on_write_request() { #[async_std::test] async fn report_outbound_timeout_on_read_response() { - libp2p_logging::init_tracing_subscriber_with_default_env_filter(); + libp2p_logging::with_default_env_filter(); // `swarm1` needs to have a bigger timeout to avoid racing let (peer1_id, mut swarm1) = new_swarm_with_timeout(Duration::from_millis(200)); @@ -154,7 +154,7 @@ async fn report_outbound_timeout_on_read_response() { #[async_std::test] async fn report_outbound_failure_on_max_streams() { - libp2p_logging::init_tracing_subscriber_with_default_env_filter(); + libp2p_logging::with_default_env_filter(); // `swarm2` will be able to handle only 1 stream per time. let swarm2_config = request_response::Config::default() @@ -204,7 +204,7 @@ async fn report_outbound_failure_on_max_streams() { #[async_std::test] async fn report_inbound_failure_on_read_request() { - libp2p_logging::init_tracing_subscriber_with_default_env_filter(); + libp2p_logging::with_default_env_filter(); let (peer1_id, mut swarm1) = new_swarm(); let (_peer2_id, mut swarm2) = new_swarm(); @@ -239,7 +239,7 @@ async fn report_inbound_failure_on_read_request() { #[async_std::test] async fn report_inbound_failure_on_write_response() { - libp2p_logging::init_tracing_subscriber_with_default_env_filter(); + libp2p_logging::with_default_env_filter(); let (peer1_id, mut swarm1) = new_swarm(); let (peer2_id, mut swarm2) = new_swarm(); @@ -303,7 +303,7 @@ async fn report_inbound_failure_on_write_response() { #[async_std::test] async fn report_inbound_timeout_on_write_response() { - libp2p_logging::init_tracing_subscriber_with_default_env_filter(); + libp2p_logging::with_default_env_filter(); // `swarm2` needs to have a bigger timeout to avoid racing let (peer1_id, mut swarm1) = new_swarm_with_timeout(Duration::from_millis(100)); diff --git a/protocols/request-response/tests/peer_address.rs b/protocols/request-response/tests/peer_address.rs index ac0ee44ab01..3f9a6849175 100644 --- a/protocols/request-response/tests/peer_address.rs +++ b/protocols/request-response/tests/peer_address.rs @@ -10,7 +10,7 @@ use serde::{Deserialize, Serialize}; #[async_std::test] #[cfg(feature = "cbor")] async fn dial_succeeds_after_adding_peers_address() { - libp2p_logging::init_tracing_subscriber_with_default_env_filter(); + libp2p_logging::with_default_env_filter(); let protocols = iter::once((StreamProtocol::new("/ping/1"), ProtocolSupport::Full)); let config = request_response::Config::default(); diff --git a/protocols/request-response/tests/ping.rs b/protocols/request-response/tests/ping.rs index ea6ceb44092..94a3a7aaab5 100644 --- a/protocols/request-response/tests/ping.rs +++ b/protocols/request-response/tests/ping.rs @@ -34,7 +34,7 @@ use serde::{Deserialize, Serialize}; #[async_std::test] #[cfg(feature = "cbor")] async fn is_response_outbound() { - libp2p_logging::init_tracing_subscriber_with_default_env_filter(); + libp2p_logging::with_default_env_filter(); let ping = Ping("ping".to_string().into_bytes()); let offline_peer = PeerId::random(); diff --git a/protocols/stream/tests/lib.rs b/protocols/stream/tests/lib.rs index bc37428e7c5..0a0fa7ffd66 100644 --- a/protocols/stream/tests/lib.rs +++ b/protocols/stream/tests/lib.rs @@ -13,7 +13,7 @@ const PROTOCOL: StreamProtocol = StreamProtocol::new("/test"); #[tokio::test] async fn dropping_incoming_streams_deregisters() { - libp2p_logging::init_tracing_subscriber_with_env_filter( + libp2p_logging::with_env_filter( EnvFilter::builder() .with_default_directive(LevelFilter::DEBUG.into()) .from_env() diff --git a/swarm/src/connection.rs b/swarm/src/connection.rs index f4295802276..90ae21a1a56 100644 --- a/swarm/src/connection.rs +++ b/swarm/src/connection.rs @@ -803,7 +803,7 @@ mod tests { #[test] fn max_negotiating_inbound_streams() { - libp2p_logging::init_tracing_subscriber_with_default_env_filter(); + libp2p_logging::with_default_env_filter(); fn prop(max_negotiating_inbound_streams: u8) { let max_negotiating_inbound_streams: usize = max_negotiating_inbound_streams.into(); @@ -971,7 +971,7 @@ mod tests { #[test] fn checked_add_fraction_can_add_u64_max() { - libp2p_logging::init_tracing_subscriber_with_default_env_filter(); + libp2p_logging::with_default_env_filter(); let start = Instant::now(); let duration = checked_add_fraction(start, Duration::from_secs(u64::MAX)); @@ -981,7 +981,7 @@ mod tests { #[test] fn compute_new_shutdown_does_not_panic() { - libp2p_logging::init_tracing_subscriber_with_default_env_filter(); + libp2p_logging::with_default_env_filter(); #[derive(Debug)] struct ArbitraryShutdown(Shutdown); diff --git a/swarm/src/lib.rs b/swarm/src/lib.rs index f5eed7ae407..a666c22f911 100644 --- a/swarm/src/lib.rs +++ b/swarm/src/lib.rs @@ -1497,14 +1497,17 @@ impl Config { /// /// Defaults to 10s. /// - /// Typically, you shouldn't _need_ to modify this default as connections will be kept alive whilst they are "in use" (see below). - /// Depending on the application's usecase, it may be desirable to keep connections alive despite them not being in use. + /// Typically, you shouldn't _need_ to modify this default as connections will be kept alive + /// whilst they are "in use" (see below). Depending on the application's usecase, it may be + /// desirable to keep connections alive despite them not being in use. /// /// A connection is considered idle if: /// - There are no active inbound streams. /// - There are no active outbounds streams. - /// - There are no pending outbound streams (i.e. all streams requested via [`ConnectionHandlerEvent::OutboundSubstreamRequest`] have completed). - /// - Every [`ConnectionHandler`] returns `false` from [`ConnectionHandler::connection_keep_alive`]. + /// - There are no pending outbound streams (i.e. all streams requested via + /// [`ConnectionHandlerEvent::OutboundSubstreamRequest`] have completed). + /// - Every [`ConnectionHandler`] returns `false` from + /// [`ConnectionHandler::connection_keep_alive`]. /// /// Once all these conditions are true, the idle connection timeout starts ticking. pub fn with_idle_connection_timeout(mut self, timeout: Duration) -> Self { @@ -2300,7 +2303,7 @@ mod tests { #[tokio::test] async fn aborting_pending_connection_surfaces_error() { - libp2p_logging::init_tracing_subscriber_with_default_env_filter(); + libp2p_logging::with_default_env_filter(); let mut dialer = new_test_swarm(Config::with_tokio_executor()); let mut listener = new_test_swarm(Config::with_tokio_executor()); diff --git a/transports/dns/src/lib.rs b/transports/dns/src/lib.rs index cd31c038d7c..6ae44bd848f 100644 --- a/transports/dns/src/lib.rs +++ b/transports/dns/src/lib.rs @@ -160,7 +160,7 @@ use async_trait::async_trait; use futures::{future::BoxFuture, prelude::*}; pub use hickory_resolver::{ config::{ResolverConfig, ResolverOpts}, - {ResolveError, ResolveErrorKind}, + ResolveError, ResolveErrorKind, }; use hickory_resolver::{ lookup::{Ipv4Lookup, Ipv6Lookup, TxtLookup}, @@ -629,7 +629,7 @@ mod tests { #[test] fn basic_resolve() { - libp2p_logging::init_tracing_subscriber_with_default_env_filter(); + libp2p_logging::with_default_env_filter(); #[derive(Clone)] struct CustomTransport; diff --git a/transports/noise/tests/smoke.rs b/transports/noise/tests/smoke.rs index 5e8ba2b2e3c..6d8f05b4303 100644 --- a/transports/noise/tests/smoke.rs +++ b/transports/noise/tests/smoke.rs @@ -43,7 +43,7 @@ fn core_upgrade_compat() { #[test] fn xx() { - libp2p_logging::init_tracing_subscriber_with_default_env_filter(); + libp2p_logging::with_default_env_filter(); fn prop(mut messages: Vec) -> bool { messages.truncate(5); let server_id = identity::Keypair::generate_ed25519(); diff --git a/transports/plaintext/tests/smoke.rs b/transports/plaintext/tests/smoke.rs index 871ca3b7a7b..f6648b117cc 100644 --- a/transports/plaintext/tests/smoke.rs +++ b/transports/plaintext/tests/smoke.rs @@ -26,7 +26,7 @@ use quickcheck::QuickCheck; #[test] fn variable_msg_length() { - libp2p_logging::init_tracing_subscriber_with_default_env_filter(); + libp2p_logging::with_default_env_filter(); fn prop(msg: Vec) { let msg_to_send = msg.clone(); diff --git a/transports/quic/tests/smoke.rs b/transports/quic/tests/smoke.rs index 92af236f22f..e87cd451b06 100644 --- a/transports/quic/tests/smoke.rs +++ b/transports/quic/tests/smoke.rs @@ -49,7 +49,7 @@ async fn async_std_smoke() { #[cfg(feature = "tokio")] #[tokio::test] async fn endpoint_reuse() { - libp2p_logging::init_tracing_subscriber_with_default_env_filter(); + libp2p_logging::with_default_env_filter(); let (_, mut a_transport) = create_default_transport::(); let (_, mut b_transport) = create_default_transport::(); @@ -74,7 +74,7 @@ async fn endpoint_reuse() { #[cfg(feature = "async-std")] #[async_std::test] async fn ipv4_dial_ipv6() { - libp2p_logging::init_tracing_subscriber_with_default_env_filter(); + libp2p_logging::with_default_env_filter(); let (a_peer_id, mut a_transport) = create_default_transport::(); let (b_peer_id, mut b_transport) = create_default_transport::(); @@ -94,7 +94,7 @@ async fn ipv4_dial_ipv6() { async fn wrapped_with_delay() { use libp2p_core::transport::DialOpts; - libp2p_logging::init_tracing_subscriber_with_default_env_filter(); + libp2p_logging::with_default_env_filter(); struct DialDelay(Arc>>); @@ -264,7 +264,7 @@ async fn tcp_and_quic() { #[cfg(feature = "async-std")] #[test] fn concurrent_connections_and_streams_async_std() { - libp2p_logging::init_tracing_subscriber_with_default_env_filter(); + libp2p_logging::with_default_env_filter(); quickcheck::QuickCheck::new() .min_tests_passed(1) @@ -275,7 +275,7 @@ fn concurrent_connections_and_streams_async_std() { #[cfg(feature = "tokio")] #[test] fn concurrent_connections_and_streams_tokio() { - libp2p_logging::init_tracing_subscriber_with_default_env_filter(); + libp2p_logging::with_default_env_filter(); let rt = tokio::runtime::Runtime::new().unwrap(); let _guard = rt.enter(); @@ -292,7 +292,7 @@ async fn draft_29_support() { use futures::{future::poll_fn, select}; use libp2p_core::transport::TransportError; - libp2p_logging::init_tracing_subscriber_with_default_env_filter(); + libp2p_logging::with_default_env_filter(); let (_, mut a_transport) = create_transport::(|cfg| cfg.support_draft_29 = true); @@ -367,7 +367,7 @@ async fn draft_29_support() { #[cfg(feature = "async-std")] #[async_std::test] async fn backpressure() { - libp2p_logging::init_tracing_subscriber_with_default_env_filter(); + libp2p_logging::with_default_env_filter(); let max_stream_data = quic::Config::new(&generate_tls_keypair()).max_stream_data; let (mut stream_a, mut stream_b) = build_streams::().await; @@ -391,7 +391,7 @@ async fn backpressure() { #[cfg(feature = "async-std")] #[async_std::test] async fn read_after_peer_dropped_stream() { - libp2p_logging::init_tracing_subscriber_with_default_env_filter(); + libp2p_logging::with_default_env_filter(); let (mut stream_a, mut stream_b) = build_streams::().await; let data = vec![0; 10]; @@ -411,7 +411,7 @@ async fn read_after_peer_dropped_stream() { #[async_std::test] #[should_panic] async fn write_after_peer_dropped_stream() { - libp2p_logging::init_tracing_subscriber_with_default_env_filter(); + libp2p_logging::with_default_env_filter(); let (stream_a, mut stream_b) = build_streams::().await; drop(stream_a); futures_timer::Delay::new(Duration::from_millis(100)).await; @@ -465,7 +465,7 @@ async fn test_local_listener_reuse() { } async fn smoke() { - libp2p_logging::init_tracing_subscriber_with_default_env_filter(); + libp2p_logging::with_default_env_filter(); let (a_peer_id, mut a_transport) = create_default_transport::

(); let (b_peer_id, mut b_transport) = create_default_transport::

(); diff --git a/transports/tcp/src/lib.rs b/transports/tcp/src/lib.rs index 44b8c304f00..a4860710631 100644 --- a/transports/tcp/src/lib.rs +++ b/transports/tcp/src/lib.rs @@ -762,7 +762,7 @@ mod tests { #[test] fn communicating_between_dialer_and_listener() { - libp2p_logging::init_tracing_subscriber_with_default_env_filter(); + libp2p_logging::with_default_env_filter(); async fn listener(addr: Multiaddr, mut ready_tx: mpsc::Sender) { let mut tcp = Transport::::default().boxed(); @@ -841,7 +841,7 @@ mod tests { #[test] fn wildcard_expansion() { - libp2p_logging::init_tracing_subscriber_with_default_env_filter(); + libp2p_logging::with_default_env_filter(); async fn listener(addr: Multiaddr, mut ready_tx: mpsc::Sender) { let mut tcp = Transport::::default().boxed(); @@ -919,7 +919,7 @@ mod tests { #[test] fn port_reuse_dialing() { - libp2p_logging::init_tracing_subscriber_with_default_env_filter(); + libp2p_logging::with_default_env_filter(); async fn listener( addr: Multiaddr, @@ -1036,7 +1036,7 @@ mod tests { #[test] fn port_reuse_listening() { - libp2p_logging::init_tracing_subscriber_with_default_env_filter(); + libp2p_logging::with_default_env_filter(); async fn listen_twice(addr: Multiaddr) { let mut tcp = Transport::::new(Config::new()); @@ -1090,7 +1090,7 @@ mod tests { #[test] fn listen_port_0() { - libp2p_logging::init_tracing_subscriber_with_default_env_filter(); + libp2p_logging::with_default_env_filter(); async fn listen(addr: Multiaddr) -> Multiaddr { let mut tcp = Transport::::default().boxed(); @@ -1125,7 +1125,7 @@ mod tests { #[test] fn listen_invalid_addr() { - libp2p_logging::init_tracing_subscriber_with_default_env_filter(); + libp2p_logging::with_default_env_filter(); fn test(addr: Multiaddr) { #[cfg(feature = "async-io")] @@ -1146,7 +1146,7 @@ mod tests { #[test] fn test_remove_listener() { - libp2p_logging::init_tracing_subscriber_with_default_env_filter(); + libp2p_logging::with_default_env_filter(); async fn cycle_listeners() -> bool { let mut tcp = Transport::::default().boxed(); diff --git a/transports/webrtc/tests/smoke.rs b/transports/webrtc/tests/smoke.rs index 74ca496fafc..5d81b84156b 100644 --- a/transports/webrtc/tests/smoke.rs +++ b/transports/webrtc/tests/smoke.rs @@ -45,7 +45,7 @@ use rand::{thread_rng, RngCore}; #[tokio::test] async fn smoke() { - libp2p_logging::init_tracing_subscriber_with_default_env_filter(); + libp2p_logging::with_default_env_filter(); let (a_peer_id, mut a_transport) = create_transport(); let (b_peer_id, mut b_transport) = create_transport(); @@ -62,7 +62,7 @@ async fn smoke() { // Note: This test should likely be ported to the muxer compliance test suite. #[test] fn concurrent_connections_and_streams_tokio() { - libp2p_logging::init_tracing_subscriber_with_default_env_filter(); + libp2p_logging::with_default_env_filter(); let rt = tokio::runtime::Runtime::new().unwrap(); let _guard = rt.enter(); From 9e2291dc8dd58563bb1438502c5ea10b46e8b9e7 Mon Sep 17 00:00:00 2001 From: kamuik16 Date: Sat, 14 Dec 2024 13:03:25 +0530 Subject: [PATCH 6/9] clippy --- Cargo.lock | 1 + protocols/perf/Cargo.toml | 1 + 2 files changed, 2 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index 3a688bd5094..88240d3f590 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3126,6 +3126,7 @@ dependencies = [ "libp2p-core", "libp2p-dns", "libp2p-identity", + "libp2p-logging", "libp2p-quic", "libp2p-swarm", "libp2p-swarm-test", diff --git a/protocols/perf/Cargo.toml b/protocols/perf/Cargo.toml index cd499a8c949..0e6f5e28e0e 100644 --- a/protocols/perf/Cargo.toml +++ b/protocols/perf/Cargo.toml @@ -31,6 +31,7 @@ serde_json = "1.0" thiserror = { workspace = true } tracing = { workspace = true } tracing-subscriber = { workspace = true, features = ["env-filter"] } +libp2p-logging = { workspace = true } tokio = { workspace = true, features = ["macros", "rt", "rt-multi-thread"] } [dev-dependencies] From 649821f8e9c08cb2e35bf850d8ea30189b0ece13 Mon Sep 17 00:00:00 2001 From: kamuik16 Date: Sat, 14 Dec 2024 13:08:30 +0530 Subject: [PATCH 7/9] clippy --- protocols/identify/tests/smoke.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/protocols/identify/tests/smoke.rs b/protocols/identify/tests/smoke.rs index 8ebc9fdb459..41ca1cf307f 100644 --- a/protocols/identify/tests/smoke.rs +++ b/protocols/identify/tests/smoke.rs @@ -429,9 +429,7 @@ async fn configured_interval_starts_after_first_identify() { #[async_std::test] async fn reject_mismatched_public_key() { - let _ = tracing_subscriber::fmt() - .with_env_filter(EnvFilter::from_default_env()) - .try_init(); + libp2p_logging::with_default_env_filter(); let mut honest_swarm = Swarm::new_ephemeral(|identity| { identify::Behaviour::new( From 3d42e1f99a6a70c18b9dde6dd44cdf791c961995 Mon Sep 17 00:00:00 2001 From: kamuik16 Date: Sat, 14 Dec 2024 15:55:55 +0530 Subject: [PATCH 8/9] chore: use EnvFilter from libp2p-logging --- Cargo.lock | 2 -- misc/multistream-select/Cargo.toml | 1 - misc/multistream-select/src/dialer_select.rs | 2 +- protocols/stream/Cargo.toml | 1 - protocols/stream/tests/lib.rs | 2 +- 5 files changed, 2 insertions(+), 6 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 88240d3f590..f72e1e367c1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3348,7 +3348,6 @@ dependencies = [ "rand 0.8.5", "tokio", "tracing", - "tracing-subscriber", ] [[package]] @@ -3926,7 +3925,6 @@ dependencies = [ "rw-stream-sink", "smallvec", "tracing", - "tracing-subscriber", "unsigned-varint 0.8.0", ] diff --git a/misc/multistream-select/Cargo.toml b/misc/multistream-select/Cargo.toml index ca3b4e47e03..fe284ed4749 100644 --- a/misc/multistream-select/Cargo.toml +++ b/misc/multistream-select/Cargo.toml @@ -24,7 +24,6 @@ futures_ringbuf = "0.4.0" quickcheck = { workspace = true } rand = "0.8" rw-stream-sink = { workspace = true } -tracing-subscriber = { workspace = true, features = ["env-filter"] } libp2p-logging = { workspace = true } # Passing arguments to the docsrs builder in order to properly document cfg's. diff --git a/misc/multistream-select/src/dialer_select.rs b/misc/multistream-select/src/dialer_select.rs index c95bd90c069..7fb5e684f59 100644 --- a/misc/multistream-select/src/dialer_select.rs +++ b/misc/multistream-select/src/dialer_select.rs @@ -214,9 +214,9 @@ mod tests { future::timeout, net::{TcpListener, TcpStream}, }; + use libp2p_logging::EnvFilter; use quickcheck::{Arbitrary, Gen, GenRange}; use tracing::metadata::LevelFilter; - use tracing_subscriber::EnvFilter; use super::*; use crate::listener_select_proto; diff --git a/protocols/stream/Cargo.toml b/protocols/stream/Cargo.toml index 173089948b4..c095c84b529 100644 --- a/protocols/stream/Cargo.toml +++ b/protocols/stream/Cargo.toml @@ -20,7 +20,6 @@ rand = "0.8" [dev-dependencies] libp2p-swarm-test = { workspace = true } tokio = { workspace = true, features = ["full"] } -tracing-subscriber = { workspace = true, features = ["env-filter"] } libp2p-logging = { workspace = true } [lints] diff --git a/protocols/stream/tests/lib.rs b/protocols/stream/tests/lib.rs index 0a0fa7ffd66..7e488a76988 100644 --- a/protocols/stream/tests/lib.rs +++ b/protocols/stream/tests/lib.rs @@ -2,12 +2,12 @@ use std::io; use futures::{AsyncReadExt as _, AsyncWriteExt as _, StreamExt as _}; use libp2p_identity::PeerId; +use libp2p_logging::EnvFilter; use libp2p_stream as stream; use libp2p_swarm::{StreamProtocol, Swarm}; use libp2p_swarm_test::SwarmExt as _; use stream::OpenStreamError; use tracing::level_filters::LevelFilter; -use tracing_subscriber::EnvFilter; const PROTOCOL: StreamProtocol = StreamProtocol::new("/test"); From 05f66ad73bf6a40bd54e40164ddc5ee3cd1a0067 Mon Sep 17 00:00:00 2001 From: kamuik16 Date: Sat, 14 Dec 2024 16:00:05 +0530 Subject: [PATCH 9/9] chore: add description --- misc/logging/src/lib.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/misc/logging/src/lib.rs b/misc/logging/src/lib.rs index 96f068dde29..1155c79b614 100644 --- a/misc/logging/src/lib.rs +++ b/misc/logging/src/lib.rs @@ -1,9 +1,12 @@ pub use tracing_subscriber::EnvFilter; +/// Initializes logging with the default environment filter (`RUST_LOG`). pub fn with_default_env_filter() { with_env_filter(EnvFilter::from_default_env()); } +/// Initializes logging with a custom environment filter. +/// Logs are written to standard error (`stderr`). pub fn with_env_filter(filter: impl Into) { let _ = tracing_subscriber::fmt() .with_env_filter(filter)