From 4b67c73ce9039c7f0eb514d1df97e39b4a207f26 Mon Sep 17 00:00:00 2001 From: Joseph Perez Date: Tue, 18 Jun 2024 16:11:41 +0200 Subject: [PATCH] fix: replace `init_logging` with `try_init_log_from_env` `init_logging` code is kept unused waiting for https://github.com/eclipse-zenoh/zenoh/pull/1165 --- examples/z_delete.py | 2 +- examples/z_get.py | 2 +- examples/z_info.py | 2 +- examples/z_ping.py | 2 +- examples/z_pong.py | 2 +- examples/z_pub.py | 2 +- examples/z_pub_thr.py | 2 +- examples/z_pull.py | 2 +- examples/z_put.py | 2 +- examples/z_queryable.py | 2 +- examples/z_scout.py | 2 +- examples/z_storage.py | 2 +- examples/z_sub.py | 2 +- examples/z_sub_queued.py | 2 +- examples/z_sub_thr.py | 2 +- src/lib.rs | 9 +++++++-- tests/test_session.py | 2 +- zenoh/__init__.pyi | 28 ++++------------------------ 18 files changed, 27 insertions(+), 42 deletions(-) diff --git a/examples/z_delete.py b/examples/z_delete.py index 98ba0fd1..8db06c8e 100644 --- a/examples/z_delete.py +++ b/examples/z_delete.py @@ -78,7 +78,7 @@ # Zenoh code --- --- --- --- --- --- --- --- --- --- --- def main(): # initiate logging - zenoh.init_logging() + zenoh.try_init_log_from_env() print("Opening session...") with zenoh.open(conf) as session: diff --git a/examples/z_get.py b/examples/z_get.py index 7ff91d70..a417b594 100644 --- a/examples/z_get.py +++ b/examples/z_get.py @@ -99,7 +99,7 @@ # Zenoh code --- --- --- --- --- --- --- --- --- --- --- def main(): # initiate logging - zenoh.init_logging() + zenoh.try_init_log_from_env() print("Opening session...") with zenoh.open(conf) as session: diff --git a/examples/z_info.py b/examples/z_info.py index 94c090db..dc84eeff 100644 --- a/examples/z_info.py +++ b/examples/z_info.py @@ -69,7 +69,7 @@ # Zenoh code --- --- --- --- --- --- --- --- --- --- --- def main(): # initiate logging - zenoh.init_logging() + zenoh.try_init_log_from_env() print("Opening session...") with zenoh.open(conf) as session: diff --git a/examples/z_ping.py b/examples/z_ping.py index 39000478..00728a33 100644 --- a/examples/z_ping.py +++ b/examples/z_ping.py @@ -94,7 +94,7 @@ # Zenoh code --- --- --- --- --- --- --- --- --- --- --- def main(): # initiate logging - zenoh.init_logging() + zenoh.try_init_log_from_env() print("Opening session...") with zenoh.open(conf) as session: diff --git a/examples/z_pong.py b/examples/z_pong.py index b5d34958..e00e7d3d 100644 --- a/examples/z_pong.py +++ b/examples/z_pong.py @@ -78,7 +78,7 @@ # Zenoh code --- --- --- --- --- --- --- --- --- --- --- def main(): # initiate logging - zenoh.init_logging() + zenoh.try_init_log_from_env() print("Opening session...") with zenoh.open(conf) as session: diff --git a/examples/z_pub.py b/examples/z_pub.py index 51890894..97475d8a 100644 --- a/examples/z_pub.py +++ b/examples/z_pub.py @@ -96,7 +96,7 @@ def main(): # initiate logging - zenoh.init_logging() + zenoh.try_init_log_from_env() print("Opening session...") with zenoh.open(conf) as session: diff --git a/examples/z_pub_thr.py b/examples/z_pub_thr.py index eae4d884..9ee1ac67 100644 --- a/examples/z_pub_thr.py +++ b/examples/z_pub_thr.py @@ -75,7 +75,7 @@ # Zenoh code --- --- --- --- --- --- --- --- --- --- --- def main(): # initiate logging - zenoh.init_logging() + zenoh.try_init_log_from_env() data = bytearray() for i in range(0, size): diff --git a/examples/z_pull.py b/examples/z_pull.py index 14278a6c..7f6f03f1 100644 --- a/examples/z_pull.py +++ b/examples/z_pull.py @@ -90,7 +90,7 @@ def main(): # initiate logging - zenoh.init_logging() + zenoh.try_init_log_from_env() print("Opening session...") with zenoh.open(conf) as session: diff --git a/examples/z_put.py b/examples/z_put.py index c93f264f..87ce72df 100644 --- a/examples/z_put.py +++ b/examples/z_put.py @@ -87,7 +87,7 @@ # Zenoh code --- --- --- --- --- --- --- --- --- --- --- def main(): # initiate logging - zenoh.init_logging() + zenoh.try_init_log_from_env() print("Opening session...") with zenoh.open(conf) as session: diff --git a/examples/z_queryable.py b/examples/z_queryable.py index f9c6617e..bfbd8195 100644 --- a/examples/z_queryable.py +++ b/examples/z_queryable.py @@ -111,7 +111,7 @@ def queryable_callback(query): def main(): # initiate logging - zenoh.init_logging() + zenoh.try_init_log_from_env() print("Opening session...") with zenoh.open(conf) as session: diff --git a/examples/z_scout.py b/examples/z_scout.py index 0cae3118..0cae2fa4 100644 --- a/examples/z_scout.py +++ b/examples/z_scout.py @@ -18,7 +18,7 @@ def main(): # initiate logging - zenoh.init_logging() + zenoh.try_init_log_from_env() print("Scouting...") scout = zenoh.scout(what="peer|router") diff --git a/examples/z_storage.py b/examples/z_storage.py index 3f76311f..3354e7ef 100644 --- a/examples/z_storage.py +++ b/examples/z_storage.py @@ -117,7 +117,7 @@ def query_handler(query: zenoh.Query): def main(): # initiate logging - zenoh.init_logging() + zenoh.try_init_log_from_env() print("Opening session...") with zenoh.open(conf) as session: diff --git a/examples/z_sub.py b/examples/z_sub.py index baf08093..82179da4 100644 --- a/examples/z_sub.py +++ b/examples/z_sub.py @@ -79,7 +79,7 @@ # Zenoh code --- --- --- --- --- --- --- --- --- --- --- def main(): # initiate logging - zenoh.init_logging() + zenoh.try_init_log_from_env() print("Opening session...") with zenoh.open(conf) as session: diff --git a/examples/z_sub_queued.py b/examples/z_sub_queued.py index 897cdd49..6d680d12 100644 --- a/examples/z_sub_queued.py +++ b/examples/z_sub_queued.py @@ -81,7 +81,7 @@ def main(): # initiate logging - zenoh.init_logging() + zenoh.try_init_log_from_env() print("Opening session...") with zenoh.open(conf) as session: diff --git a/examples/z_sub_thr.py b/examples/z_sub_thr.py index dc6be158..8dad03a2 100644 --- a/examples/z_sub_thr.py +++ b/examples/z_sub_thr.py @@ -113,7 +113,7 @@ def report(): def main(): # initiate logging - zenoh.init_logging() + zenoh.try_init_log_from_env() with zenoh.open(conf) as session: session.declare_subscriber( diff --git a/src/lib.rs b/src/lib.rs index 0d001ef9..bd70d520 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -17,7 +17,8 @@ mod encoding; mod handlers; mod info; mod key_expr; -mod logging; +#[allow(unused)] +mod logging; // https://github.com/eclipse-zenoh/zenoh-python/pull/235#discussion_r1644498390 mod macros; mod publisher; mod query; @@ -44,6 +45,11 @@ pub(crate) mod zenoh { use crate::handlers::{Callback, DefaultHandler, FifoChannel, Handler, RingChannel}; } + #[pyfunction] + fn try_init_log_from_env() { + zenoh::try_init_log_from_env(); + } + #[pymodule_export] use crate::{ bytes::{deserializer, serializer, ZBytes}, @@ -53,7 +59,6 @@ pub(crate) mod zenoh { info::SessionInfo, key_expr::KeyExpr, key_expr::SetIntersectionLevel, - logging::init_logging, publisher::{CongestionControl, Priority, Publisher}, query::{ConsolidationMode, Query, QueryTarget, Reply, ReplyError}, queryable::Queryable, diff --git a/tests/test_session.py b/tests/test_session.py index 71c258db..40f9fa56 100644 --- a/tests/test_session.py +++ b/tests/test_session.py @@ -124,7 +124,7 @@ def sub_callback(sample: Sample): def test_session(): - zenoh.init_logging() + zenoh.try_init_log_from_env() (peer01, peer02) = open_session(["tcp/127.0.0.1:17447"]) run_session_qryrep(peer01, peer02) run_session_pubsub(peer01, peer02) diff --git a/zenoh/__init__.pyi b/zenoh/__init__.pyi index be68cab4..a9f8ca77 100644 --- a/zenoh/__init__.pyi +++ b/zenoh/__init__.pyi @@ -948,31 +948,11 @@ class ZenohId: def into_keyexpr(self) -> KeyExpr: ... def __str__(self) -> str: ... -@overload -def init_logging(*, basic_config: bool = True, raw: Literal[False] = False, **kwargs): - """Start redirecting all zenoh logs to Python logger named `zenoh`. - - If `basic_config=True`, which is the default, it also calls - `logging.basicConfig(**kwargs)` as a convenience. - - If the overloaded signature `init_logging(raw=True)` is called, then - zenoh logs are directly printed to stdout, bypassing the Python logger - (and its formatting). Raw log level can be controlled using `RUST_LOG` - environment variable. - """ +def try_init_log_from_env(): + """Redirect zenoh logs to stdout, according to the `RUST_LOG` environment variable. -@overload -def init_logging(*, raw: Literal[True]): - """Start redirecting all zenoh logs to Python logger named `zenoh`. - - If `basic_config=True`, which is the default, it also calls - `logging.basicConfig(**kwargs)` as a convenience. - - If the overloaded signature `init_logging(raw=True)` is called, then - zenoh logs are directly printed to stdout, bypassing the Python logger - (and its formatting). Raw log level can be controlled using `RUST_LOG` - environment variable. - """ + For example, `RUST_LOG=debug` will set the log level to DEBUG. + If `RUST_LOG` is not set, then logging is not enabled.""" def open(config: Config | None = None) -> Session: """Open a zenoh Session."""