Skip to content

Commit 7505fd9

Browse files
Nightshift ScoutNightshift Scout
authored andcommitted
[nightshift] iris: hoist logging.py imports and simplify callers
Move ``from iris.rpc import logging_pb2`` and the ``_STR_TO_ENUM`` mapping to module scope so each ``str_to_log_level`` call no longer re-executes the import and rebuilds the dict. The deferred import had no corresponding circular dependency (``iris.rpc.logging_pb2`` does not import ``iris.logging``) and every file that imports ``str_to_log_level`` already imports ``logging_pb2`` directly. Widen the parameter type to ``str | None`` and fold the falsy-check into the function itself, then drop the redundant ``if level_name else 0`` guards at four call sites. Update the stale CALLABLE_RUNNER comment that pointed at ``iris.logging`` for ``LevelPrefixFormatter``/``_LEVEL_PREFIX`` — those moved to ``rigging.log_setup``.
1 parent 09234be commit 7505fd9

6 files changed

Lines changed: 22 additions & 23 deletions

File tree

lib/iris/src/iris/cluster/log_store/duckdb_store.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -525,7 +525,7 @@ def get_logs(
525525
regular expression and matched with DuckDB's ``regexp_matches()``.
526526
Otherwise it is treated as an exact key lookup.
527527
"""
528-
min_level_enum = str_to_log_level(min_level) if min_level else 0
528+
min_level_enum = str_to_log_level(min_level)
529529
is_pattern = bool(REGEX_META_RE.search(key))
530530

531531
if not is_pattern:

lib/iris/src/iris/cluster/log_store/mem_store.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ def get_logs(
103103
tail: bool = False,
104104
min_level: str = "",
105105
) -> LogReadResult:
106-
min_level_enum = str_to_log_level(min_level) if min_level else 0
106+
min_level_enum = str_to_log_level(min_level)
107107
is_pattern = bool(REGEX_META_RE.search(key))
108108

109109
if is_pattern:

lib/iris/src/iris/cluster/providers/k8s/tasks.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -525,7 +525,7 @@ def _build_pod_manifest(
525525

526526
def _kubectl_log_line_to_log_entry(kll: KubectlLogLine, attempt_id: int) -> logging_pb2.LogEntry:
527527
level_name = parse_log_level(kll.data)
528-
level = str_to_log_level(level_name) if level_name else 0
528+
level = str_to_log_level(level_name)
529529
entry = logging_pb2.LogEntry(source=kll.stream, data=kll.data, attempt_id=attempt_id, level=level)
530530
entry.timestamp.CopyFrom(timestamp_to_proto(Timestamp.from_seconds(kll.timestamp.timestamp())))
531531
return entry

lib/iris/src/iris/cluster/types.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -481,9 +481,9 @@ def to_proto(self) -> job_pb2.ResourceSpecProto:
481481
482482
# Reinitialize logging with the unified Iris format.
483483
# Uses single-letter level prefix: I=INFO, W=WARNING, E=ERROR, D=DEBUG, C=CRITICAL.
484-
# NOTE: This duplicates LevelPrefixFormatter and _LEVEL_PREFIX from iris.logging
484+
# NOTE: This duplicates LevelPrefixFormatter and _LEVEL_PREFIX from rigging.log_setup
485485
# because CALLABLE_RUNNER executes inside an isolated task container that may not
486-
# have the iris package installed (e.g. user-provided Docker images).
486+
# have the rigging package installed (e.g. user-provided Docker images).
487487
_LEVEL_PREFIX = {"DEBUG": "D", "INFO": "I", "WARNING": "W", "ERROR": "E", "CRITICAL": "C"}
488488
489489
class _LevelPrefixFormatter(logging.Formatter):

lib/iris/src/iris/cluster/worker/task_attempt.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -898,7 +898,7 @@ def _monitor_loop(
898898
def _make_log_entry(self, *, source: str, data: str) -> logging_pb2.LogEntry:
899899
"""Build a LogEntry proto from a source/data pair, parsing the level prefix."""
900900
level_name = parse_log_level(data)
901-
level = str_to_log_level(level_name) if level_name else 0
901+
level = str_to_log_level(level_name)
902902
entry = logging_pb2.LogEntry(source=source, data=data, level=level)
903903
entry.timestamp.epoch_ms = Timestamp.now().epoch_ms()
904904
return entry

lib/iris/src/iris/logging.py

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,23 @@
88
retains only the functions that depend on ``iris.rpc.logging_pb2``.
99
"""
1010

11+
from iris.rpc import logging_pb2
1112

12-
def str_to_log_level(level_name: str) -> int:
13+
_STR_TO_ENUM = {
14+
"DEBUG": logging_pb2.LOG_LEVEL_DEBUG,
15+
"INFO": logging_pb2.LOG_LEVEL_INFO,
16+
"WARNING": logging_pb2.LOG_LEVEL_WARNING,
17+
"ERROR": logging_pb2.LOG_LEVEL_ERROR,
18+
"CRITICAL": logging_pb2.LOG_LEVEL_CRITICAL,
19+
}
20+
21+
22+
def str_to_log_level(level_name: str | None) -> int:
1323
"""Convert a canonical level name (e.g. "INFO") to the LogLevel proto enum value.
1424
15-
Returns LOG_LEVEL_UNKNOWN (0) for unrecognized names.
16-
Uses lazy import to avoid pulling in protobuf at module load time.
25+
Returns ``LOG_LEVEL_UNKNOWN`` (0) for ``None``, empty strings, and
26+
unrecognized names.
1727
"""
18-
from iris.rpc import logging_pb2
19-
20-
_STR_TO_ENUM = {
21-
"DEBUG": logging_pb2.LOG_LEVEL_DEBUG,
22-
"INFO": logging_pb2.LOG_LEVEL_INFO,
23-
"WARNING": logging_pb2.LOG_LEVEL_WARNING,
24-
"ERROR": logging_pb2.LOG_LEVEL_ERROR,
25-
"CRITICAL": logging_pb2.LOG_LEVEL_CRITICAL,
26-
}
27-
return (
28-
_STR_TO_ENUM.get(level_name.upper(), logging_pb2.LOG_LEVEL_UNKNOWN)
29-
if level_name
30-
else logging_pb2.LOG_LEVEL_UNKNOWN
31-
)
28+
if not level_name:
29+
return logging_pb2.LOG_LEVEL_UNKNOWN
30+
return _STR_TO_ENUM.get(level_name.upper(), logging_pb2.LOG_LEVEL_UNKNOWN)

0 commit comments

Comments
 (0)