Skip to content

Commit

Permalink
Add option to log handler to prefix every line
Browse files Browse the repository at this point in the history
  • Loading branch information
momchil-flex committed Jan 8, 2025
1 parent a0c3cfe commit c520ccc
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions tidy3d/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from typing import Callable, List, Union

from rich.console import Console
from rich.text import Text
from typing_extensions import Literal

# Note: "SUPPORT" and "USER" levels are meant for backend runs only.
Expand Down Expand Up @@ -68,10 +69,12 @@ def __init__(
console: Console,
level: LogValue,
log_level_format: Callable = _default_log_level_format,
prefix_every_line: bool = False,
):
self.level = _get_level_int(level)
self.console = console
self.log_level_format = log_level_format
self.prefix_every_line = prefix_every_line

def handle(self, level, level_name, message):
"""Output log messages depending on log level"""
Expand All @@ -81,13 +84,19 @@ def handle(self, level, level_name, message):
if stack[offset - 1].filename.endswith("exceptions.py"):
# We want the calling site for exceptions.py
offset += 1
self.console.log(
self.log_level_format(level_name),
message,
sep=": ",
style=DEFAULT_LOG_STYLES[level_name],
_stack_offset=offset,
)
if self.prefix_every_line:
wrapped_text = Text(message, style="default")
msgs = wrapped_text.wrap(console=self.console, width=self.console.width)
else:
msgs = [message]
for msg in msgs:
self.console.log(
self.log_level_format(level_name),
msg,
sep=": ",
style=DEFAULT_LOG_STYLES[level_name],
_stack_offset=offset,
)


class Logger:
Expand Down

0 comments on commit c520ccc

Please sign in to comment.