Skip to content

Commit

Permalink
Show log file path if not in quiet mode. (#186)
Browse files Browse the repository at this point in the history
* Show log file path if not in quiet mode.

- print the log file path with Halo if not in quiet mode.

Closes: #185

* - move progress indicator to the beginning of the function.

* - move progress indicator stop_and_persist to the end of logging
  configuration. This way if fails in the middle the user will see
  something like: Configuring logging...  ✖

* - use COU_DIR_LOG directly as Path
  • Loading branch information
gabrielcocenza authored Dec 7, 2023
1 parent c716836 commit 1d1de17
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
14 changes: 8 additions & 6 deletions cou/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@
"""Set up both global logger for logfile and console'."""
import logging
import logging.handlers
import pathlib
from datetime import datetime

from cou.utils import COU_DATA
from cou.utils import COU_DATA, progress_indicator

COU_DIR_LOG = COU_DATA / "log"

Expand Down Expand Up @@ -46,6 +45,11 @@ def setup_logging(log_level: str = "INFO") -> None:
:param log_level: Logging level, defaults to "INFO"
:type log_level: str, optional
"""
progress_indicator.start("Configuring logging...")
time_stamp = datetime.now().strftime("%Y%m%d%H%M%S")
file_name = f"{COU_DIR_LOG}/cou-{time_stamp}.log"
COU_DIR_LOG.mkdir(parents=True, exist_ok=True)

log_formatter_file = logging.Formatter(
fmt="%(asctime)s [%(name)s] [%(levelname)s] %(message)s", datefmt="%Y-%m-%d %H:%M:%S"
)
Expand All @@ -56,9 +60,6 @@ def setup_logging(log_level: str = "INFO") -> None:
root_logger.setLevel("NOTSET")

# handler for the log file. Log level is "NOTSET"
time_stamp = datetime.now().strftime("%Y%m%d%H%M%S")
file_name = f"{COU_DIR_LOG}/cou-{time_stamp}.log"
pathlib.Path(COU_DIR_LOG).mkdir(parents=True, exist_ok=True)
log_file_handler = logging.FileHandler(file_name)
log_file_handler.setFormatter(log_formatter_file)
# suppress python libjuju and websockets debug logs
Expand All @@ -76,7 +77,8 @@ def setup_logging(log_level: str = "INFO") -> None:

root_logger.addHandler(log_file_handler)
root_logger.addHandler(console_handler)
logger.info("Logs of this execution can be found at %s", file_name)

progress_indicator.stop_and_persist(text=f"Logs of this execution can be found at {file_name}")


def filter_debug_logs(record: logging.LogRecord) -> bool:
Expand Down
7 changes: 6 additions & 1 deletion tests/unit/test_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ def test_filter_clears_exc_info_and_text():
@pytest.mark.parametrize("log_level", ["NOTSET", "DEBUG", "INFO", "WARNING", "ERROR"])
def test_setup_logging(log_level):
"""Test setting up logging."""
with patch("cou.logging.logging") as mock_logging:
with (
patch("cou.logging.logging") as mock_logging,
patch("cou.logging.progress_indicator") as mock_indicator,
):
log_file_handler = MagicMock()
console_handler = MagicMock()
mock_root_logger = mock_logging.getLogger.return_value
Expand All @@ -47,6 +50,8 @@ def test_setup_logging(log_level):

mock_root_logger.addHandler.assert_any_call(log_file_handler)
mock_root_logger.addHandler.assert_any_call(console_handler)
mock_indicator.start.assert_called_once()
mock_indicator.stop_and_persist.assert_called_once()

if log_level == "NOTSET":
log_file_handler.addFilter.assert_not_called()
Expand Down

0 comments on commit 1d1de17

Please sign in to comment.