[Zope-CVS] CVS: Packages/SFTPGateway/src/sftpgateway/tests - test_application.py:1.1

Fred L. Drake, Jr. fred at zope.com
Wed Jun 23 23:57:08 EDT 2004


Update of /cvs-repository/Packages/SFTPGateway/src/sftpgateway/tests
In directory cvs.zope.org:/tmp/cvs-serv26845/src/sftpgateway/tests

Added Files:
	test_application.py 
Log Message:
Avoid leaking a logging.Logger object for each connection.

Loggers created by logging.getLogger() are kept alive in a global
table within the logging pakcage.  To avoid this, use a wrapper object
around a single logger that adds the annotations this had been abusing
the logger's "name" attribute for.

This causes a difference in the actual log; the specific difference is
determined by the message format configured for the log handlers.


=== Added File Packages/SFTPGateway/src/sftpgateway/tests/test_application.py ===
##############################################################################
#
# Copyright (c) 2004 Zope Corporation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.0 (ZPL).  A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################

"""Unit tests for sftpgateway.application."""

import logging
import unittest

from sftpgateway import application


class ConnectionLoggerTestCase(unittest.TestCase):

    def setUp(self):
        self.root_logger = logging.getLogger()
        self.old_level = self.root_logger.level
        self.root_logger.setLevel(logging.DEBUG)
        self.handler = CollectingHandler()
        self.root_logger.addHandler(self.handler)

    def tearDown(self):
        self.root_logger.removeHandler(self.handler)
        self.root_logger.setLevel(self.old_level)

    def make_logger(self):
        return application.ConnectionLogger(self.root_logger, "[prefix] ")

    def test_prefix_with_percent(self):
        self.assertRaises(ValueError, application.ConnectionLogger,
                          self.root_logger, "foo % bar")
        self.assertRaises(ValueError, application.ConnectionLogger,
                          self.root_logger, "%")
        self.assertRaises(ValueError, application.ConnectionLogger,
                          self.root_logger, "%(name)s")
        self.assertEqual(len(self.handler.records), 0)

    def test_debug_prefix_added_to_message(self):
        self.make_logger().debug("foobar")
        self.check_prefix_added_to_message(logging.DEBUG)

    def test_info_prefix_added_to_message(self):
        self.make_logger().info("foobar")
        self.check_prefix_added_to_message(logging.INFO)

    def test_warn_prefix_added_to_message(self):
        self.make_logger().warn("foobar")
        self.check_prefix_added_to_message(logging.WARNING)

    def test_warning_prefix_added_to_message(self):
        self.make_logger().warning("foobar")
        self.check_prefix_added_to_message(logging.WARNING)

    def test_error_prefix_added_to_message(self):
        self.make_logger().error("foobar")
        self.check_prefix_added_to_message(logging.ERROR)

    def test_exception_prefix_added_to_message(self):
        logger = self.make_logger()
        try:
            raise RuntimeError("just a test")
        except RuntimeError:
            logger.exception("foobar")
        self.check_prefix_added_to_message(logging.ERROR)

    def test_critical_prefix_added_to_message(self):
        self.make_logger().critical("foobar")
        self.check_prefix_added_to_message(logging.CRITICAL)

    def test_fatal_prefix_added_to_message(self):
        self.make_logger().fatal("foobar")
        self.check_prefix_added_to_message(logging.CRITICAL)

    def test_log_prefix_added_to_message(self):
        self.make_logger().log(12, "foobar")
        self.check_prefix_added_to_message(12)

    def check_prefix_added_to_message(self, level):
        self.assertEqual(len(self.handler.records), 1)
        rec = self.handler.records[0]
        self.assert_(rec.getMessage().startswith("[prefix] "))
        self.assertEqual(rec.levelno, level)


class CollectingHandler(logging.Handler):

    def __init__(self):
        self.records = []
        logging.Handler.__init__(self)

    def emit(self, record):
        self.records.append(record)


def test_suite():
    return unittest.makeSuite(ConnectionLoggerTestCase)

if __name__ == "__main__":
    unittest.main(defaultTest="test_suite")



More information about the Zope-CVS mailing list