Skip to content

Commit 1b7144d

Browse files
committed
feat: clarified logger messages
1 parent f1672d2 commit 1b7144d

File tree

2 files changed

+36
-10
lines changed

2 files changed

+36
-10
lines changed

hydrodiy/io/iutils.py

+28-3
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@
1818
import numpy as np
1919
import pandas as pd
2020

21+
# Configuration of logging messages
22+
LOGGER_NSEPARATORS = 50
23+
LOGGER_SEPARATOR = "-"
24+
LOGGER_NSEPARATORS_CONTEXTUAL = 3
25+
LOGGER_SEPARATOR_CONTEXTUAL = "#"
26+
2127

2228
def script_template(filename, comment,
2329
type="simple",
@@ -119,25 +125,44 @@ class StartedCompletedLogger(logging.Logger):
119125
""" Add context to logging messages via the context attribute """
120126

121127
def __init__(self, *args, **kwargs):
122-
self.context = ""
123128
super(StartedCompletedLogger, self).__init__(*args, **kwargs)
124129

125130
def started(self):
126131
self.info("@@@ Process started @@@")
132+
self.info(LOGGER_SEPARATOR*LOGGER_NSEPARATORS)
133+
self.info("")
127134

128135
def completed(self):
129-
self.context = ""
130136
self.info("")
137+
self.info(LOGGER_SEPARATOR*LOGGER_NSEPARATORS)
131138
self.info("@@@ Process completed @@@")
132139

133140

134141
class HydrodiyContextualLogger(StartedCompletedLogger):
135142
""" Add context to logging messages via the context attribute """
136143

137144
def __init__(self, *args, **kwargs):
138-
self.context = ""
145+
self._context = ""
139146
super(HydrodiyContextualLogger, self).__init__(*args, **kwargs)
140147

148+
@property
149+
def context(self):
150+
return self._context
151+
152+
@context.setter
153+
def context(self, value):
154+
self._context = str(value)
155+
if self._context != "":
156+
self.info(LOGGER_SEPARATOR*LOGGER_NSEPARATORS)
157+
sep = LOGGER_SEPARATOR_CONTEXTUAL\
158+
*LOGGER_NSEPARATORS_CONTEXTUAL
159+
mess = sep+" "+self._context+" "+sep
160+
self.info(mess)
161+
162+
def completed(self):
163+
self.context = ""
164+
super(HydrodiyContextualLogger, self).completed()
165+
141166
def _log(self, level, msg, args, exc_info=None, extra=None):
142167
if self.context != "":
143168
msg = "{{ {0} }} {1}".format(self.context, msg)

hydrodiy/io/tests/test_hyio_iutils.py

+8-7
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,10 @@ def test_get_logger():
126126

127127
with open(flog1, "r") as fl:
128128
txt = fl.readlines()
129+
129130
ck = txt[0].strip().endswith("INFO | @@@ Process started @@@")
130-
ck = ck & txt[1].strip().endswith("INFO | "+mess[0])
131-
ck = ck & txt[2].strip().endswith("INFO | "+mess[1])
131+
ck = ck & txt[3].strip().endswith("INFO | "+mess[0])
132+
ck = ck & txt[4].strip().endswith("INFO | "+mess[1])
132133
assert ck
133134

134135
# Test logging with different format
@@ -145,8 +146,8 @@ def test_get_logger():
145146

146147
with open(flog2, "r") as fl:
147148
txt = fl.readlines()
148-
expected = ["@@@ Process started @@@"]+mess+\
149-
["", "@@@ Process completed @@@"]
149+
expected = ["@@@ Process started @@@", "-"*50, ""]+mess+\
150+
["", "-"*50, "@@@ Process completed @@@"]
150151
assert expected == [t.strip() for t in txt]
151152

152153
# Close log file handler and delete files
@@ -179,9 +180,9 @@ def test_get_logger_contextual():
179180
txt = fl.readlines()
180181

181182
ck = bool(re.search("@@@ Process started @@@", txt[0]))
182-
ck &= bool(re.search("\\{ context1 \\}", txt[1]))
183-
ck &= bool(re.search("\\{ context2 \\}", txt[2]))
184-
ck &= bool(re.search("@@@ Process completed @@@", txt[4]))
183+
ck &= bool(re.search("\\{ context1 \\}", txt[5]))
184+
ck &= bool(re.search("\\{ context2 \\}", txt[8]))
185+
ck &= bool(re.search("@@@ Process completed @@@", txt[11]))
185186
assert ck
186187

187188
logger.handlers[1].close()

0 commit comments

Comments
 (0)