Skip to content

Commit

Permalink
moving the log_theme() to log_utils
Browse files Browse the repository at this point in the history
  • Loading branch information
brifordwylie committed Jan 21, 2025
1 parent 49e6749 commit 7d433c6
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 41 deletions.
6 changes: 3 additions & 3 deletions src/workbench/repl/workbench_shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@

# Workbench Imports
from workbench.utils.repl_utils import cprint, Spinner
from workbench.utils.workbench_logging import IMPORTANT_LEVEL_NUM, TRACE_LEVEL_NUM, set_log_theme
from workbench.utils.workbench_logging import IMPORTANT_LEVEL_NUM, TRACE_LEVEL_NUM
from workbench.utils.config_manager import ConfigManager
from workbench.utils.log_utils import silence_logs
from workbench.utils.log_utils import silence_logs, log_theme

# If we have RDKIT/Mordred let's pull in our cheminformatics utils
try:
Expand Down Expand Up @@ -160,7 +160,7 @@ def __init__(self):
self.commands["version"] = lambda: print(version)
self.commands["cached_meta"] = self.switch_to_cached_meta
self.commands["direct_meta"] = self.switch_to_direct_meta
self.commands["log_theme"] = set_log_theme
self.commands["log_theme"] = log_theme

# Add cheminformatics utils if available
if HAVE_CHEM_UTILS:
Expand Down
60 changes: 57 additions & 3 deletions src/workbench/utils/log_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import os
import logging
from contextlib import contextmanager
from workbench.utils.workbench_logging import ColoredFormatter


@contextmanager
Expand All @@ -24,11 +25,36 @@ def silence_logs():
logger.setLevel(original_level)


def log_theme(theme: str):
"""Update the logging theme dynamically."""
# Validate the theme
if theme.lower() not in ["light", "dark"]:
raise ValueError("Theme must be 'light' or 'dark'")

# Set the global theme in ColoredFormatter
ColoredFormatter.set_theme(theme)
log = logging.getLogger("workbench")

# Replace the formatter for all handlers
for handler in log.handlers:
formatter = handler.formatter
if formatter and formatter.__class__.__name__ == "ColoredFormatter":
# Create a new formatter with the updated theme
new_formatter = ColoredFormatter(
"%(asctime)s (%(filename)s:%(lineno)d) %(levelname)s %(message)s",
datefmt="%Y-%m-%d %H:%M:%S",
)
handler.setFormatter(new_formatter)


if __name__ == "__main__":
# Test the log utils functions
from workbench.utils.workbench_logging import logging_setup

logging_setup()

log = logging.getLogger("workbench")
log.setLevel(logging.INFO)
log.setLevel(logging.DEBUG)
log.info("You should see me")

with silence_logs():
Expand All @@ -38,6 +64,34 @@ def silence_logs():
log.info("You should see me")
log.warning("You should see this warning")

with silence_logs():
raise ValueError("This is a test error")
try:
with silence_logs():
raise ValueError("This is a test error")
except ValueError:
pass
log.info("You should see me")

# Test the log theme
log_theme("light")
print("\n\n\n")
log.info("Switched to light theme")
log.debug("This should be a muted color")
log.trace("Trace color should stand out from debug")
log.info("This should be a nice color")
log.important("Important color should stand out from info")
log.warning("This should be a color that attracts attention")
log.monitor("This is a monitor message")
log.error("This should be a bright color")
log.critical("This should be an alert color")

log_theme("dark")
print("\n\n\n")
log.info("Switched to light theme")
log.debug("This should be a muted color")
log.trace("Trace color should stand out from debug")
log.info("This should be a nice color")
log.important("Important color should stand out from info")
log.warning("This should be a color that attracts attention")
log.monitor("This is a monitor message")
log.error("This should be a bright color")
log.critical("This should be an alert color")
35 changes: 0 additions & 35 deletions src/workbench/utils/workbench_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,28 +241,6 @@ def exception_log_forward(call_on_exception=None):
time.sleep(2) # Give the logs a chance to flush


def set_log_theme(theme: str):
"""Update the logging theme dynamically."""
# Validate the theme
if theme.lower() not in ["light", "dark"]:
raise ValueError("Theme must be 'light' or 'dark'")

# Set the global theme in ColoredFormatter
ColoredFormatter.set_theme(theme)
log = logging.getLogger("workbench")

# Replace the formatter for all handlers
for handler in log.handlers:
formatter = handler.formatter
if formatter and formatter.__class__.__name__ == "ColoredFormatter":
# Create a new formatter with the updated theme
new_formatter = ColoredFormatter(
"%(asctime)s (%(filename)s:%(lineno)d) %(levelname)s %(message)s",
datefmt="%Y-%m-%d %H:%M:%S",
)
handler.setFormatter(new_formatter)


if __name__ == "__main__":
# Uncomment to test the WORKBENCH_DEBUG env variable
# os.environ["WORKBENCH_DEBUG"] = "True"
Expand All @@ -286,19 +264,6 @@ def set_log_theme(theme: str):
my_log.error("This should be a bright color")
my_log.critical("This should be an alert color")

# Test the log theme
set_log_theme("light")
print("\n\n\n")
my_log.info("Switched to light theme")
my_log.debug("This should be a muted color")
my_log.trace("Trace color should stand out from debug")
my_log.info("This should be a nice color")
my_log.important("Important color should stand out from info")
my_log.warning("This should be a color that attracts attention")
my_log.monitor("This is a monitor message")
my_log.error("This should be a bright color")
my_log.critical("This should be an alert color")

# Test the exception handler
with exception_log_forward():
raise ValueError("Testing the exception handler")

0 comments on commit 7d433c6

Please sign in to comment.