Put python's logs into Trial when running unit tests (#3319)

This commit is contained in:
Amber Brown 2018-06-04 16:06:06 +10:00 committed by GitHub
parent 86accac5d5
commit 5dbf305444
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -12,23 +12,37 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
import twisted
from twisted.trial import unittest
import logging import logging
# logging doesn't have a "don't log anything at all EVARRRR setting, import twisted
# but since the highest value is 50, 1000000 should do ;) import twisted.logger
NEVER = 1000000 from twisted.trial import unittest
handler = logging.StreamHandler() from synapse.util.logcontext import LoggingContextFilter
handler.setFormatter(logging.Formatter(
"%(levelname)s:%(name)s:%(message)s [%(pathname)s:%(lineno)d]" # Set up putting Synapse's logs into Trial's.
)) rootLogger = logging.getLogger()
logging.getLogger().addHandler(handler)
logging.getLogger().setLevel(NEVER) log_format = (
logging.getLogger("synapse.storage.SQL").setLevel(NEVER) "%(asctime)s - %(name)s - %(lineno)d - %(levelname)s - %(request)s - %(message)s"
logging.getLogger("synapse.storage.txn").setLevel(NEVER) )
class ToTwistedHandler(logging.Handler):
tx_log = twisted.logger.Logger()
def emit(self, record):
log_entry = self.format(record)
log_level = record.levelname.lower().replace('warning', 'warn')
self.tx_log.emit(twisted.logger.LogLevel.levelWithName(log_level), log_entry)
handler = ToTwistedHandler()
formatter = logging.Formatter(log_format)
handler.setFormatter(formatter)
handler.addFilter(LoggingContextFilter(request=""))
rootLogger.addHandler(handler)
def around(target): def around(target):
@ -61,7 +75,7 @@ class TestCase(unittest.TestCase):
method = getattr(self, methodName) method = getattr(self, methodName)
level = getattr(method, "loglevel", getattr(self, "loglevel", NEVER)) level = getattr(method, "loglevel", getattr(self, "loglevel", logging.ERROR))
@around(self) @around(self)
def setUp(orig): def setUp(orig):