Skip to content

Commit 33f1261

Browse files
authored
feat: provide a log level option for log functions (#279)
1 parent ea9a27c commit 33f1261

File tree

2 files changed

+30
-16
lines changed

2 files changed

+30
-16
lines changed

solnlib/log.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,12 +220,16 @@ def set_level(self, level: int, name: str = None):
220220
logging.getLogger().setLevel(level)
221221

222222

223-
def log_event(logger: logging.Logger, key_values: Dict[str, Any]):
223+
def log_event(
224+
logger: logging.Logger, key_values: Dict[str, Any], log_level: int = logging.INFO
225+
):
226+
"""General function to log any event in key-value format."""
224227
message = " ".join([f"{k}={v}" for k, v in key_values.items()])
225-
logger.info(message)
228+
logger.log(log_level, message)
226229

227230

228231
def modular_input_start(logger: logging.Logger, modular_input_name: str):
232+
"""Specific function to log the start of the modular input."""
229233
log_event(
230234
logger,
231235
{
@@ -236,6 +240,7 @@ def modular_input_start(logger: logging.Logger, modular_input_name: str):
236240

237241

238242
def modular_input_end(logger: logging.Logger, modular_input_name: str):
243+
"""Specific function to log the end of the modular input."""
239244
log_event(
240245
logger,
241246
{
@@ -248,6 +253,7 @@ def modular_input_end(logger: logging.Logger, modular_input_name: str):
248253
def events_ingested(
249254
logger: logging.Logger, modular_input_name: str, sourcetype: str, n_events: int
250255
):
256+
"""Specific function to log the number of events ingested."""
251257
log_event(
252258
logger,
253259
{

tests/unit/test_log.py

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,21 @@ def test_log_event():
148148
},
149149
)
150150

151-
assert mock_logger.info.call_count == 1
152-
assert "key=foo value=bar" in mock_logger.info.call_args[0]
151+
mock_logger.log.assert_called_once_with(logging.INFO, "key=foo value=bar")
152+
153+
154+
def test_log_event_when_debug_log_level():
155+
with mock.patch("logging.Logger") as mock_logger:
156+
log.log_event(
157+
mock_logger,
158+
{
159+
"key": "foo",
160+
"value": "bar",
161+
},
162+
log_level=logging.DEBUG,
163+
)
164+
165+
mock_logger.log.assert_called_once_with(logging.DEBUG, "key=foo value=bar")
153166

154167

155168
def test_modular_input_start():
@@ -159,10 +172,8 @@ def test_modular_input_start():
159172
"modular_input_name",
160173
)
161174

162-
assert mock_logger.info.call_count == 1
163-
assert (
164-
"action=started modular_input_name=modular_input_name"
165-
in mock_logger.info.call_args[0]
175+
mock_logger.log.assert_called_once_with(
176+
logging.INFO, "action=started modular_input_name=modular_input_name"
166177
)
167178

168179

@@ -173,19 +184,16 @@ def test_modular_input_end():
173184
"modular_input_name",
174185
)
175186

176-
assert mock_logger.info.call_count == 1
177-
assert (
178-
"action=ended modular_input_name=modular_input_name"
179-
in mock_logger.info.call_args[0]
187+
mock_logger.log.assert_called_once_with(
188+
logging.INFO, "action=ended modular_input_name=modular_input_name"
180189
)
181190

182191

183192
def test_events_ingested():
184193
with mock.patch("logging.Logger") as mock_logger:
185194
log.events_ingested(mock_logger, "modular_input_name", "sourcetype", 5)
186195

187-
assert mock_logger.info.call_count == 1
188-
assert (
189-
"action=events_ingested modular_input_name=modular_input_name sourcetype=sourcetype n_events=5"
190-
in mock_logger.info.call_args[0]
196+
mock_logger.log.assert_called_once_with(
197+
logging.INFO,
198+
"action=events_ingested modular_input_name=modular_input_name sourcetype=sourcetype n_events=5",
191199
)

0 commit comments

Comments
 (0)