Skip to content

Commit

Permalink
fix: replace init_logging with try_init_log_from_env
Browse files Browse the repository at this point in the history
`init_logging` code is kept unused waiting for
eclipse-zenoh/zenoh#1165
  • Loading branch information
wyfo committed Jun 18, 2024
1 parent af303e1 commit 4b67c73
Show file tree
Hide file tree
Showing 18 changed files with 27 additions and 42 deletions.
2 changes: 1 addition & 1 deletion examples/z_delete.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion examples/z_get.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion examples/z_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion examples/z_ping.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion examples/z_pong.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion examples/z_pub.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion examples/z_pub_thr.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion examples/z_pull.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion examples/z_put.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion examples/z_queryable.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion examples/z_scout.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion examples/z_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion examples/z_sub.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion examples/z_sub_queued.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion examples/z_sub_thr.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
9 changes: 7 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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},
Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion tests/test_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
28 changes: 4 additions & 24 deletions zenoh/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand Down

0 comments on commit 4b67c73

Please sign in to comment.