Back to index

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

List of all members.

Public Member Functions

def setUp
def log_at_all_levels
def test_logger_filter
def test_handler_filter
def test_specific_filters
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 expected_log_pat = r"^[\w.]+ -> ([\w]+): ([\d]+)$"
string log_format = "%(name)s -> %(levelname)s: %(message)s"
int message_num = 0

Detailed Description

Test various filtering possibilities with custom logging levels.

Definition at line 408 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:

Definition at line 420 of file test_logging.py.

00420 
00421     def log_at_all_levels(self, logger):
00422         for lvl in LEVEL_RANGE:
00423             logger.log(lvl, self.next_message())

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:

Setup the default logging stream to an internal StringIO instance,
so that we can examine log output as we want.

Reimplemented from test.test_logging.BaseTest.

Definition at line 415 of file test_logging.py.

00415 
00416     def setUp(self):
00417         BaseTest.setUp(self)
00418         for k, v in my_logging_levels.items():
00419             logging.addLevelName(k, v)

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 438 of file test_logging.py.

00438 
00439     def test_handler_filter(self):
00440         # Filter at handler level.
00441         self.root_logger.handlers[0].setLevel(SOCIABLE)
00442         try:
00443             # Levels >= 'Sociable' are good.
00444             self.log_at_all_levels(self.root_logger)
00445             self.assert_log_lines([
00446                 ('Sociable', '6'),
00447                 ('Effusive', '7'),
00448                 ('Terse', '8'),
00449                 ('Taciturn', '9'),
00450                 ('Silent', '10'),
00451             ])
00452         finally:
00453             self.root_logger.handlers[0].setLevel(logging.NOTSET)

Here is the call graph for this function:

Definition at line 424 of file test_logging.py.

00424 
00425     def test_logger_filter(self):
00426         # Filter at logger level.
00427         self.root_logger.setLevel(VERBOSE)
00428         # Levels >= 'Verbose' are good.
00429         self.log_at_all_levels(self.root_logger)
00430         self.assert_log_lines([
00431             ('Verbose', '5'),
00432             ('Sociable', '6'),
00433             ('Effusive', '7'),
00434             ('Terse', '8'),
00435             ('Taciturn', '9'),
00436             ('Silent', '10'),
00437         ])

Here is the call graph for this function:

Definition at line 454 of file test_logging.py.

00454 
00455     def test_specific_filters(self):
00456         # Set a specific filter object on the handler, and then add another
00457         #  filter object on the logger itself.
00458         handler = self.root_logger.handlers[0]
00459         specific_filter = None
00460         garr = GarrulousFilter()
00461         handler.addFilter(garr)
00462         try:
00463             self.log_at_all_levels(self.root_logger)
00464             first_lines = [
00465                 # Notice how 'Garrulous' is missing
00466                 ('Boring', '1'),
00467                 ('Chatterbox', '2'),
00468                 ('Talkative', '4'),
00469                 ('Verbose', '5'),
00470                 ('Sociable', '6'),
00471                 ('Effusive', '7'),
00472                 ('Terse', '8'),
00473                 ('Taciturn', '9'),
00474                 ('Silent', '10'),
00475             ]
00476             self.assert_log_lines(first_lines)
00477 
00478             specific_filter = VerySpecificFilter()
00479             self.root_logger.addFilter(specific_filter)
00480             self.log_at_all_levels(self.root_logger)
00481             self.assert_log_lines(first_lines + [
00482                 # Not only 'Garrulous' is still missing, but also 'Sociable'
00483                 # and 'Taciturn'
00484                 ('Boring', '11'),
00485                 ('Chatterbox', '12'),
00486                 ('Talkative', '14'),
00487                 ('Verbose', '15'),
00488                 ('Effusive', '17'),
00489                 ('Terse', '18'),
00490                 ('Silent', '20'),
00491         ])
00492         finally:
00493             if specific_filter:
00494                 self.root_logger.removeFilter(specific_filter)
00495             handler.removeFilter(garr)
00496 

Here is the call graph for this function:


Member Data Documentation

string test.test_logging.CustomLevelsAndFiltersTest.expected_log_pat = r"^[\w.]+ -> ([\w]+): ([\d]+)$" [static]

Reimplemented from test.test_logging.BaseTest.

Definition at line 413 of file test_logging.py.

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: