Back to index

python3.2  3.2.2
Public Member Functions | Public Attributes | Static Public Attributes
test.test_logging.BuiltinLevelsTest Class Reference
Inheritance diagram for test.test_logging.BuiltinLevelsTest:
Inheritance graph
[legend]
Collaboration diagram for test.test_logging.BuiltinLevelsTest:
Collaboration graph
[legend]

List of all members.

Public Member Functions

def test_flat
def test_nested_explicit
def test_nested_inherited
def test_nested_with_virtual_parent
def setUp
def tearDown
def assert_log_lines
def next_message

Public Attributes

 saved_handlers
 saved_handler_list
 saved_loggers
 saved_level_names
 logger_states
 logger1
 logger2
 root_logger
 original_logging_level
 stream
 root_hdlr
 root_formatter

Static Public Attributes

string log_format = "%(name)s -> %(levelname)s: %(message)s"
string expected_log_pat = r"^([\w.]+) -> ([\w]+): ([\d]+)$"
int message_num = 0

Detailed Description

Test builtin levels and their inheritance.

Definition at line 162 of file test_logging.py.


Member Function Documentation

def test.test_logging.BaseTest.assert_log_lines (   self,
  expected_values,
  stream = None 
) [inherited]
Match the collected log lines against the regular expression
self.expected_log_pat, and compare the extracted group values to
the expected_values list of tuples.

Definition at line 131 of file test_logging.py.

00131 
00132     def assert_log_lines(self, expected_values, stream=None):
00133         """Match the collected log lines against the regular expression
00134         self.expected_log_pat, and compare the extracted group values to
00135         the expected_values list of tuples."""
00136         stream = stream or self.stream
00137         pat = re.compile(self.expected_log_pat)
00138         try:
00139             stream.reset()
00140             actual_lines = stream.readlines()
00141         except AttributeError:
00142             # StringIO.StringIO lacks a reset() method.
00143             actual_lines = stream.getvalue().splitlines()
00144         self.assertEqual(len(actual_lines), len(expected_values),
00145                           '%s vs. %s' % (actual_lines, expected_values))
00146         for actual, expected in zip(actual_lines, expected_values):
00147             match = pat.search(actual)
00148             if not match:
00149                 self.fail("Log line does not match expected pattern:\n" +
00150                             actual)
00151             self.assertEqual(tuple(match.groups()), expected)
00152         s = stream.read()
00153         if s:
00154             self.fail("Remaining output at end of log stream:\n" + s)

Here is the call graph for this function:

Here is the caller graph for this function:

def test.test_logging.BaseTest.next_message (   self) [inherited]
Generate a message consisting solely of an auto-incrementing
integer.

Definition at line 155 of file test_logging.py.

00155 
00156     def next_message(self):
00157         """Generate a message consisting solely of an auto-incrementing
00158         integer."""
00159         self.message_num += 1
00160         return "%d" % self.message_num
00161 

Here is the caller graph for this function:

def test.test_logging.BaseTest.setUp (   self) [inherited]
Setup the default logging stream to an internal StringIO instance,
so that we can examine log output as we want.

Reimplemented in test.test_logging.BaseFileTest, test.test_logging.QueueHandlerTest, test.test_logging.LogRecordFactoryTest, test.test_logging.MemoryTest, test.test_logging.SocketHandlerTest, test.test_logging.MemoryHandlerTest, and test.test_logging.CustomLevelsAndFiltersTest.

Definition at line 63 of file test_logging.py.

00063 
00064     def setUp(self):
00065         """Setup the default logging stream to an internal StringIO instance,
00066         so that we can examine log output as we want."""
00067         logger_dict = logging.getLogger().manager.loggerDict
00068         logging._acquireLock()
00069         try:
00070             self.saved_handlers = logging._handlers.copy()
00071             self.saved_handler_list = logging._handlerList[:]
00072             self.saved_loggers = saved_loggers = logger_dict.copy()
00073             self.saved_level_names = logging._levelNames.copy()
00074             self.logger_states = logger_states = {}
00075             for name in saved_loggers:
00076                 logger_states[name] = getattr(saved_loggers[name],
00077                                               'disabled', None)
00078         finally:
00079             logging._releaseLock()
00080 
00081         # Set two unused loggers: one non-ASCII and one Unicode.
00082         # This is to test correct operation when sorting existing
00083         # loggers in the configuration code. See issue 8201.
00084         self.logger1 = logging.getLogger("\xab\xd7\xbb")
00085         self.logger2 = logging.getLogger("\u013f\u00d6\u0047")
00086 
00087         self.root_logger = logging.getLogger("")
00088         self.original_logging_level = self.root_logger.getEffectiveLevel()
00089 
00090         self.stream = io.StringIO()
00091         self.root_logger.setLevel(logging.DEBUG)
00092         self.root_hdlr = logging.StreamHandler(self.stream)
00093         self.root_formatter = logging.Formatter(self.log_format)
00094         self.root_hdlr.setFormatter(self.root_formatter)
00095         if self.logger1.hasHandlers():
00096             hlist = self.logger1.handlers + self.root_logger.handlers
00097             raise AssertionError('Unexpected handlers: %s' % hlist)
00098         if self.logger2.hasHandlers():
00099             hlist = self.logger2.handlers + self.root_logger.handlers
00100             raise AssertionError('Unexpected handlers: %s' % hlist)
00101         self.root_logger.addHandler(self.root_hdlr)
00102         self.assertTrue(self.logger1.hasHandlers())
00103         self.assertTrue(self.logger2.hasHandlers())

Here is the call graph for this function:

Here is the caller graph for this function:

def test.test_logging.BaseTest.tearDown (   self) [inherited]
Remove our logging stream, and restore the original logging
level.

Reimplemented in test.test_logging.BaseFileTest, test.test_logging.QueueHandlerTest, test.test_logging.LogRecordFactoryTest, test.test_logging.SocketHandlerTest, and test.test_logging.MemoryHandlerTest.

Definition at line 104 of file test_logging.py.

00104 
00105     def tearDown(self):
00106         """Remove our logging stream, and restore the original logging
00107         level."""
00108         self.stream.close()
00109         self.root_logger.removeHandler(self.root_hdlr)
00110         while self.root_logger.handlers:
00111             h = self.root_logger.handlers[0]
00112             self.root_logger.removeHandler(h)
00113             h.close()
00114         self.root_logger.setLevel(self.original_logging_level)
00115         logging._acquireLock()
00116         try:
00117             logging._levelNames.clear()
00118             logging._levelNames.update(self.saved_level_names)
00119             logging._handlers.clear()
00120             logging._handlers.update(self.saved_handlers)
00121             logging._handlerList[:] = self.saved_handler_list
00122             loggerDict = logging.getLogger().manager.loggerDict
00123             loggerDict.clear()
00124             loggerDict.update(self.saved_loggers)
00125             logger_states = self.logger_states
00126             for name in self.logger_states:
00127                 if logger_states[name] is not None:
00128                     self.saved_loggers[name].disabled = logger_states[name]
00129         finally:
00130             logging._releaseLock()

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 165 of file test_logging.py.

00165 
00166     def test_flat(self):
00167         #Logging levels in a flat logger namespace.
00168         m = self.next_message
00169 
00170         ERR = logging.getLogger("ERR")
00171         ERR.setLevel(logging.ERROR)
00172         INF = logging.LoggerAdapter(logging.getLogger("INF"), {})
00173         INF.setLevel(logging.INFO)
00174         DEB = logging.getLogger("DEB")
00175         DEB.setLevel(logging.DEBUG)
00176 
00177         # These should log.
00178         ERR.log(logging.CRITICAL, m())
00179         ERR.error(m())
00180 
00181         INF.log(logging.CRITICAL, m())
00182         INF.error(m())
00183         INF.warn(m())
00184         INF.info(m())
00185 
00186         DEB.log(logging.CRITICAL, m())
00187         DEB.error(m())
00188         DEB.warn (m())
00189         DEB.info (m())
00190         DEB.debug(m())
00191 
00192         # These should not log.
00193         ERR.warn(m())
00194         ERR.info(m())
00195         ERR.debug(m())
00196 
00197         INF.debug(m())
00198 
00199         self.assert_log_lines([
00200             ('ERR', 'CRITICAL', '1'),
00201             ('ERR', 'ERROR', '2'),
00202             ('INF', 'CRITICAL', '3'),
00203             ('INF', 'ERROR', '4'),
00204             ('INF', 'WARNING', '5'),
00205             ('INF', 'INFO', '6'),
00206             ('DEB', 'CRITICAL', '7'),
00207             ('DEB', 'ERROR', '8'),
00208             ('DEB', 'WARNING', '9'),
00209             ('DEB', 'INFO', '10'),
00210             ('DEB', 'DEBUG', '11'),
00211         ])

Here is the call graph for this function:

Definition at line 212 of file test_logging.py.

00212 
00213     def test_nested_explicit(self):
00214         # Logging levels in a nested namespace, all explicitly set.
00215         m = self.next_message
00216 
00217         INF = logging.getLogger("INF")
00218         INF.setLevel(logging.INFO)
00219         INF_ERR  = logging.getLogger("INF.ERR")
00220         INF_ERR.setLevel(logging.ERROR)
00221 
00222         # These should log.
00223         INF_ERR.log(logging.CRITICAL, m())
00224         INF_ERR.error(m())
00225 
00226         # These should not log.
00227         INF_ERR.warn(m())
00228         INF_ERR.info(m())
00229         INF_ERR.debug(m())
00230 
00231         self.assert_log_lines([
00232             ('INF.ERR', 'CRITICAL', '1'),
00233             ('INF.ERR', 'ERROR', '2'),
00234         ])

Here is the call graph for this function:

Definition at line 235 of file test_logging.py.

00235 
00236     def test_nested_inherited(self):
00237         #Logging levels in a nested namespace, inherited from parent loggers.
00238         m = self.next_message
00239 
00240         INF = logging.getLogger("INF")
00241         INF.setLevel(logging.INFO)
00242         INF_ERR  = logging.getLogger("INF.ERR")
00243         INF_ERR.setLevel(logging.ERROR)
00244         INF_UNDEF = logging.getLogger("INF.UNDEF")
00245         INF_ERR_UNDEF = logging.getLogger("INF.ERR.UNDEF")
00246         UNDEF = logging.getLogger("UNDEF")
00247 
00248         # These should log.
00249         INF_UNDEF.log(logging.CRITICAL, m())
00250         INF_UNDEF.error(m())
00251         INF_UNDEF.warn(m())
00252         INF_UNDEF.info(m())
00253         INF_ERR_UNDEF.log(logging.CRITICAL, m())
00254         INF_ERR_UNDEF.error(m())
00255 
00256         # These should not log.
00257         INF_UNDEF.debug(m())
00258         INF_ERR_UNDEF.warn(m())
00259         INF_ERR_UNDEF.info(m())
00260         INF_ERR_UNDEF.debug(m())
00261 
00262         self.assert_log_lines([
00263             ('INF.UNDEF', 'CRITICAL', '1'),
00264             ('INF.UNDEF', 'ERROR', '2'),
00265             ('INF.UNDEF', 'WARNING', '3'),
00266             ('INF.UNDEF', 'INFO', '4'),
00267             ('INF.ERR.UNDEF', 'CRITICAL', '5'),
00268             ('INF.ERR.UNDEF', 'ERROR', '6'),
00269         ])

Here is the call graph for this function:

Definition at line 270 of file test_logging.py.

00270 
00271     def test_nested_with_virtual_parent(self):
00272         # Logging levels when some parent does not exist yet.
00273         m = self.next_message
00274 
00275         INF = logging.getLogger("INF")
00276         GRANDCHILD = logging.getLogger("INF.BADPARENT.UNDEF")
00277         CHILD = logging.getLogger("INF.BADPARENT")
00278         INF.setLevel(logging.INFO)
00279 
00280         # These should log.
00281         GRANDCHILD.log(logging.FATAL, m())
00282         GRANDCHILD.info(m())
00283         CHILD.log(logging.FATAL, m())
00284         CHILD.info(m())
00285 
00286         # These should not log.
00287         GRANDCHILD.debug(m())
00288         CHILD.debug(m())
00289 
00290         self.assert_log_lines([
00291             ('INF.BADPARENT.UNDEF', 'CRITICAL', '1'),
00292             ('INF.BADPARENT.UNDEF', 'INFO', '2'),
00293             ('INF.BADPARENT', 'CRITICAL', '3'),
00294             ('INF.BADPARENT', 'INFO', '4'),
00295         ])
00296 

Here is the call graph for this function:


Member Data Documentation

string test.test_logging.BaseTest.expected_log_pat = r"^([\w.]+) -> ([\w]+): ([\d]+)$" [static, inherited]
string test.test_logging.BaseTest.log_format = "%(name)s -> %(levelname)s: %(message)s" [static, inherited]

Definition at line 59 of file test_logging.py.

Definition at line 83 of file test_logging.py.

Definition at line 84 of file test_logging.py.

Definition at line 73 of file test_logging.py.

Definition at line 61 of file test_logging.py.

Definition at line 87 of file test_logging.py.

Definition at line 92 of file test_logging.py.

Definition at line 91 of file test_logging.py.

Definition at line 86 of file test_logging.py.

Definition at line 70 of file test_logging.py.

Definition at line 69 of file test_logging.py.

Definition at line 72 of file test_logging.py.

Definition at line 71 of file test_logging.py.

Definition at line 89 of file test_logging.py.


The documentation for this class was generated from the following file: