Просмотр исходного кода

Omit the system filepath north of the installation root

Jeremy Stretch 2 дней назад
Родитель
Сommit
c40640af81
2 измененных файлов с 14 добавлено и 3 удалено
  1. 9 1
      netbox/netbox/jobs.py
  2. 5 2
      netbox/netbox/tests/test_jobs.py

+ 9 - 1
netbox/netbox/jobs.py

@@ -1,7 +1,9 @@
 import logging
 import logging
+import os
 import traceback
 import traceback
 from abc import ABC, abstractmethod
 from abc import ABC, abstractmethod
 from datetime import timedelta
 from datetime import timedelta
+from pathlib import Path
 
 
 from django.core.exceptions import ImproperlyConfigured
 from django.core.exceptions import ImproperlyConfigured
 from django.utils import timezone
 from django.utils import timezone
@@ -22,6 +24,11 @@ __all__ = (
     'system_job',
     'system_job',
 )
 )
 
 
+# The installation root, e.g. "/opt/netbox/". Used to strip absolute path
+# prefixes from traceback file paths before recording them in the job log.
+# jobs.py lives at <root>/netbox/netbox/jobs.py, so parents[2] is the root.
+_INSTALL_ROOT = str(Path(__file__).resolve().parents[2]) + os.sep
+
 
 
 def system_job(interval):
 def system_job(interval):
     """
     """
@@ -108,10 +115,11 @@ class JobRunner(ABC):
             job.terminate(status=JobStatusChoices.STATUS_FAILED)
             job.terminate(status=JobStatusChoices.STATUS_FAILED)
 
 
         except Exception as e:
         except Exception as e:
+            tb_str = traceback.format_exc().replace(_INSTALL_ROOT, '')
             tb_record = logging.makeLogRecord({
             tb_record = logging.makeLogRecord({
                 'levelno': logging.ERROR,
                 'levelno': logging.ERROR,
                 'levelname': 'ERROR',
                 'levelname': 'ERROR',
-                'msg': traceback.format_exc(),
+                'msg': tb_str,
             })
             })
             job.log(tb_record)
             job.log(tb_record)
             job.terminate(status=JobStatusChoices.STATUS_ERRORED, error=repr(e))
             job.terminate(status=JobStatusChoices.STATUS_ERRORED, error=repr(e))

+ 5 - 2
netbox/netbox/tests/test_jobs.py

@@ -10,6 +10,7 @@ from core.models import DataSource, Job
 from utilities.testing import disable_warnings
 from utilities.testing import disable_warnings
 
 
 from ..jobs import *
 from ..jobs import *
+from ..jobs import _INSTALL_ROOT
 
 
 
 
 class TestJobRunner(JobRunner):
 class TestJobRunner(JobRunner):
@@ -85,8 +86,10 @@ class JobRunnerTest(JobRunnerTestCase):
         self.assertEqual(job.error, repr(ErroredJobRunner.EXP))
         self.assertEqual(job.error, repr(ErroredJobRunner.EXP))
         self.assertEqual(len(job.log_entries), 1)
         self.assertEqual(len(job.log_entries), 1)
         self.assertEqual(job.log_entries[0]['level'], 'error')
         self.assertEqual(job.log_entries[0]['level'], 'error')
-        self.assertIn('Traceback', job.log_entries[0]['message'])
-        self.assertIn('Test error', job.log_entries[0]['message'])
+        tb_message = job.log_entries[0]['message']
+        self.assertIn('Traceback', tb_message)
+        self.assertIn('Test error', tb_message)
+        self.assertNotIn(_INSTALL_ROOT, tb_message)
 
 
 
 
 class EnqueueTest(JobRunnerTestCase):
 class EnqueueTest(JobRunnerTestCase):