Skip to content

Commit

Permalink
Set logger name and catch it properly.
Browse files Browse the repository at this point in the history
  • Loading branch information
terjekv authored and Terje Kvernes committed Dec 5, 2023
1 parent 0e09dbb commit 01113db
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
10 changes: 5 additions & 5 deletions mreg/api/v1/history.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

from mreg.models.base import History

log = structlog.get_logger(__name__)
log = structlog.get_logger("mreg.history")


class DjangoJSONModelEncoder(DjangoJSONEncoder):
Expand Down Expand Up @@ -59,8 +59,8 @@ def save_log(self, action, serializer, data, orig_data=None):

try:
history.full_clean()
except ValidationError as e:
log.exception("ValidationError", e=e, exc_info=True)
except ValidationError:
log.exception("ValidationError occured during full_clean()", exc_info=True)
return
history.save()

Expand Down Expand Up @@ -101,8 +101,8 @@ def save_log_m2m_alteration(self, method, instance):

try:
history.full_clean()
except ValidationError as e:
log.exception("ValidationError", e=e, exc_info=True)
except ValidationError:
log.exception("ValidationError occured during full_clean()", exc_info=True)
return
history.save()

Expand Down
6 changes: 3 additions & 3 deletions mreg/api/v1/tests/test_history.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ def test_save_log_handles_validation_error(self, mock_get_jsondata, mock_manipul
# Mocking a method for save_log_m2m_alteration method
method = mock.Mock(__name__='TestMethod')

with self.assertLogs('dl_logger', level='ERROR') as cm:
with self.assertLogs('mreg.history', level='ERROR') as cm:
log.save_log('test_action', mock_serializer, data)
log.save_log_m2m_alteration(method, instance)

self.assertEqual(mock_full_clean.call_count, 2)

# Assert that the error was logged twice, with tracebacks.
self.assertTrue(cm.output[0].startswith('ERROR:dl_logger:Traceback'))
self.assertTrue(cm.output[1].startswith('ERROR:dl_logger:Traceback'))
self.assertTrue(cm.output[0].startswith('ERROR:mreg.history:'))
self.assertTrue(cm.output[1].startswith('ERROR:mreg.history:'))

0 comments on commit 01113db

Please sign in to comment.