Skip to content

Commit edf4e4f

Browse files
committed
Proper set up of logging to work correctly with TUI also for logging level DEBUG.
1 parent e8bf3c8 commit edf4e4f

5 files changed

Lines changed: 25 additions & 15 deletions

File tree

plain2code.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
TuiLoggingHandler,
4444
dump_crash_logs,
4545
get_log_file_path,
46+
LOGGER_NAME,
4647
)
4748
from plain2code_state import RunState
4849
from plain2code_utils import print_dry_run_output
@@ -115,6 +116,7 @@ def setup_logging(
115116
):
116117
# Set default level to INFO for everything not explicitly configured
117118
logging.getLogger().setLevel(logging.INFO)
119+
logging.getLogger(LOGGER_NAME).setLevel(logging.INFO)
118120
logging.getLogger("git").setLevel(logging.WARNING)
119121
logging.getLogger("repositories").setLevel(logging.WARNING)
120122
logging.getLogger("transitions").setLevel(logging.ERROR)
@@ -136,9 +138,9 @@ def setup_logging(
136138
# We add the TuiLoggingHandler to the root logger.
137139
# CRITICAL: We must remove existing handlers (like StreamHandler) to prevent double-logging
138140
# that spills into the TUI dashboard.
139-
root_logger = logging.getLogger()
140-
for h in root_logger.handlers[:]:
141-
root_logger.removeHandler(h)
141+
root_logger = logging.getLogger(LOGGER_NAME)
142+
configured_log_level = root_logger.level
143+
root_logger.setLevel(logging.DEBUG) # Capture all logs; handlers will filter levels as needed
142144

143145
formatter = IndentedFormatter("%(levelname)s:%(name)s:%(message)s")
144146

@@ -151,6 +153,7 @@ def setup_logging(
151153
try:
152154
file_handler = logging.FileHandler(log_file_path, mode="w")
153155
file_handler.setFormatter(formatter)
156+
file_handler.setLevel(configured_log_level)
154157
root_logger.addHandler(file_handler)
155158
except Exception as e:
156159
console.warning(f"Failed to setup file logging to {log_file_path}: {str(e)}")
@@ -159,6 +162,7 @@ def setup_logging(
159162
# in case we need to dump them on crash.
160163
crash_handler = CrashLogHandler()
161164
crash_handler.setFormatter(formatter)
165+
crash_handler.setLevel(configured_log_level)
162166
root_logger.addHandler(crash_handler)
163167

164168
root_logger.info(f"Render ID: {render_id}") # Ensure render ID is logged in to codeplain.log file

plain2code_console.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,12 @@
44
from rich.style import Style
55
from rich.tree import Tree
66

7+
import plain2code_logger
8+
79
CHARACTERS_TO_TOKENS_RULE_OF_THUMB_RATIO = 4
810

11+
logger = logging.getLogger(plain2code_logger.LOGGER_NAME)
12+
913

1014
class Plain2CodeConsole(Console):
1115
INFO_STYLE = Style()
@@ -22,35 +26,35 @@ def __init__(self):
2226

2327
self.llm_encoding = tiktoken.get_encoding("cl100k_base")
2428
except Exception as e:
25-
logging.warning(
29+
logger.warning(
2630
"Failed to import optional library tiktoken. Using approximate instead of exact token count."
2731
)
28-
logging.debug(f"Exception: {e}")
32+
logger.debug(f"Exception: {e}")
2933
self.llm_encoding = None
3034

3135
def info(self, *args, **kwargs):
32-
logging.info(" ".join(map(str, args)))
36+
logger.info(" ".join(map(str, args)))
3337
super().print(*args, **kwargs, style=self.INFO_STYLE)
3438

3539
def warning(self, *args, **kwargs):
36-
logging.warning(" ".join(map(str, args)))
40+
logger.warning(" ".join(map(str, args)))
3741
super().print(*args, **kwargs, style=self.WARNING_STYLE)
3842

3943
def error(self, *args, **kwargs):
40-
logging.error(" ".join(map(str, args)))
44+
logger.error(" ".join(map(str, args)))
4145
super().print(*args, **kwargs, style=self.ERROR_STYLE)
4246

4347
def input(self, *args, **kwargs):
4448
# We also log input as info so it shows in the toggled view
45-
logging.info(" ".join(map(str, args)))
49+
logger.info(" ".join(map(str, args)))
4650
super().print(*args, **kwargs, style=self.INPUT_STYLE)
4751

4852
def output(self, *args, **kwargs):
49-
logging.info(" ".join(map(str, args)))
53+
logger.info(" ".join(map(str, args)))
5054
super().print(*args, **kwargs, style=self.OUTPUT_STYLE)
5155

5256
def debug(self, *args, **kwargs):
53-
logging.debug(" ".join(map(str, args)))
57+
logger.debug(" ".join(map(str, args)))
5458
super().print(*args, **kwargs, style=self.DEBUG_STYLE)
5559

5660
def print_list(self, items, style=None):
@@ -116,7 +120,7 @@ def _count_tokens(self, text):
116120

117121
def print_resources(self, resources_list, linked_resources):
118122
if len(resources_list) == 0:
119-
self.input("Linked resources: None")
123+
self.debug("Linked resources: None")
120124
return
121125

122126
self.input("Linked resources:")

plain2code_events.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class RenderFailed(BaseEvent):
4040

4141
@dataclass
4242
class LogMessageEmitted(BaseEvent):
43-
logger_name: str # e.g., "services.langsmith.langsmith_service", "root"
43+
logger_name: str # e.g., "codeplain"
4444
level: str # e.g., "INFO", "DEBUG", "ERROR"
4545
message: str # The actual log message
4646
timestamp: str # Formatted timestamp

plain2code_logger.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
from event_bus import EventBus
77
from plain2code_events import LogMessageEmitted
88

9+
LOGGER_NAME = "codeplain"
10+
911

1012
class RetryOnlyFilter(logging.Filter):
1113
def filter(self, record):
@@ -105,7 +107,7 @@ def dump_crash_logs(args, formatter=None):
105107
if formatter is None:
106108
formatter = IndentedFormatter("%(levelname)s:%(name)s:%(message)s")
107109

108-
root_logger = logging.getLogger()
110+
root_logger = logging.getLogger(LOGGER_NAME)
109111
crash_handler = next((h for h in root_logger.handlers if isinstance(h, CrashLogHandler)), None)
110112

111113
if crash_handler and args.filename:

tui/components.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -488,7 +488,7 @@ class StructuredLogView(VerticalScroll):
488488

489489
def __init__(self, **kwargs):
490490
super().__init__(**kwargs)
491-
self.min_level = "DEBUG" # Show all by default
491+
self.min_level = "INFO" # By default, show INFO and above
492492

493493
def _should_show_log(self, level: str) -> bool:
494494
"""Check if log should be shown based on minimum level."""

0 commit comments

Comments
 (0)