Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions src/hydrodiy/io/iutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,15 @@ def __init__(self, logger,
dictseparator_charac="+",
dictseparator_length=30,
tab_length=4):
errmess = "Expected a logger object"
assert isinstance(logger, logging.Logger), errmess

if not isinstance(logger, logging.Logger):
errmess = "Expected a logger object"
raise ValueError(errmess)

self._logger = logger

self._time_start = datetime.now()

self.separator_charac = separator_charac
self.separator_length = separator_length

Expand Down Expand Up @@ -165,6 +170,11 @@ def completed(self):
self.info("")
self.info(self.get_separator(self.separator_charac,
self.separator_length))
tdelta = datetime.now() - self._time_start
days = tdelta.days
hours, rem = divmod(tdelta.seconds, 3600)
mins, secs = divmod(rem, 60)
self.info(f"Execution time : {days*24 + hours}h {mins}m {secs}s")
self.info("@@@ Process completed @@@")

def log_dict(self, tolog, name="", level="info"):
Expand Down
18 changes: 10 additions & 8 deletions src/hydrodiy/io/tests/test_hyio_iutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,13 +160,14 @@ def test_get_logger():

with open(flog2, "r") as fl:
txt = fl.readlines()
expected = ["@@@ Process started @@@", \
logger2.separator_charac*logger2.separator_length, \
""]+\
mess+\
["", \
logger2.separator_charac*logger2.separator_length, \
"@@@ Process completed @@@"]
expected = ["@@@ Process started @@@",
logger2.separator_charac*logger2.separator_length,
""] +\
mess +\
["",
logger2.separator_charac*logger2.separator_length,
"Execution time : 0h 0m 0s",
"@@@ Process completed @@@"]
assert expected == [t.strip() for t in txt]

# Close log file handler and delete files
Expand Down Expand Up @@ -211,7 +212,8 @@ def test_get_logger_contextual():
ck &= bool(re.search("CRITICAL . \\{ context2 \\}", txt[10]))
ck &= bool(re.search("WARNING . \\{ context2 \\}", txt[11]))
ck &= bool(re.search("INFO . \\{ context2 \\} test tab3", txt[12]))
ck &= bool(re.search("@@@ Process completed @@@", txt[16]))
ck &= bool(re.search("Execution time", txt[16]))
ck &= bool(re.search("@@@ Process completed @@@", txt[17]))
assert ck

logger.handlers[1].close()
Expand Down