Skip to content

Commit

Permalink
Use specific logger instead of global one
Browse files Browse the repository at this point in the history
Instead of using global logger, this patch is using dedicate logger
named as `libnmstate` for python logging.

This allows user to control the logging level of nmstate module only.

For example, this python script will disable nmstate logs but still
enable debug logging for others:

```python

import logging
import libnmstate

logging.basicConfig(level=logging.DEBUG)

logger = logging.getLogger("libnmstate")
logger.disabled = True

print(libnmstate.show())

logging.debug("DEBUGGING")
```

Resolves: https://issues.redhat.com/browse/RHEL-72016

Signed-off-by: Gris Ge <[email protected]>
  • Loading branch information
cathay4t committed Jan 14, 2025
1 parent 75c88fa commit c2fdbb9
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions rust/src/python/libnmstate/clib_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,15 +315,16 @@ def parse_log(logs):
log_entries = json.loads(logs.decode("utf-8"))
except Exception:
pass
logger = logging.getLogger("libnmstate")
for log_entry in log_entries:
msg = f"{log_entry['time']}:{log_entry['file']}: {log_entry['msg']}"
level = log_entry["level"]

if level == "ERROR":
logging.error(msg)
logger.error(msg)
elif level == "WARN":
logging.warning(msg)
logger.warning(msg)
elif level == "INFO":
logging.info(msg)
logger.info(msg)
else:
logging.debug(msg)
logger.debug(msg)

0 comments on commit c2fdbb9

Please sign in to comment.