Skip to content

Commit 0621241

Browse files
authored
Initialize logging after native init (#616)
1 parent ff06588 commit 0621241

File tree

1 file changed

+17
-10
lines changed

1 file changed

+17
-10
lines changed

matter_server/server/stack.py

+17-10
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
CHIP_DETAIL = logging.DEBUG - 1
2929
CHIP_AUTOMATION = logging.DEBUG - 2
3030

31+
_category_num: int = 4
32+
3133

3234
@LogRedirectCallback_t # type: ignore[misc]
3335
def _redirect_to_python_logging(
@@ -58,27 +60,24 @@ def init_logging(category: str) -> None:
5860
"""Initialize Matter SDK logging. Filter by category."""
5961

6062
_LOGGER.info("Initializing CHIP/Matter Logging...")
61-
category_num = ERROR_CATEGORY_NONE
63+
_category_num = ERROR_CATEGORY_NONE
6264
if category == "ERROR":
63-
category_num = ERROR_CATEGORY_ERROR
65+
_category_num = ERROR_CATEGORY_ERROR
6466
elif category == "PROGRESS":
65-
category_num = ERROR_CATEGORY_PROGRESS
67+
_category_num = ERROR_CATEGORY_PROGRESS
6668
elif category == "DETAIL":
67-
category_num = ERROR_CATEGORY_DETAIL
69+
_category_num = ERROR_CATEGORY_DETAIL
6870
elif category == "AUTOMATION":
69-
category_num = 4
71+
_category_num = 4
7072

7173
logging.addLevelName(CHIP_ERROR, "CHIP_ERROR")
7274
logging.addLevelName(CHIP_PROGRESS, "CHIP_PROGRESS")
7375
logging.addLevelName(CHIP_DETAIL, "CHIP_DETAIL")
7476
logging.addLevelName(CHIP_AUTOMATION, "CHIP_AUTOMATION")
7577
logging.getLogger("chip.native").setLevel(CHIP_AUTOMATION)
7678

77-
handle = _GetLoggingLibraryHandle()
78-
handle.pychip_logging_set_callback(_redirect_to_python_logging)
79-
80-
# Handle log level selection on SDK level
81-
chip.logging.SetLogFilter(category_num)
79+
# We can't setup logging here yet as the stack needs to be
80+
# initialized first!
8281

8382

8483
class MatterStack:
@@ -95,6 +94,14 @@ def __init__(
9594
self.logger.debug("Using storage file: %s", storage_file)
9695
chip.native.Init()
9796

97+
# Initialize logging after stack init!
98+
# See: https://github.com/project-chip/connectedhomeip/issues/20233
99+
handle = _GetLoggingLibraryHandle()
100+
handle.pychip_logging_set_callback(_redirect_to_python_logging)
101+
102+
# Handle log level selection on SDK level
103+
chip.logging.SetLogFilter(_category_num)
104+
98105
self._chip_stack = ChipStack(
99106
persistentStoragePath=storage_file,
100107
installDefaultLogHandler=False,

0 commit comments

Comments
 (0)