Replace LogInterceptor with one that behaves more like the one on CMF 1.5

git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@31382 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent 0b2c7dd2
......@@ -40,7 +40,7 @@ from Products.ERP5Type.tests.Sequence import SequenceList
from urllib import pathname2url
from Products.ERP5Type.Globals import PersistentMapping
from Products.CMFCore.Expression import Expression
from Products.CMFCore.tests.base.testcase import LogInterceptor
from Products.ERP5Type.tests.utils import LogInterceptor
import shutil
import os
......
......@@ -35,7 +35,7 @@ from AccessControl import getSecurityManager
from AccessControl.SecurityManagement import newSecurityManager
from zLOG import LOG
from DateTime import DateTime
from Products.CMFCore.tests.base.testcase import LogInterceptor
from Products.ERP5Type.tests.utils import LogInterceptor
from Testing.ZopeTestCase.PortalTestCase import PortalTestCase
from Products.ERP5Type.tests.utils import createZODBPythonScript, \
getExtraSqlConnectionStringList
......
......@@ -36,7 +36,7 @@ from AccessControl import getSecurityManager
from AccessControl.SecurityManagement import newSecurityManager
from zLOG import LOG
from DateTime import DateTime
from Products.CMFCore.tests.base.testcase import LogInterceptor
from Products.ERP5Type.tests.utils import LogInterceptor
from Products.ERP5Type.tests.utils import createZODBPythonScript, todo_erp5, \
getExtraSqlConnectionStringList
from Products.ZSQLCatalog.ZSQLCatalog import HOT_REINDEXING_FINISHED_STATE,\
......
......@@ -39,7 +39,7 @@ from Products.ERP5Type.tests.ERP5TypeTestCase import ERP5TypeTestCase
from Products.ERP5Type.tests.utils import DummyLocalizer
from zLOG import LOG, INFO
from Products.CMFCore.Expression import Expression
from Products.CMFCore.tests.base.testcase import LogInterceptor
from Products.ERP5Type.tests.utils import LogInterceptor
from Products.CMFCore.WorkflowCore import WorkflowException
from Products.ERP5Type.Accessor.Constant import PropertyGetter as ConstantGetter
from Products.ERP5Type.Base import _aq_reset
......
......@@ -30,7 +30,7 @@ import unittest
from Products.ERP5Type.tests.ERP5TypeTestCase import ERP5TypeTestCase
from zLOG import LOG
from Products.CMFCore.tests.base.testcase import LogInterceptor
from Products.ERP5Type.tests.utils import LogInterceptor
from Products.ERP5Type.tests.utils import createZODBPythonScript
from Products.ERP5Type.ERP5Type import ERP5TypeInformation
from Products.ERP5Type.Cache import clearCache
......
......@@ -33,7 +33,7 @@ import transaction
from Products.ERP5Type.tests.ERP5TypeTestCase import ERP5TypeTestCase
from zLOG import LOG
from Products.CMFCore.tests.base.testcase import LogInterceptor
from Products.ERP5Type.tests.utils import LogInterceptor
from Products.ERP5Type.Cache import clearCache
class TestFolderMigration(ERP5TypeTestCase, LogInterceptor):
......
......@@ -34,7 +34,7 @@ import transaction
from DateTime import DateTime
from Products.ERP5Type.tests.ERP5TypeTestCase import ERP5TypeTestCase
from zLOG import LOG
from Products.CMFCore.tests.base.testcase import LogInterceptor
from Products.ERP5Type.tests.utils import LogInterceptor
import os, hotshot
# Define variable to chek if performance are good or not
......
......@@ -31,7 +31,7 @@ import unittest
from Testing import ZopeTestCase
from Products.ERP5Type.tests.ERP5TypeTestCase import ERP5TypeTestCase
from zLOG import LOG
from Products.CMFCore.tests.base.testcase import LogInterceptor
from Products.ERP5Type.tests.utils import LogInterceptor
from Products.ERP5Type.TransactionalVariable import getTransactionalVariable
try:
......
......@@ -31,7 +31,7 @@ import unittest
from Testing import ZopeTestCase
from Products.ERP5Type.tests.ERP5TypeTestCase import ERP5TypeTestCase
from Products.CMFCore.tests.base.testcase import LogInterceptor
from Products.ERP5Type.tests.utils import LogInterceptor
from Products.ERP5Type.Utils import cartesianProduct
from AccessControl.SecurityManagement import newSecurityManager
from zLOG import PROBLEM
......
......@@ -30,8 +30,10 @@
"""
import os
import logging
import transaction
import zLOG
import Products.ERP5Type
from Products.MailHost.MailHost import MailHost
from email import message_from_string
......@@ -270,3 +272,68 @@ class reindex(object):
# Test cases using this decorator must extend backportUnittest.TestCase
todo_erp5 = backportUnittest.skip("TODO ERP5")
class LogInterceptor:
'''Replacement for Products.CMFCore.tests.base.testcase.LogInterceptor
On CMF 1, LogInterceptor would bail if a log record with too high
severity would pass through, and it would monkey-patch zLOG.log_write to do
its job, meaning it would take on all Zope messages.
The CMF 2 LogInterceptor plugs itself as a filter on the requested logger
(the root logger, by default), which meant it would only be called on
log records at that exact subsystem (not lower subsystems), and it no
longer raises AssertionError on messages with high severity.
This replacement restore the original semantics while keeping close to the
new implementation, so it can act on both "zLOG" and "logging" calls.
'''
logged = None
installed = ()
level = 0
def _zLOGLSeverityToLoggingLevel(self, severity):
'''Given a zLOG severity, return a logging level
'''
# inspired by zLOG.EventLogger.log_write
from zLOG.EventLogger import zlog_to_pep282_severity_cache_get
from zLOG.EventLogger import zlog_to_pep282_severity
level = (zlog_to_pep282_severity_cache_get(severity) or
zlog_to_pep282_severity(severity))
return level
def _catch_log_errors(self, ignored_level=zLOG.WARNING, subsystem=''):
if subsystem in self.installed:
raise ValueError, 'Already installed filter!'
root_logger = logging.getLogger(subsystem)
self.installed += (subsystem,)
self.level = self._zLOGLSeverityToLoggingLevel(ignored_level)
# attach to a handler instead of a logger, since logger filters are
# not always called. See http://bugs.python.org/issue7535
for handler in root_logger.handlers:
handler.addFilter(self)
break
else:
raise ValueError('No handlers to attach in logging subsystem %r' %
subsystem or 'root')
def filter(self, record):
if record.levelno > self.level:
raise AssertionError("%s(%s): %s" %
(record.name,
record.levelname,
record.getMessage()))
if self.logged is None:
self.logged = []
self.logged.append(record)
return False
def _ignore_log_errors(self, subsystem=''):
if subsystem not in self.installed:
return
root_logger = logging.getLogger(subsystem)
for handler in root_logger.handlers:
handler.removeFilter(self)
self.installed = tuple([s for s in self.installed if s != subsystem])
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