Commit e3804008 authored by Jérome Perrin's avatar Jérome Perrin

ERP5Type: fix logger "name" when logging warnings

Instead of using an always-different logger name which confuses log
analytics system, use the warning category as logger name (or what zLOG.LOG
calls "subsystem")
parent 21c271cd
...@@ -220,8 +220,7 @@ warnings.simplefilter("default") ...@@ -220,8 +220,7 @@ warnings.simplefilter("default")
def _showwarning(message, category, filename, lineno, file=None, line=None): def _showwarning(message, category, filename, lineno, file=None, line=None):
if file is None: if file is None:
LOG("%s:%u %s: %s" % (filename, lineno, category.__name__, message), LOG(category.__name__, WARNING, "%s:%u %s" % (filename, lineno, message))
WARNING, '')
else: else:
file.write(warnings.formatwarning(message, category, filename, lineno, line)) file.write(warnings.formatwarning(message, category, filename, lineno, line))
warnings.showwarning = _showwarning warnings.showwarning = _showwarning
......
...@@ -32,6 +32,7 @@ of Portal Type as Classes and ZODB Components ...@@ -32,6 +32,7 @@ of Portal Type as Classes and ZODB Components
""" """
import unittest import unittest
import warnings
from Products.ERP5Type.tests.ERP5TypeTestCase import ERP5TypeTestCase from Products.ERP5Type.tests.ERP5TypeTestCase import ERP5TypeTestCase
from AccessControl.ZopeGuards import guarded_import from AccessControl.ZopeGuards import guarded_import
from Products.ERP5Type.tests.utils import LogInterceptor from Products.ERP5Type.tests.utils import LogInterceptor
...@@ -243,6 +244,17 @@ class TestERP5Type(ERP5TypeTestCase, LogInterceptor): ...@@ -243,6 +244,17 @@ class TestERP5Type(ERP5TypeTestCase, LogInterceptor):
self.assertTrue(o.isTempObject()) self.assertTrue(o.isTempObject())
self.assertTrue(guarded_import("Products.ERP5Type.Document", fromlist=["newTempBase"])) self.assertTrue(guarded_import("Products.ERP5Type.Document", fromlist=["newTempBase"]))
def test_warnings_redirected_to_event_log(self):
self._catch_log_errors()
self.addCleanup(self._ignore_log_errors)
warnings.warn('user warning')
self.assertEqual(self.logged[-1].name, 'UserWarning')
self.assertIn('Products/ERP5Type/tests/testERP5Type.py', self.logged[-1].message)
self.assertIn('user warning', self.logged[-1].message)
warnings.warn('user warning', DeprecationWarning)
self.assertEqual(self.logged[-1].name, 'DeprecationWarning')
def test_suite(): def test_suite():
suite = unittest.TestSuite() suite = unittest.TestSuite()
suite.addTest(unittest.makeSuite(TestERP5Type)) suite.addTest(unittest.makeSuite(TestERP5Type))
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment