Skip to content

Commit e11ed39

Browse files
committed
feat: add nocolor logging config, simplify logging init, remove circular import
Add log.nocolor config field and move logging init logic into LogConfig.init_logging() so the config object encapsulates its own setup. This simplifies _init_logging() in __main__.py from 6 lines to 2. Break the circular dependency between log_utils and config by making print_horizontal_line accept explicit show/width params instead of importing Config internally. Remove horizontal_line_width config field in favor of a fixed 80-column width.
1 parent 06ee80a commit e11ed39

4 files changed

Lines changed: 34 additions & 46 deletions

File tree

esbmc_ai/__main__.py

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
# Author: Yiannis Charalambous 2023
44

55
import json
6-
import logging
76
import sys
87
from time import perf_counter
98

@@ -19,7 +18,7 @@
1918
from esbmc_ai.addon_loader import AddonLoader
2019
from esbmc_ai.base_component import BaseComponent
2120
from esbmc_ai.command_result import CommandResult
22-
from esbmc_ai.log_utils import LogCategories, get_log_level, init_logging
21+
from esbmc_ai.log_utils import LogCategories
2322
from esbmc_ai.verifiers import BaseSourceVerifier, ESBMC, CommandOracle
2423
from esbmc_ai.component_manager import ComponentManager
2524
import esbmc_ai.commands
@@ -140,16 +139,8 @@ def _init_builtin_components() -> None:
140139

141140

142141
def _init_logging() -> None:
143-
# Add logging handlers with config options
144142
config = Config()
145-
logging_handlers: list[logging.Handler] = config.log.logging_handlers
146-
147-
# Reinit logging
148-
init_logging(
149-
level=get_log_level(config.verbose_level),
150-
file_handlers=logging_handlers,
151-
init_basic=config.log.basic,
152-
)
143+
config.log.init_logging(config.verbose_level)
153144

154145

155146
def main() -> None:

esbmc_ai/commands/help_config.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,10 @@ def execute(self) -> CommandResult | None:
147147

148148
from esbmc_ai.log_utils import print_horizontal_line
149149

150-
print_horizontal_line()
150+
config = Config()
151+
hl_kwargs = dict(show=config.show_horizontal_lines)
152+
153+
print_horizontal_line(**hl_kwargs)
151154

152155
# Get component manager to access builtin and addon components
153156
component_manager = ComponentManager()
@@ -158,7 +161,7 @@ def execute(self) -> CommandResult | None:
158161
dict(component_manager.builtin_components),
159162
)
160163

161-
print_horizontal_line()
164+
print_horizontal_line(**hl_kwargs)
162165

163166
# Print addon component config fields
164167
self._print_components_section(

esbmc_ai/config.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,21 @@ class LogConfig(BaseModel):
114114
"noisy libs.",
115115
)
116116

117+
nocolor: bool = Field(
118+
default=False,
119+
description="Disable color output in the logger.",
120+
)
121+
122+
def init_logging(self, verbose_level: int) -> None:
123+
from esbmc_ai.log_utils import get_log_level, init_logging
124+
125+
init_logging(
126+
level=get_log_level(verbose_level),
127+
file_handlers=self.logging_handlers,
128+
init_basic=self.basic,
129+
nocolor=self.nocolor,
130+
)
131+
117132
@property
118133
def logging_handlers(self) -> list[logging.Handler]:
119134
logging_handlers: list[logging.Handler] = []
@@ -445,13 +460,6 @@ def on_set_addon_modules(cls, mods: list[str]) -> list[str]:
445460
"Makes it easier to read.",
446461
)
447462

448-
horizontal_line_width: int | None = Field(
449-
default=None,
450-
validation_alias="horizontal_line_width",
451-
description="Sets the width of the horizontal lines to draw. "
452-
"Don't set a value to use the terminal width. Needs to have "
453-
"show_horizontal_lines set to true.",
454-
)
455463

456464
ai_custom: dict[str, AICustomModelConfig] = Field(
457465
default_factory=defaultdict,

esbmc_ai/log_utils.py

Lines changed: 12 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
"""Horizontal line logging integrated with Structlog."""
44

55
from enum import Enum
6-
from os import get_terminal_size
76
import logging
87
import structlog
98
from structlog.typing import EventDict
@@ -36,6 +35,7 @@ def _init_logging_basic(
3635
*,
3736
level: int,
3837
logging_format: str = _logging_format,
38+
nocolor: bool = False,
3939
) -> None:
4040
"""Initializes the logging system in basic mode, good for debugging since
4141
it can easily change the logging_format."""
@@ -46,7 +46,7 @@ def _init_logging_basic(
4646
structlog.processors.add_log_level,
4747
_add_category_field,
4848
_render_prefix_category_to_event,
49-
structlog.dev.ConsoleRenderer(),
49+
structlog.dev.ConsoleRenderer(colors=not nocolor),
5050
],
5151
wrapper_class=structlog.stdlib.BoundLogger,
5252
logger_factory=structlog.stdlib.LoggerFactory(),
@@ -66,6 +66,7 @@ def init_logging(
6666
level: int,
6767
file_handlers: list[logging.Handler] = [],
6868
init_basic: bool = False,
69+
nocolor: bool = False,
6970
) -> None:
7071
"""Initializes the logging system.
7172
@@ -95,7 +96,7 @@ def init_logging(
9596

9697
# Use the basic unformatted logger instead.
9798
if init_basic:
98-
_init_logging_basic(level=level)
99+
_init_logging_basic(level=level, nocolor=nocolor)
99100
return
100101

101102
structlog.configure(
@@ -116,7 +117,7 @@ def init_logging(
116117
structlog.stdlib.ProcessorFormatter(
117118
processors=[
118119
_filter_keys_processor,
119-
structlog.dev.ConsoleRenderer(),
120+
structlog.dev.ConsoleRenderer(colors=not nocolor),
120121
]
121122
)
122123
)
@@ -154,17 +155,17 @@ def print_horizontal_line(
154155
*,
155156
char: str = "=",
156157
category: Enum | str = LogCategories.ALL,
157-
width: int | None = None,
158+
show: bool = True,
158159
logger: structlog.stdlib.BoundLogger | None = None,
159160
) -> None:
160161
"""
161162
Print a horizontal line if logging is enabled for the specified level. Both
162-
an int of the level or the verbose name could be surprised.
163-
"""
164-
# Import Config locally to avoid circular import
165-
from esbmc_ai.config import Config
163+
an int of the level or the verbose name could be supplied.
166164
167-
if not Config().show_horizontal_lines:
165+
Args:
166+
show: Whether to actually print. Pass Config().show_horizontal_lines.
167+
"""
168+
if not show:
168169
return
169170

170171
# Convert level name to numeric value (e.g., "info" -> logging.INFO)
@@ -174,26 +175,11 @@ def print_horizontal_line(
174175
else level
175176
)
176177

177-
# Determine line width
178-
line_width: int
179-
if width is not None:
180-
line_width = width
181-
182-
else:
183-
config_hlw: int | None = Config().horizontal_line_width
184-
if config_hlw is not None:
185-
line_width = config_hlw
186-
else:
187-
try:
188-
line_width = get_terminal_size().columns
189-
except OSError:
190-
line_width = 80 - _largest_cat_len
191-
192178
if logger is None:
193179
logger = structlog.get_logger()
194180
assert logger is not None
195181

196-
logger.log(level=level_no, event=char * line_width, category=category)
182+
logger.log(level=level_no, event=char * 80, category=category)
197183

198184

199185
def _render_prefix_category_to_event(

0 commit comments

Comments
 (0)