Skip to content

Commit

Permalink
[BugFix] Fix logging disabling bug and add tests (#1218)
Browse files Browse the repository at this point in the history
SUMMARY:
Fixed logging and clear loggers enabling/disabling bug. Previously, any
value on the right environment variables would disable logging. Now, we
explicitly check for `true`

TEST PLAN:
Added unit tests for enabling logging.
`make test` passes

---------

Signed-off-by: Aman Gupta <[email protected]>
Co-authored-by: Dipika Sikka <[email protected]>
  • Loading branch information
aman2304 and dsikka authored Mar 5, 2025
1 parent 8fc6012 commit 14ac2e7
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/llmcompressor/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ class LoggerConfig:
metrics_disabled: bool = False


def configure_logger(config: Optional[LoggerConfig] = None):
def configure_logger(config: Optional[LoggerConfig] = None) -> None:
"""
Configure the metrics for LLM Compressor.
Configure the logger for LLM Compressor.
This function sets up the console and file logging
as per the specified or default parameters.
Expand All @@ -68,9 +68,9 @@ def configure_logger(config: Optional[LoggerConfig] = None):

# env vars get priority
if (disabled := os.getenv("LLM_COMPRESSOR_LOG_DISABLED")) is not None:
logger_config.disabled = disabled.lower()
logger_config.disabled = disabled.lower() == "true"
if (clear_loggers := os.getenv("LLM_COMPRESSOR_CLEAR_LOGGERS")) is not None:
logger_config.clear_loggers = clear_loggers.lower()
logger_config.clear_loggers = clear_loggers.lower() == "true"
if (console_log_level := os.getenv("LLM_COMPRESSOR_LOG_LEVEL")) is not None:
logger_config.console_log_level = console_log_level.upper()
if (log_file := os.getenv("LLM_COMPRESSOR_LOG_FILE")) is not None:
Expand Down
13 changes: 13 additions & 0 deletions tests/unit/test_logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,16 @@ def test_environment_variable_disable_logging(monkeypatch, capsys):
captured = capsys.readouterr()
assert captured.out == ""
assert captured.err == ""


def test_environment_variable_enable_logging(monkeypatch, capsys):
# Test environment variable to enable logging
monkeypatch.setenv("LLM_COMPRESSOR_LOG_DISABLED", "false")

configure_logger(config=LoggerConfig())
logger.info("Info message")
logger.error("Error message")

captured = capsys.readouterr()
assert captured.out.count("Info message") == 1
assert captured.out.count("Error message") == 1

0 comments on commit 14ac2e7

Please sign in to comment.