Commits

Boris FELD committed 4b7ac51

Automatically set CloseRecord levelno attribute based on status loglevel if not given.

  • Participants
  • Parent commits 00f3dd4

Comments (0)

Files changed (2)

File goatlog/records.py

     __context_action__ = 'close'
 
     def __init__(self, context_id, status, name=None, loglevel=None):
+        if loglevel is None:
+            loglevel = status.loglevel
+
         ContextRecord.__init__(self, context_id, name, loglevel)
         self.status = status
 

File tests/test_log.py

         get = json.loads(self.json_dump.getvalue())
         self.assertEqual(get['contents'][0]['status'], status.human)
 
+    def test_context_loglevel(self):
+        with LogCapture() as captured_logs:
+            status = STATUSES.WARNING
+
+            with self.log.context('simple', 'id') as ctx:
+                ctx.close(status=status)
+
+        log_close = captured_logs.records[1]
+        self.assertEqual(log_close.levelno, status.loglevel)
+
     def test_get_current_status(self):
         status = STATUSES.WARNING
 
         get = json.loads(self.json_dump.getvalue())
         self.assertEqual(get['contents'][0]['status'], status.human)
 
+    def test_set_log_record_loglevel(self):
+        self.log.registry.add_record_class(SimpleLogRecord)
+        with LogCapture() as captured_logs:
+            status = STATUSES.WARNING
+
+            with self.log.context('simple', 'id'):
+                self.log.log_record('simple', status=status)
+
+        log_close = captured_logs.records[2]
+        self.assertEqual(log_close.levelno, status.loglevel)
+
 if __name__ == "__main__":
     unittest.main()