annotate Lib/test/test_logging.py @ 54294:dba767bc3155 legacy-trunk

remove mention of old ctypes version
author Benjamin Peterson <benjamin@python.org>
date Sat, 23 May 2009 20:59:09 +0000
parents 73bfcb398886
children 6768b2350c57
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
27024
1098ccc84d63 SF #660795
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
1 #!/usr/bin/env python
45623
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
2 #
51474
e49013f1d831 Issue #5170: Fixed Unicode output bug in logging and added test case. This is a regression which did not occur in 2.5.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 49955
diff changeset
3 # Copyright 2001-2009 by Vinay Sajip. All Rights Reserved.
45623
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
4 #
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
5 # Permission to use, copy, modify, and distribute this software and its
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
6 # documentation for any purpose and without fee is hereby granted,
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
7 # provided that the above copyright notice appear in all copies and that
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
8 # both that copyright notice and this permission notice appear in
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
9 # supporting documentation, and that the name of Vinay Sajip
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
10 # not be used in advertising or publicity pertaining to distribution
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
11 # of the software without specific, written prior permission.
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
12 # VINAY SAJIP DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
13 # ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
14 # VINAY SAJIP BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
15 # ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
16 # IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
17 # OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
45375
6550659dbc9d Move test_logging over to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 41114
diff changeset
18
45623
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
19 """Test harness for the logging module. Run all tests.
45375
6550659dbc9d Move test_logging over to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 41114
diff changeset
20
51474
e49013f1d831 Issue #5170: Fixed Unicode output bug in logging and added test case. This is a regression which did not occur in 2.5.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 49955
diff changeset
21 Copyright (C) 2001-2009 Vinay Sajip. All Rights Reserved.
45623
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
22 """
45375
6550659dbc9d Move test_logging over to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 41114
diff changeset
23
45623
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
24 import logging
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
25 import logging.handlers
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
26 import logging.config
45375
6550659dbc9d Move test_logging over to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 41114
diff changeset
27
51474
e49013f1d831 Issue #5170: Fixed Unicode output bug in logging and added test case. This is a regression which did not occur in 2.5.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 49955
diff changeset
28 import codecs
45623
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
29 import copy
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
30 import cPickle
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
31 import cStringIO
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
32 import gc
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
33 import os
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
34 import re
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
35 import select
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
36 import socket
47241
ba1fa511b72d socketserver renaming reversal part 3: move the module into the right
Georg Brandl <georg@python.org>
parents: 46929
diff changeset
37 from SocketServer import ThreadingTCPServer, StreamRequestHandler
45623
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
38 import string
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
39 import struct
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
40 import sys
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
41 import tempfile
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
42 from test.test_support import captured_stdout, run_with_locale, run_unittest
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
43 import textwrap
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
44 import threading
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
45 import time
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
46 import types
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
47 import unittest
49955
878d40468c4e Issue #4384: Added logging integration with warnings module using captureWarnings(). This change includes a NullHandler which does nothing; it will be of use to library developers who want to avoid the "No handlers could be found for logger XXX" message which can appear if the library user doesn't configure logging.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 49109
diff changeset
48 import warnings
45623
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
49 import weakref
45375
6550659dbc9d Move test_logging over to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 41114
diff changeset
50
6550659dbc9d Move test_logging over to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 41114
diff changeset
51
45623
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
52 class BaseTest(unittest.TestCase):
45375
6550659dbc9d Move test_logging over to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 41114
diff changeset
53
45623
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
54 """Base class for logging tests."""
45375
6550659dbc9d Move test_logging over to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 41114
diff changeset
55
45623
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
56 log_format = "%(name)s -> %(levelname)s: %(message)s"
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
57 expected_log_pat = r"^([\w.]+) -> ([\w]+): ([\d]+)$"
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
58 message_num = 0
45375
6550659dbc9d Move test_logging over to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 41114
diff changeset
59
45623
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
60 def setUp(self):
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
61 """Setup the default logging stream to an internal StringIO instance,
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
62 so that we can examine log output as we want."""
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
63 logger_dict = logging.getLogger().manager.loggerDict
45375
6550659dbc9d Move test_logging over to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 41114
diff changeset
64 logging._acquireLock()
6550659dbc9d Move test_logging over to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 41114
diff changeset
65 try:
45623
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
66 self.saved_handlers = logging._handlers.copy()
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
67 self.saved_handler_list = logging._handlerList[:]
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
68 self.saved_loggers = logger_dict.copy()
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
69 self.saved_level_names = logging._levelNames.copy()
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
70 finally:
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
71 logging._releaseLock()
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
72
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
73 self.root_logger = logging.getLogger("")
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
74 self.original_logging_level = self.root_logger.getEffectiveLevel()
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
75
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
76 self.stream = cStringIO.StringIO()
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
77 self.root_logger.setLevel(logging.DEBUG)
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
78 self.root_hdlr = logging.StreamHandler(self.stream)
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
79 self.root_formatter = logging.Formatter(self.log_format)
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
80 self.root_hdlr.setFormatter(self.root_formatter)
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
81 self.root_logger.addHandler(self.root_hdlr)
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
82
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
83 def tearDown(self):
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
84 """Remove our logging stream, and restore the original logging
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
85 level."""
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
86 self.stream.close()
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
87 self.root_logger.removeHandler(self.root_hdlr)
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
88 self.root_logger.setLevel(self.original_logging_level)
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
89 logging._acquireLock()
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
90 try:
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
91 logging._levelNames.clear()
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
92 logging._levelNames.update(self.saved_level_names)
45375
6550659dbc9d Move test_logging over to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 41114
diff changeset
93 logging._handlers.clear()
45623
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
94 logging._handlers.update(self.saved_handlers)
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
95 logging._handlerList[:] = self.saved_handler_list
45375
6550659dbc9d Move test_logging over to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 41114
diff changeset
96 loggerDict = logging.getLogger().manager.loggerDict
6550659dbc9d Move test_logging over to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 41114
diff changeset
97 loggerDict.clear()
45623
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
98 loggerDict.update(self.saved_loggers)
45375
6550659dbc9d Move test_logging over to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 41114
diff changeset
99 finally:
6550659dbc9d Move test_logging over to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 41114
diff changeset
100 logging._releaseLock()
6550659dbc9d Move test_logging over to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 41114
diff changeset
101
45623
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
102 def assert_log_lines(self, expected_values, stream=None):
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
103 """Match the collected log lines against the regular expression
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
104 self.expected_log_pat, and compare the extracted group values to
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
105 the expected_values list of tuples."""
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
106 stream = stream or self.stream
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
107 pat = re.compile(self.expected_log_pat)
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
108 try:
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
109 stream.reset()
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
110 actual_lines = stream.readlines()
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
111 except AttributeError:
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
112 # StringIO.StringIO lacks a reset() method.
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
113 actual_lines = stream.getvalue().splitlines()
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
114 self.assertEquals(len(actual_lines), len(expected_values))
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
115 for actual, expected in zip(actual_lines, expected_values):
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
116 match = pat.search(actual)
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
117 if not match:
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
118 self.fail("Log line does not match expected pattern:\n" +
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
119 actual)
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
120 self.assertEquals(tuple(match.groups()), expected)
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
121 s = stream.read()
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
122 if s:
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
123 self.fail("Remaining output at end of log stream:\n" + s)
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
124
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
125 def next_message(self):
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
126 """Generate a message consisting solely of an auto-incrementing
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
127 integer."""
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
128 self.message_num += 1
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
129 return "%d" % self.message_num
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
130
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
131
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
132 class BuiltinLevelsTest(BaseTest):
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
133 """Test builtin levels and their inheritance."""
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
134
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
135 def test_flat(self):
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
136 #Logging levels in a flat logger namespace.
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
137 m = self.next_message
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
138
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
139 ERR = logging.getLogger("ERR")
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
140 ERR.setLevel(logging.ERROR)
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
141 INF = logging.getLogger("INF")
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
142 INF.setLevel(logging.INFO)
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
143 DEB = logging.getLogger("DEB")
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
144 DEB.setLevel(logging.DEBUG)
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
145
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
146 # These should log.
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
147 ERR.log(logging.CRITICAL, m())
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
148 ERR.error(m())
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
149
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
150 INF.log(logging.CRITICAL, m())
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
151 INF.error(m())
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
152 INF.warn(m())
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
153 INF.info(m())
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
154
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
155 DEB.log(logging.CRITICAL, m())
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
156 DEB.error(m())
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
157 DEB.warn (m())
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
158 DEB.info (m())
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
159 DEB.debug(m())
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
160
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
161 # These should not log.
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
162 ERR.warn(m())
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
163 ERR.info(m())
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
164 ERR.debug(m())
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
165
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
166 INF.debug(m())
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
167
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
168 self.assert_log_lines([
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
169 ('ERR', 'CRITICAL', '1'),
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
170 ('ERR', 'ERROR', '2'),
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
171 ('INF', 'CRITICAL', '3'),
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
172 ('INF', 'ERROR', '4'),
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
173 ('INF', 'WARNING', '5'),
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
174 ('INF', 'INFO', '6'),
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
175 ('DEB', 'CRITICAL', '7'),
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
176 ('DEB', 'ERROR', '8'),
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
177 ('DEB', 'WARNING', '9'),
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
178 ('DEB', 'INFO', '10'),
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
179 ('DEB', 'DEBUG', '11'),
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
180 ])
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
181
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
182 def test_nested_explicit(self):
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
183 # Logging levels in a nested namespace, all explicitly set.
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
184 m = self.next_message
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
185
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
186 INF = logging.getLogger("INF")
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
187 INF.setLevel(logging.INFO)
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
188 INF_ERR = logging.getLogger("INF.ERR")
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
189 INF_ERR.setLevel(logging.ERROR)
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
190
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
191 # These should log.
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
192 INF_ERR.log(logging.CRITICAL, m())
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
193 INF_ERR.error(m())
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
194
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
195 # These should not log.
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
196 INF_ERR.warn(m())
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
197 INF_ERR.info(m())
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
198 INF_ERR.debug(m())
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
199
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
200 self.assert_log_lines([
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
201 ('INF.ERR', 'CRITICAL', '1'),
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
202 ('INF.ERR', 'ERROR', '2'),
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
203 ])
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
204
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
205 def test_nested_inherited(self):
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
206 #Logging levels in a nested namespace, inherited from parent loggers.
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
207 m = self.next_message
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
208
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
209 INF = logging.getLogger("INF")
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
210 INF.setLevel(logging.INFO)
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
211 INF_ERR = logging.getLogger("INF.ERR")
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
212 INF_ERR.setLevel(logging.ERROR)
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
213 INF_UNDEF = logging.getLogger("INF.UNDEF")
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
214 INF_ERR_UNDEF = logging.getLogger("INF.ERR.UNDEF")
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
215 UNDEF = logging.getLogger("UNDEF")
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
216
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
217 # These should log.
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
218 INF_UNDEF.log(logging.CRITICAL, m())
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
219 INF_UNDEF.error(m())
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
220 INF_UNDEF.warn(m())
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
221 INF_UNDEF.info(m())
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
222 INF_ERR_UNDEF.log(logging.CRITICAL, m())
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
223 INF_ERR_UNDEF.error(m())
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
224
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
225 # These should not log.
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
226 INF_UNDEF.debug(m())
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
227 INF_ERR_UNDEF.warn(m())
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
228 INF_ERR_UNDEF.info(m())
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
229 INF_ERR_UNDEF.debug(m())
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
230
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
231 self.assert_log_lines([
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
232 ('INF.UNDEF', 'CRITICAL', '1'),
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
233 ('INF.UNDEF', 'ERROR', '2'),
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
234 ('INF.UNDEF', 'WARNING', '3'),
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
235 ('INF.UNDEF', 'INFO', '4'),
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
236 ('INF.ERR.UNDEF', 'CRITICAL', '5'),
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
237 ('INF.ERR.UNDEF', 'ERROR', '6'),
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
238 ])
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
239
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
240 def test_nested_with_virtual_parent(self):
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
241 # Logging levels when some parent does not exist yet.
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
242 m = self.next_message
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
243
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
244 INF = logging.getLogger("INF")
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
245 GRANDCHILD = logging.getLogger("INF.BADPARENT.UNDEF")
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
246 CHILD = logging.getLogger("INF.BADPARENT")
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
247 INF.setLevel(logging.INFO)
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
248
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
249 # These should log.
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
250 GRANDCHILD.log(logging.FATAL, m())
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
251 GRANDCHILD.info(m())
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
252 CHILD.log(logging.FATAL, m())
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
253 CHILD.info(m())
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
254
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
255 # These should not log.
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
256 GRANDCHILD.debug(m())
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
257 CHILD.debug(m())
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
258
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
259 self.assert_log_lines([
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
260 ('INF.BADPARENT.UNDEF', 'CRITICAL', '1'),
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
261 ('INF.BADPARENT.UNDEF', 'INFO', '2'),
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
262 ('INF.BADPARENT', 'CRITICAL', '3'),
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
263 ('INF.BADPARENT', 'INFO', '4'),
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
264 ])
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
265
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
266
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
267 class BasicFilterTest(BaseTest):
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
268
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
269 """Test the bundled Filter class."""
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
270
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
271 def test_filter(self):
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
272 # Only messages satisfying the specified criteria pass through the
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
273 # filter.
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
274 filter_ = logging.Filter("spam.eggs")
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
275 handler = self.root_logger.handlers[0]
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
276 try:
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
277 handler.addFilter(filter_)
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
278 spam = logging.getLogger("spam")
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
279 spam_eggs = logging.getLogger("spam.eggs")
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
280 spam_eggs_fish = logging.getLogger("spam.eggs.fish")
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
281 spam_bakedbeans = logging.getLogger("spam.bakedbeans")
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
282
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
283 spam.info(self.next_message())
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
284 spam_eggs.info(self.next_message()) # Good.
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
285 spam_eggs_fish.info(self.next_message()) # Good.
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
286 spam_bakedbeans.info(self.next_message())
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
287
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
288 self.assert_log_lines([
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
289 ('spam.eggs', 'INFO', '2'),
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
290 ('spam.eggs.fish', 'INFO', '3'),
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
291 ])
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
292 finally:
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
293 handler.removeFilter(filter_)
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
294
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
295
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
296 #
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
297 # First, we define our levels. There can be as many as you want - the only
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
298 # limitations are that they should be integers, the lowest should be > 0 and
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
299 # larger values mean less information being logged. If you need specific
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
300 # level values which do not fit into these limitations, you can use a
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
301 # mapping dictionary to convert between your application levels and the
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
302 # logging system.
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
303 #
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
304 SILENT = 120
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
305 TACITURN = 119
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
306 TERSE = 118
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
307 EFFUSIVE = 117
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
308 SOCIABLE = 116
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
309 VERBOSE = 115
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
310 TALKATIVE = 114
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
311 GARRULOUS = 113
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
312 CHATTERBOX = 112
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
313 BORING = 111
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
314
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
315 LEVEL_RANGE = range(BORING, SILENT + 1)
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
316
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
317 #
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
318 # Next, we define names for our levels. You don't need to do this - in which
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
319 # case the system will use "Level n" to denote the text for the level.
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
320 #
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
321 my_logging_levels = {
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
322 SILENT : 'Silent',
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
323 TACITURN : 'Taciturn',
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
324 TERSE : 'Terse',
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
325 EFFUSIVE : 'Effusive',
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
326 SOCIABLE : 'Sociable',
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
327 VERBOSE : 'Verbose',
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
328 TALKATIVE : 'Talkative',
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
329 GARRULOUS : 'Garrulous',
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
330 CHATTERBOX : 'Chatterbox',
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
331 BORING : 'Boring',
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
332 }
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
333
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
334 class GarrulousFilter(logging.Filter):
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
335
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
336 """A filter which blocks garrulous messages."""
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
337
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
338 def filter(self, record):
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
339 return record.levelno != GARRULOUS
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
340
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
341 class VerySpecificFilter(logging.Filter):
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
342
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
343 """A filter which blocks sociable and taciturn messages."""
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
344
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
345 def filter(self, record):
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
346 return record.levelno not in [SOCIABLE, TACITURN]
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
347
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
348
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
349 class CustomLevelsAndFiltersTest(BaseTest):
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
350
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
351 """Test various filtering possibilities with custom logging levels."""
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
352
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
353 # Skip the logger name group.
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
354 expected_log_pat = r"^[\w.]+ -> ([\w]+): ([\d]+)$"
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
355
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
356 def setUp(self):
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
357 BaseTest.setUp(self)
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
358 for k, v in my_logging_levels.items():
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
359 logging.addLevelName(k, v)
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
360
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
361 def log_at_all_levels(self, logger):
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
362 for lvl in LEVEL_RANGE:
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
363 logger.log(lvl, self.next_message())
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
364
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
365 def test_logger_filter(self):
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
366 # Filter at logger level.
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
367 self.root_logger.setLevel(VERBOSE)
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
368 # Levels >= 'Verbose' are good.
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
369 self.log_at_all_levels(self.root_logger)
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
370 self.assert_log_lines([
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
371 ('Verbose', '5'),
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
372 ('Sociable', '6'),
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
373 ('Effusive', '7'),
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
374 ('Terse', '8'),
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
375 ('Taciturn', '9'),
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
376 ('Silent', '10'),
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
377 ])
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
378
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
379 def test_handler_filter(self):
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
380 # Filter at handler level.
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
381 self.root_logger.handlers[0].setLevel(SOCIABLE)
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
382 try:
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
383 # Levels >= 'Sociable' are good.
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
384 self.log_at_all_levels(self.root_logger)
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
385 self.assert_log_lines([
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
386 ('Sociable', '6'),
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
387 ('Effusive', '7'),
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
388 ('Terse', '8'),
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
389 ('Taciturn', '9'),
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
390 ('Silent', '10'),
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
391 ])
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
392 finally:
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
393 self.root_logger.handlers[0].setLevel(logging.NOTSET)
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
394
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
395 def test_specific_filters(self):
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
396 # Set a specific filter object on the handler, and then add another
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
397 # filter object on the logger itself.
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
398 handler = self.root_logger.handlers[0]
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
399 specific_filter = None
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
400 garr = GarrulousFilter()
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
401 handler.addFilter(garr)
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
402 try:
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
403 self.log_at_all_levels(self.root_logger)
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
404 first_lines = [
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
405 # Notice how 'Garrulous' is missing
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
406 ('Boring', '1'),
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
407 ('Chatterbox', '2'),
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
408 ('Talkative', '4'),
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
409 ('Verbose', '5'),
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
410 ('Sociable', '6'),
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
411 ('Effusive', '7'),
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
412 ('Terse', '8'),
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
413 ('Taciturn', '9'),
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
414 ('Silent', '10'),
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
415 ]
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
416 self.assert_log_lines(first_lines)
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
417
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
418 specific_filter = VerySpecificFilter()
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
419 self.root_logger.addFilter(specific_filter)
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
420 self.log_at_all_levels(self.root_logger)
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
421 self.assert_log_lines(first_lines + [
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
422 # Not only 'Garrulous' is still missing, but also 'Sociable'
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
423 # and 'Taciturn'
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
424 ('Boring', '11'),
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
425 ('Chatterbox', '12'),
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
426 ('Talkative', '14'),
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
427 ('Verbose', '15'),
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
428 ('Effusive', '17'),
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
429 ('Terse', '18'),
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
430 ('Silent', '20'),
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
431 ])
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
432 finally:
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
433 if specific_filter:
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
434 self.root_logger.removeFilter(specific_filter)
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
435 handler.removeFilter(garr)
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
436
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
437
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
438 class MemoryHandlerTest(BaseTest):
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
439
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
440 """Tests for the MemoryHandler."""
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
441
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
442 # Do not bother with a logger name group.
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
443 expected_log_pat = r"^[\w.]+ -> ([\w]+): ([\d]+)$"
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
444
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
445 def setUp(self):
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
446 BaseTest.setUp(self)
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
447 self.mem_hdlr = logging.handlers.MemoryHandler(10, logging.WARNING,
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
448 self.root_hdlr)
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
449 self.mem_logger = logging.getLogger('mem')
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
450 self.mem_logger.propagate = 0
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
451 self.mem_logger.addHandler(self.mem_hdlr)
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
452
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
453 def tearDown(self):
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
454 self.mem_hdlr.close()
46183
c440a5737149 Correctly call the base class tearDown();
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 45623
diff changeset
455 BaseTest.tearDown(self)
45623
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
456
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
457 def test_flush(self):
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
458 # The memory handler flushes to its target handler based on specific
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
459 # criteria (message count and message level).
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
460 self.mem_logger.debug(self.next_message())
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
461 self.assert_log_lines([])
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
462 self.mem_logger.info(self.next_message())
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
463 self.assert_log_lines([])
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
464 # This will flush because the level is >= logging.WARNING
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
465 self.mem_logger.warn(self.next_message())
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
466 lines = [
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
467 ('DEBUG', '1'),
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
468 ('INFO', '2'),
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
469 ('WARNING', '3'),
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
470 ]
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
471 self.assert_log_lines(lines)
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
472 for n in (4, 14):
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
473 for i in range(9):
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
474 self.mem_logger.debug(self.next_message())
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
475 self.assert_log_lines(lines)
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
476 # This will flush because it's the 10th message since the last
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
477 # flush.
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
478 self.mem_logger.debug(self.next_message())
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
479 lines = lines + [('DEBUG', str(i)) for i in range(n, n + 10)]
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
480 self.assert_log_lines(lines)
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
481
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
482 self.mem_logger.debug(self.next_message())
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
483 self.assert_log_lines(lines)
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
484
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
485
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
486 class ExceptionFormatter(logging.Formatter):
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
487 """A special exception formatter."""
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
488 def formatException(self, ei):
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
489 return "Got a [%s]" % ei[0].__name__
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
490
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
491
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
492 class ConfigFileTest(BaseTest):
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
493
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
494 """Reading logging config from a .ini-style config file."""
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
495
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
496 expected_log_pat = r"^([\w]+) \+\+ ([\w]+)$"
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
497
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
498 # config0 is a standard configuration.
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
499 config0 = """
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
500 [loggers]
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
501 keys=root
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
502
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
503 [handlers]
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
504 keys=hand1
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
505
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
506 [formatters]
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
507 keys=form1
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
508
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
509 [logger_root]
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
510 level=WARNING
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
511 handlers=hand1
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
512
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
513 [handler_hand1]
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
514 class=StreamHandler
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
515 level=NOTSET
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
516 formatter=form1
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
517 args=(sys.stdout,)
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
518
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
519 [formatter_form1]
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
520 format=%(levelname)s ++ %(message)s
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
521 datefmt=
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
522 """
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
523
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
524 # config1 adds a little to the standard configuration.
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
525 config1 = """
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
526 [loggers]
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
527 keys=root,parser
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
528
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
529 [handlers]
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
530 keys=hand1
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
531
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
532 [formatters]
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
533 keys=form1
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
534
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
535 [logger_root]
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
536 level=WARNING
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
537 handlers=
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
538
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
539 [logger_parser]
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
540 level=DEBUG
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
541 handlers=hand1
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
542 propagate=1
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
543 qualname=compiler.parser
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
544
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
545 [handler_hand1]
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
546 class=StreamHandler
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
547 level=NOTSET
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
548 formatter=form1
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
549 args=(sys.stdout,)
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
550
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
551 [formatter_form1]
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
552 format=%(levelname)s ++ %(message)s
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
553 datefmt=
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
554 """
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
555
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
556 # config2 has a subtle configuration error that should be reported
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
557 config2 = config1.replace("sys.stdout", "sys.stbout")
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
558
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
559 # config3 has a less subtle configuration error
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
560 config3 = config1.replace("formatter=form1", "formatter=misspelled_name")
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
561
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
562 # config4 specifies a custom formatter class to be loaded
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
563 config4 = """
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
564 [loggers]
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
565 keys=root
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
566
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
567 [handlers]
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
568 keys=hand1
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
569
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
570 [formatters]
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
571 keys=form1
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
572
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
573 [logger_root]
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
574 level=NOTSET
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
575 handlers=hand1
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
576
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
577 [handler_hand1]
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
578 class=StreamHandler
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
579 level=NOTSET
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
580 formatter=form1
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
581 args=(sys.stdout,)
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
582
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
583 [formatter_form1]
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
584 class=""" + __name__ + """.ExceptionFormatter
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
585 format=%(levelname)s:%(name)s:%(message)s
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
586 datefmt=
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
587 """
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
588
48010
8ec1830d5719 Issue #3389: Allow resolving dotted names for handlers in logging configuration files. Thanks to Philip Jenvey for the patch.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 47241
diff changeset
589 # config5 specifies a custom handler class to be loaded
8ec1830d5719 Issue #3389: Allow resolving dotted names for handlers in logging configuration files. Thanks to Philip Jenvey for the patch.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 47241
diff changeset
590 config5 = config1.replace('class=StreamHandler', 'class=logging.StreamHandler')
8ec1830d5719 Issue #3389: Allow resolving dotted names for handlers in logging configuration files. Thanks to Philip Jenvey for the patch.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 47241
diff changeset
591
48980
5fd39b84fc29 Issue #3726: Allowed spaces in separators in logging configuration files.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 48010
diff changeset
592 # config6 uses ', ' delimiters in the handlers and formatters sections
5fd39b84fc29 Issue #3726: Allowed spaces in separators in logging configuration files.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 48010
diff changeset
593 config6 = """
5fd39b84fc29 Issue #3726: Allowed spaces in separators in logging configuration files.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 48010
diff changeset
594 [loggers]
5fd39b84fc29 Issue #3726: Allowed spaces in separators in logging configuration files.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 48010
diff changeset
595 keys=root,parser
5fd39b84fc29 Issue #3726: Allowed spaces in separators in logging configuration files.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 48010
diff changeset
596
5fd39b84fc29 Issue #3726: Allowed spaces in separators in logging configuration files.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 48010
diff changeset
597 [handlers]
5fd39b84fc29 Issue #3726: Allowed spaces in separators in logging configuration files.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 48010
diff changeset
598 keys=hand1, hand2
5fd39b84fc29 Issue #3726: Allowed spaces in separators in logging configuration files.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 48010
diff changeset
599
5fd39b84fc29 Issue #3726: Allowed spaces in separators in logging configuration files.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 48010
diff changeset
600 [formatters]
5fd39b84fc29 Issue #3726: Allowed spaces in separators in logging configuration files.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 48010
diff changeset
601 keys=form1, form2
5fd39b84fc29 Issue #3726: Allowed spaces in separators in logging configuration files.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 48010
diff changeset
602
5fd39b84fc29 Issue #3726: Allowed spaces in separators in logging configuration files.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 48010
diff changeset
603 [logger_root]
5fd39b84fc29 Issue #3726: Allowed spaces in separators in logging configuration files.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 48010
diff changeset
604 level=WARNING
5fd39b84fc29 Issue #3726: Allowed spaces in separators in logging configuration files.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 48010
diff changeset
605 handlers=
5fd39b84fc29 Issue #3726: Allowed spaces in separators in logging configuration files.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 48010
diff changeset
606
5fd39b84fc29 Issue #3726: Allowed spaces in separators in logging configuration files.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 48010
diff changeset
607 [logger_parser]
5fd39b84fc29 Issue #3726: Allowed spaces in separators in logging configuration files.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 48010
diff changeset
608 level=DEBUG
5fd39b84fc29 Issue #3726: Allowed spaces in separators in logging configuration files.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 48010
diff changeset
609 handlers=hand1
5fd39b84fc29 Issue #3726: Allowed spaces in separators in logging configuration files.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 48010
diff changeset
610 propagate=1
5fd39b84fc29 Issue #3726: Allowed spaces in separators in logging configuration files.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 48010
diff changeset
611 qualname=compiler.parser
5fd39b84fc29 Issue #3726: Allowed spaces in separators in logging configuration files.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 48010
diff changeset
612
5fd39b84fc29 Issue #3726: Allowed spaces in separators in logging configuration files.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 48010
diff changeset
613 [handler_hand1]
5fd39b84fc29 Issue #3726: Allowed spaces in separators in logging configuration files.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 48010
diff changeset
614 class=StreamHandler
5fd39b84fc29 Issue #3726: Allowed spaces in separators in logging configuration files.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 48010
diff changeset
615 level=NOTSET
5fd39b84fc29 Issue #3726: Allowed spaces in separators in logging configuration files.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 48010
diff changeset
616 formatter=form1
5fd39b84fc29 Issue #3726: Allowed spaces in separators in logging configuration files.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 48010
diff changeset
617 args=(sys.stdout,)
5fd39b84fc29 Issue #3726: Allowed spaces in separators in logging configuration files.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 48010
diff changeset
618
5fd39b84fc29 Issue #3726: Allowed spaces in separators in logging configuration files.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 48010
diff changeset
619 [handler_hand2]
49109
36b2d674fc8c Issue #3809: Fixed spurious 'test.blah' file left behind by test_logging.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 49009
diff changeset
620 class=StreamHandler
48980
5fd39b84fc29 Issue #3726: Allowed spaces in separators in logging configuration files.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 48010
diff changeset
621 level=NOTSET
5fd39b84fc29 Issue #3726: Allowed spaces in separators in logging configuration files.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 48010
diff changeset
622 formatter=form1
49109
36b2d674fc8c Issue #3809: Fixed spurious 'test.blah' file left behind by test_logging.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 49009
diff changeset
623 args=(sys.stderr,)
48980
5fd39b84fc29 Issue #3726: Allowed spaces in separators in logging configuration files.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 48010
diff changeset
624
5fd39b84fc29 Issue #3726: Allowed spaces in separators in logging configuration files.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 48010
diff changeset
625 [formatter_form1]
5fd39b84fc29 Issue #3726: Allowed spaces in separators in logging configuration files.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 48010
diff changeset
626 format=%(levelname)s ++ %(message)s
5fd39b84fc29 Issue #3726: Allowed spaces in separators in logging configuration files.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 48010
diff changeset
627 datefmt=
5fd39b84fc29 Issue #3726: Allowed spaces in separators in logging configuration files.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 48010
diff changeset
628
5fd39b84fc29 Issue #3726: Allowed spaces in separators in logging configuration files.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 48010
diff changeset
629 [formatter_form2]
5fd39b84fc29 Issue #3726: Allowed spaces in separators in logging configuration files.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 48010
diff changeset
630 format=%(message)s
5fd39b84fc29 Issue #3726: Allowed spaces in separators in logging configuration files.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 48010
diff changeset
631 datefmt=
5fd39b84fc29 Issue #3726: Allowed spaces in separators in logging configuration files.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 48010
diff changeset
632 """
5fd39b84fc29 Issue #3726: Allowed spaces in separators in logging configuration files.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 48010
diff changeset
633
45623
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
634 def apply_config(self, conf):
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
635 try:
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
636 fn = tempfile.mktemp(".ini")
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
637 f = open(fn, "w")
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
638 f.write(textwrap.dedent(conf))
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
639 f.close()
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
640 logging.config.fileConfig(fn)
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
641 finally:
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
642 os.remove(fn)
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
643
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
644 def test_config0_ok(self):
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
645 # A simple config file which overrides the default settings.
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
646 with captured_stdout() as output:
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
647 self.apply_config(self.config0)
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
648 logger = logging.getLogger()
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
649 # Won't output anything
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
650 logger.info(self.next_message())
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
651 # Outputs a message
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
652 logger.error(self.next_message())
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
653 self.assert_log_lines([
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
654 ('ERROR', '2'),
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
655 ], stream=output)
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
656 # Original logger output is empty.
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
657 self.assert_log_lines([])
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
658
48010
8ec1830d5719 Issue #3389: Allow resolving dotted names for handlers in logging configuration files. Thanks to Philip Jenvey for the patch.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 47241
diff changeset
659 def test_config1_ok(self, config=config1):
45623
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
660 # A config file defining a sub-parser as well.
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
661 with captured_stdout() as output:
48010
8ec1830d5719 Issue #3389: Allow resolving dotted names for handlers in logging configuration files. Thanks to Philip Jenvey for the patch.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 47241
diff changeset
662 self.apply_config(config)
45623
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
663 logger = logging.getLogger("compiler.parser")
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
664 # Both will output a message
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
665 logger.info(self.next_message())
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
666 logger.error(self.next_message())
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
667 self.assert_log_lines([
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
668 ('INFO', '1'),
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
669 ('ERROR', '2'),
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
670 ], stream=output)
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
671 # Original logger output is empty.
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
672 self.assert_log_lines([])
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
673
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
674 def test_config2_failure(self):
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
675 # A simple config file which overrides the default settings.
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
676 self.assertRaises(StandardError, self.apply_config, self.config2)
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
677
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
678 def test_config3_failure(self):
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
679 # A simple config file which overrides the default settings.
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
680 self.assertRaises(StandardError, self.apply_config, self.config3)
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
681
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
682 def test_config4_ok(self):
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
683 # A config file specifying a custom formatter class.
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
684 with captured_stdout() as output:
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
685 self.apply_config(self.config4)
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
686 logger = logging.getLogger()
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
687 try:
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
688 raise RuntimeError()
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
689 except RuntimeError:
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
690 logging.exception("just testing")
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
691 sys.stdout.seek(0)
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
692 self.assertEquals(output.getvalue(),
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
693 "ERROR:root:just testing\nGot a [RuntimeError]\n")
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
694 # Original logger output is empty
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
695 self.assert_log_lines([])
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
696
48010
8ec1830d5719 Issue #3389: Allow resolving dotted names for handlers in logging configuration files. Thanks to Philip Jenvey for the patch.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 47241
diff changeset
697 def test_config5_ok(self):
8ec1830d5719 Issue #3389: Allow resolving dotted names for handlers in logging configuration files. Thanks to Philip Jenvey for the patch.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 47241
diff changeset
698 self.test_config1_ok(config=self.config5)
45623
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
699
48980
5fd39b84fc29 Issue #3726: Allowed spaces in separators in logging configuration files.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 48010
diff changeset
700 def test_config6_ok(self):
5fd39b84fc29 Issue #3726: Allowed spaces in separators in logging configuration files.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 48010
diff changeset
701 self.test_config1_ok(config=self.config6)
5fd39b84fc29 Issue #3726: Allowed spaces in separators in logging configuration files.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 48010
diff changeset
702
45623
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
703 class LogRecordStreamHandler(StreamRequestHandler):
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
704
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
705 """Handler for a streaming logging request. It saves the log message in the
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
706 TCP server's 'log_output' attribute."""
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
707
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
708 TCP_LOG_END = "!!!END!!!"
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
709
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
710 def handle(self):
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
711 """Handle multiple requests - each expected to be of 4-byte length,
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
712 followed by the LogRecord in pickle format. Logs the record
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
713 according to whatever policy is configured locally."""
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
714 while True:
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
715 chunk = self.connection.recv(4)
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
716 if len(chunk) < 4:
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
717 break
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
718 slen = struct.unpack(">L", chunk)[0]
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
719 chunk = self.connection.recv(slen)
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
720 while len(chunk) < slen:
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
721 chunk = chunk + self.connection.recv(slen - len(chunk))
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
722 obj = self.unpickle(chunk)
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
723 record = logging.makeLogRecord(obj)
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
724 self.handle_log_record(record)
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
725
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
726 def unpickle(self, data):
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
727 return cPickle.loads(data)
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
728
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
729 def handle_log_record(self, record):
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
730 # If the end-of-messages sentinel is seen, tell the server to
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
731 # terminate.
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
732 if self.TCP_LOG_END in record.msg:
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
733 self.server.abort = 1
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
734 return
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
735 self.server.log_output += record.msg + "\n"
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
736
28824
0cd8d570c591 New version from Vinaj, should solve the threading problems (hopefully).
Guido van Rossum <guido@python.org>
parents: 27932
diff changeset
737
27024
1098ccc84d63 SF #660795
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
738 class LogRecordSocketReceiver(ThreadingTCPServer):
45623
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
739
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
740 """A simple-minded TCP socket-based logging receiver suitable for test
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
741 purposes."""
27024
1098ccc84d63 SF #660795
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
742
1098ccc84d63 SF #660795
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
743 allow_reuse_address = 1
45623
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
744 log_output = ""
27024
1098ccc84d63 SF #660795
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
745
1098ccc84d63 SF #660795
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
746 def __init__(self, host='localhost',
1098ccc84d63 SF #660795
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
747 port=logging.handlers.DEFAULT_TCP_LOGGING_PORT,
1098ccc84d63 SF #660795
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
748 handler=LogRecordStreamHandler):
1098ccc84d63 SF #660795
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
749 ThreadingTCPServer.__init__(self, (host, port), handler)
45451
8f994d697747 #2165: fix test_logging failure on some machines.
Georg Brandl <georg@python.org>
parents: 45375
diff changeset
750 self.abort = False
45623
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
751 self.timeout = 0.1
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
752 self.finished = threading.Event()
27024
1098ccc84d63 SF #660795
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
753
1098ccc84d63 SF #660795
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
754 def serve_until_stopped(self):
36448
388a583e7a63 Get test_logging to not hang when running under regrtest.py -R ::
Neal Norwitz <nnorwitz@gmail.com>
parents: 36425
diff changeset
755 while not self.abort:
36843
1848d75bbdb3 Backout the last hack and add in this new one.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36825
diff changeset
756 rd, wr, ex = select.select([self.socket.fileno()], [], [],
1848d75bbdb3 Backout the last hack and add in this new one.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36825
diff changeset
757 self.timeout)
1848d75bbdb3 Backout the last hack and add in this new one.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36825
diff changeset
758 if rd:
1848d75bbdb3 Backout the last hack and add in this new one.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36825
diff changeset
759 self.handle_request()
45623
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
760 # Notify the main thread that we're about to exit
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
761 self.finished.set()
36423
29bfbb588d93 Explicitly close the server socket.
Martin v. Löwis <martin@v.loewis.de>
parents: 36350
diff changeset
762 # close the listen socket
29bfbb588d93 Explicitly close the server socket.
Martin v. Löwis <martin@v.loewis.de>
parents: 36350
diff changeset
763 self.server_close()
27024
1098ccc84d63 SF #660795
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
764
1098ccc84d63 SF #660795
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
765
45623
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
766 class SocketHandlerTest(BaseTest):
27024
1098ccc84d63 SF #660795
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
767
45623
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
768 """Test for SocketHandler objects."""
27024
1098ccc84d63 SF #660795
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
769
45623
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
770 def setUp(self):
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
771 """Set up a TCP server to receive log messages, and a SocketHandler
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
772 pointing to that server's address and port."""
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
773 BaseTest.setUp(self)
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
774 self.tcpserver = LogRecordSocketReceiver(port=0)
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
775 self.port = self.tcpserver.socket.getsockname()[1]
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
776 self.threads = [
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
777 threading.Thread(target=self.tcpserver.serve_until_stopped)]
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
778 for thread in self.threads:
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
779 thread.start()
27024
1098ccc84d63 SF #660795
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
780
45623
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
781 self.sock_hdlr = logging.handlers.SocketHandler('localhost', self.port)
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
782 self.sock_hdlr.setFormatter(self.root_formatter)
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
783 self.root_logger.removeHandler(self.root_logger.handlers[0])
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
784 self.root_logger.addHandler(self.sock_hdlr)
27024
1098ccc84d63 SF #660795
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
785
45623
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
786 def tearDown(self):
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
787 """Shutdown the TCP server."""
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
788 try:
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
789 self.tcpserver.abort = True
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
790 del self.tcpserver
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
791 self.root_logger.removeHandler(self.sock_hdlr)
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
792 self.sock_hdlr.close()
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
793 for thread in self.threads:
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
794 thread.join(2.0)
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
795 finally:
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
796 BaseTest.tearDown(self)
27024
1098ccc84d63 SF #660795
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
797
45623
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
798 def get_output(self):
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
799 """Get the log output as received by the TCP server."""
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
800 # Signal the TCP receiver and wait for it to terminate.
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
801 self.root_logger.critical(LogRecordStreamHandler.TCP_LOG_END)
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
802 self.tcpserver.finished.wait(2.0)
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
803 return self.tcpserver.log_output
27024
1098ccc84d63 SF #660795
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
804
45623
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
805 def test_output(self):
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
806 # The log message sent to the SocketHandler is properly received.
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
807 logger = logging.getLogger("tcp")
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
808 logger.error("spam")
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
809 logger.debug("eggs")
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
810 self.assertEquals(self.get_output(), "spam\neggs\n")
45375
6550659dbc9d Move test_logging over to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 41114
diff changeset
811
45451
8f994d697747 #2165: fix test_logging failure on some machines.
Georg Brandl <georg@python.org>
parents: 45375
diff changeset
812
45623
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
813 class MemoryTest(BaseTest):
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
814
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
815 """Test memory persistence of logger objects."""
45375
6550659dbc9d Move test_logging over to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 41114
diff changeset
816
45623
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
817 def setUp(self):
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
818 """Create a dict to remember potentially destroyed objects."""
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
819 BaseTest.setUp(self)
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
820 self._survivors = {}
45375
6550659dbc9d Move test_logging over to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 41114
diff changeset
821
45623
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
822 def _watch_for_survival(self, *args):
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
823 """Watch the given objects for survival, by creating weakrefs to
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
824 them."""
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
825 for obj in args:
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
826 key = id(obj), repr(obj)
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
827 self._survivors[key] = weakref.ref(obj)
45375
6550659dbc9d Move test_logging over to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 41114
diff changeset
828
45623
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
829 def _assert_survival(self):
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
830 """Assert that all objects watched for survival have survived."""
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
831 # Trigger cycle breaking.
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
832 gc.collect()
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
833 dead = []
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
834 for (id_, repr_), ref in self._survivors.items():
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
835 if ref() is None:
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
836 dead.append(repr_)
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
837 if dead:
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
838 self.fail("%d objects should have survived "
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
839 "but have been destroyed: %s" % (len(dead), ", ".join(dead)))
36307
beceb8e66b0b Added test for fileConfig. Contributed by Shane Hathaway.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 36163
diff changeset
840
45623
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
841 def test_persistent_loggers(self):
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
842 # Logger objects are persistent and retain their configuration, even
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
843 # if visible references are destroyed.
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
844 self.root_logger.setLevel(logging.INFO)
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
845 foo = logging.getLogger("foo")
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
846 self._watch_for_survival(foo)
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
847 foo.setLevel(logging.DEBUG)
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
848 self.root_logger.debug(self.next_message())
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
849 foo.debug(self.next_message())
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
850 self.assert_log_lines([
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
851 ('foo', 'DEBUG', '2'),
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
852 ])
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
853 del foo
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
854 # foo has survived.
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
855 self._assert_survival()
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
856 # foo has retained its settings.
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
857 bar = logging.getLogger("foo")
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
858 bar.debug(self.next_message())
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
859 self.assert_log_lines([
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
860 ('foo', 'DEBUG', '2'),
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
861 ('foo', 'DEBUG', '3'),
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
862 ])
36307
beceb8e66b0b Added test for fileConfig. Contributed by Shane Hathaway.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 36163
diff changeset
863
51474
e49013f1d831 Issue #5170: Fixed Unicode output bug in logging and added test case. This is a regression which did not occur in 2.5.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 49955
diff changeset
864
49009
833d3ecfbbf0 Issue #3772: Fixed regression problem in StreamHandler.emit().
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 48980
diff changeset
865 class EncodingTest(BaseTest):
833d3ecfbbf0 Issue #3772: Fixed regression problem in StreamHandler.emit().
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 48980
diff changeset
866 def test_encoding_plain_file(self):
833d3ecfbbf0 Issue #3772: Fixed regression problem in StreamHandler.emit().
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 48980
diff changeset
867 # In Python 2.x, a plain file object is treated as having no encoding.
833d3ecfbbf0 Issue #3772: Fixed regression problem in StreamHandler.emit().
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 48980
diff changeset
868 log = logging.getLogger("test")
833d3ecfbbf0 Issue #3772: Fixed regression problem in StreamHandler.emit().
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 48980
diff changeset
869 fn = tempfile.mktemp(".log")
833d3ecfbbf0 Issue #3772: Fixed regression problem in StreamHandler.emit().
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 48980
diff changeset
870 # the non-ascii data we write to the log.
833d3ecfbbf0 Issue #3772: Fixed regression problem in StreamHandler.emit().
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 48980
diff changeset
871 data = "foo\x80"
833d3ecfbbf0 Issue #3772: Fixed regression problem in StreamHandler.emit().
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 48980
diff changeset
872 try:
833d3ecfbbf0 Issue #3772: Fixed regression problem in StreamHandler.emit().
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 48980
diff changeset
873 handler = logging.FileHandler(fn)
833d3ecfbbf0 Issue #3772: Fixed regression problem in StreamHandler.emit().
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 48980
diff changeset
874 log.addHandler(handler)
833d3ecfbbf0 Issue #3772: Fixed regression problem in StreamHandler.emit().
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 48980
diff changeset
875 try:
833d3ecfbbf0 Issue #3772: Fixed regression problem in StreamHandler.emit().
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 48980
diff changeset
876 # write non-ascii data to the log.
833d3ecfbbf0 Issue #3772: Fixed regression problem in StreamHandler.emit().
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 48980
diff changeset
877 log.warning(data)
833d3ecfbbf0 Issue #3772: Fixed regression problem in StreamHandler.emit().
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 48980
diff changeset
878 finally:
833d3ecfbbf0 Issue #3772: Fixed regression problem in StreamHandler.emit().
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 48980
diff changeset
879 log.removeHandler(handler)
833d3ecfbbf0 Issue #3772: Fixed regression problem in StreamHandler.emit().
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 48980
diff changeset
880 handler.close()
833d3ecfbbf0 Issue #3772: Fixed regression problem in StreamHandler.emit().
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 48980
diff changeset
881 # check we wrote exactly those bytes, ignoring trailing \n etc
833d3ecfbbf0 Issue #3772: Fixed regression problem in StreamHandler.emit().
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 48980
diff changeset
882 f = open(fn)
833d3ecfbbf0 Issue #3772: Fixed regression problem in StreamHandler.emit().
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 48980
diff changeset
883 try:
833d3ecfbbf0 Issue #3772: Fixed regression problem in StreamHandler.emit().
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 48980
diff changeset
884 self.failUnlessEqual(f.read().rstrip(), data)
833d3ecfbbf0 Issue #3772: Fixed regression problem in StreamHandler.emit().
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 48980
diff changeset
885 finally:
833d3ecfbbf0 Issue #3772: Fixed regression problem in StreamHandler.emit().
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 48980
diff changeset
886 f.close()
833d3ecfbbf0 Issue #3772: Fixed regression problem in StreamHandler.emit().
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 48980
diff changeset
887 finally:
833d3ecfbbf0 Issue #3772: Fixed regression problem in StreamHandler.emit().
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 48980
diff changeset
888 if os.path.isfile(fn):
833d3ecfbbf0 Issue #3772: Fixed regression problem in StreamHandler.emit().
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 48980
diff changeset
889 os.remove(fn)
36307
beceb8e66b0b Added test for fileConfig. Contributed by Shane Hathaway.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 36163
diff changeset
890
51474
e49013f1d831 Issue #5170: Fixed Unicode output bug in logging and added test case. This is a regression which did not occur in 2.5.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 49955
diff changeset
891 def test_encoding_cyrillic_unicode(self):
e49013f1d831 Issue #5170: Fixed Unicode output bug in logging and added test case. This is a regression which did not occur in 2.5.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 49955
diff changeset
892 log = logging.getLogger("test")
e49013f1d831 Issue #5170: Fixed Unicode output bug in logging and added test case. This is a regression which did not occur in 2.5.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 49955
diff changeset
893 #Get a message in Unicode: Do svidanya in Cyrillic (meaning goodbye)
e49013f1d831 Issue #5170: Fixed Unicode output bug in logging and added test case. This is a regression which did not occur in 2.5.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 49955
diff changeset
894 message = u'\u0434\u043e \u0441\u0432\u0438\u0434\u0430\u043d\u0438\u044f'
e49013f1d831 Issue #5170: Fixed Unicode output bug in logging and added test case. This is a regression which did not occur in 2.5.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 49955
diff changeset
895 #Ensure it's written in a Cyrillic encoding
e49013f1d831 Issue #5170: Fixed Unicode output bug in logging and added test case. This is a regression which did not occur in 2.5.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 49955
diff changeset
896 writer_class = codecs.getwriter('cp1251')
53179
73bfcb398886 Issue #5768: Change to Unicode output logic and test case for same.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 52965
diff changeset
897 writer_class.encoding = 'cp1251'
51474
e49013f1d831 Issue #5170: Fixed Unicode output bug in logging and added test case. This is a regression which did not occur in 2.5.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 49955
diff changeset
898 stream = cStringIO.StringIO()
e49013f1d831 Issue #5170: Fixed Unicode output bug in logging and added test case. This is a regression which did not occur in 2.5.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 49955
diff changeset
899 writer = writer_class(stream, 'strict')
e49013f1d831 Issue #5170: Fixed Unicode output bug in logging and added test case. This is a regression which did not occur in 2.5.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 49955
diff changeset
900 handler = logging.StreamHandler(writer)
e49013f1d831 Issue #5170: Fixed Unicode output bug in logging and added test case. This is a regression which did not occur in 2.5.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 49955
diff changeset
901 log.addHandler(handler)
e49013f1d831 Issue #5170: Fixed Unicode output bug in logging and added test case. This is a regression which did not occur in 2.5.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 49955
diff changeset
902 try:
e49013f1d831 Issue #5170: Fixed Unicode output bug in logging and added test case. This is a regression which did not occur in 2.5.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 49955
diff changeset
903 log.warning(message)
e49013f1d831 Issue #5170: Fixed Unicode output bug in logging and added test case. This is a regression which did not occur in 2.5.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 49955
diff changeset
904 finally:
e49013f1d831 Issue #5170: Fixed Unicode output bug in logging and added test case. This is a regression which did not occur in 2.5.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 49955
diff changeset
905 log.removeHandler(handler)
e49013f1d831 Issue #5170: Fixed Unicode output bug in logging and added test case. This is a regression which did not occur in 2.5.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 49955
diff changeset
906 handler.close()
e49013f1d831 Issue #5170: Fixed Unicode output bug in logging and added test case. This is a regression which did not occur in 2.5.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 49955
diff changeset
907 # check we wrote exactly those bytes, ignoring trailing \n etc
e49013f1d831 Issue #5170: Fixed Unicode output bug in logging and added test case. This is a regression which did not occur in 2.5.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 49955
diff changeset
908 s = stream.getvalue()
e49013f1d831 Issue #5170: Fixed Unicode output bug in logging and added test case. This is a regression which did not occur in 2.5.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 49955
diff changeset
909 #Compare against what the data should be when encoded in CP-1251
e49013f1d831 Issue #5170: Fixed Unicode output bug in logging and added test case. This is a regression which did not occur in 2.5.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 49955
diff changeset
910 self.assertEqual(s, '\xe4\xee \xf1\xe2\xe8\xe4\xe0\xed\xe8\xff\n')
e49013f1d831 Issue #5170: Fixed Unicode output bug in logging and added test case. This is a regression which did not occur in 2.5.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 49955
diff changeset
911
e49013f1d831 Issue #5170: Fixed Unicode output bug in logging and added test case. This is a regression which did not occur in 2.5.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 49955
diff changeset
912
49955
878d40468c4e Issue #4384: Added logging integration with warnings module using captureWarnings(). This change includes a NullHandler which does nothing; it will be of use to library developers who want to avoid the "No handlers could be found for logger XXX" message which can appear if the library user doesn't configure logging.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 49109
diff changeset
913 class WarningsTest(BaseTest):
52666
b814b0c35347 test_logging was blindly clearing the warnings filter. This caused
Brett Cannon <bcannon@gmail.com>
parents: 51474
diff changeset
914
49955
878d40468c4e Issue #4384: Added logging integration with warnings module using captureWarnings(). This change includes a NullHandler which does nothing; it will be of use to library developers who want to avoid the "No handlers could be found for logger XXX" message which can appear if the library user doesn't configure logging.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 49109
diff changeset
915 def test_warnings(self):
52666
b814b0c35347 test_logging was blindly clearing the warnings filter. This caused
Brett Cannon <bcannon@gmail.com>
parents: 51474
diff changeset
916 with warnings.catch_warnings():
52867
f550e2463062 Issue #5695: Moved logging.captureWarnings() call inside with statement in WarningsTest.test_warnings.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 52666
diff changeset
917 logging.captureWarnings(True)
52666
b814b0c35347 test_logging was blindly clearing the warnings filter. This caused
Brett Cannon <bcannon@gmail.com>
parents: 51474
diff changeset
918 try:
52965
e23252a8ff59 Issue #5695: Minor tweak to improve the code as suggested by Brett Cannon and as implemented in the Py3K branch.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 52867
diff changeset
919 warnings.filterwarnings("always", category=UserWarning)
52666
b814b0c35347 test_logging was blindly clearing the warnings filter. This caused
Brett Cannon <bcannon@gmail.com>
parents: 51474
diff changeset
920 file = cStringIO.StringIO()
b814b0c35347 test_logging was blindly clearing the warnings filter. This caused
Brett Cannon <bcannon@gmail.com>
parents: 51474
diff changeset
921 h = logging.StreamHandler(file)
b814b0c35347 test_logging was blindly clearing the warnings filter. This caused
Brett Cannon <bcannon@gmail.com>
parents: 51474
diff changeset
922 logger = logging.getLogger("py.warnings")
b814b0c35347 test_logging was blindly clearing the warnings filter. This caused
Brett Cannon <bcannon@gmail.com>
parents: 51474
diff changeset
923 logger.addHandler(h)
b814b0c35347 test_logging was blindly clearing the warnings filter. This caused
Brett Cannon <bcannon@gmail.com>
parents: 51474
diff changeset
924 warnings.warn("I'm warning you...")
b814b0c35347 test_logging was blindly clearing the warnings filter. This caused
Brett Cannon <bcannon@gmail.com>
parents: 51474
diff changeset
925 logger.removeHandler(h)
b814b0c35347 test_logging was blindly clearing the warnings filter. This caused
Brett Cannon <bcannon@gmail.com>
parents: 51474
diff changeset
926 s = file.getvalue()
b814b0c35347 test_logging was blindly clearing the warnings filter. This caused
Brett Cannon <bcannon@gmail.com>
parents: 51474
diff changeset
927 h.close()
b814b0c35347 test_logging was blindly clearing the warnings filter. This caused
Brett Cannon <bcannon@gmail.com>
parents: 51474
diff changeset
928 self.assertTrue(s.find("UserWarning: I'm warning you...\n") > 0)
49955
878d40468c4e Issue #4384: Added logging integration with warnings module using captureWarnings(). This change includes a NullHandler which does nothing; it will be of use to library developers who want to avoid the "No handlers could be found for logger XXX" message which can appear if the library user doesn't configure logging.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 49109
diff changeset
929
52666
b814b0c35347 test_logging was blindly clearing the warnings filter. This caused
Brett Cannon <bcannon@gmail.com>
parents: 51474
diff changeset
930 #See if an explicit file uses the original implementation
b814b0c35347 test_logging was blindly clearing the warnings filter. This caused
Brett Cannon <bcannon@gmail.com>
parents: 51474
diff changeset
931 file = cStringIO.StringIO()
b814b0c35347 test_logging was blindly clearing the warnings filter. This caused
Brett Cannon <bcannon@gmail.com>
parents: 51474
diff changeset
932 warnings.showwarning("Explicit", UserWarning, "dummy.py", 42,
b814b0c35347 test_logging was blindly clearing the warnings filter. This caused
Brett Cannon <bcannon@gmail.com>
parents: 51474
diff changeset
933 file, "Dummy line")
b814b0c35347 test_logging was blindly clearing the warnings filter. This caused
Brett Cannon <bcannon@gmail.com>
parents: 51474
diff changeset
934 s = file.getvalue()
b814b0c35347 test_logging was blindly clearing the warnings filter. This caused
Brett Cannon <bcannon@gmail.com>
parents: 51474
diff changeset
935 file.close()
b814b0c35347 test_logging was blindly clearing the warnings filter. This caused
Brett Cannon <bcannon@gmail.com>
parents: 51474
diff changeset
936 self.assertEqual(s,
b814b0c35347 test_logging was blindly clearing the warnings filter. This caused
Brett Cannon <bcannon@gmail.com>
parents: 51474
diff changeset
937 "dummy.py:42: UserWarning: Explicit\n Dummy line\n")
b814b0c35347 test_logging was blindly clearing the warnings filter. This caused
Brett Cannon <bcannon@gmail.com>
parents: 51474
diff changeset
938 finally:
b814b0c35347 test_logging was blindly clearing the warnings filter. This caused
Brett Cannon <bcannon@gmail.com>
parents: 51474
diff changeset
939 logging.captureWarnings(False)
49955
878d40468c4e Issue #4384: Added logging integration with warnings module using captureWarnings(). This change includes a NullHandler which does nothing; it will be of use to library developers who want to avoid the "No handlers could be found for logger XXX" message which can appear if the library user doesn't configure logging.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 49109
diff changeset
940
45623
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
941 # Set the locale to the platform-dependent default. I have no idea
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
942 # why the test does this, but in any case we save the current locale
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
943 # first and restore it at the end.
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
944 @run_with_locale('LC_ALL', '')
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
945 def test_main():
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
946 run_unittest(BuiltinLevelsTest, BasicFilterTest,
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
947 CustomLevelsAndFiltersTest, MemoryHandlerTest,
49009
833d3ecfbbf0 Issue #3772: Fixed regression problem in StreamHandler.emit().
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 48980
diff changeset
948 ConfigFileTest, SocketHandlerTest, MemoryTest,
49955
878d40468c4e Issue #4384: Added logging integration with warnings module using captureWarnings(). This change includes a NullHandler which does nothing; it will be of use to library developers who want to avoid the "No handlers could be found for logger XXX" message which can appear if the library user doesn't configure logging.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 49109
diff changeset
949 EncodingTest, WarningsTest)
36307
beceb8e66b0b Added test for fileConfig. Contributed by Shane Hathaway.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 36163
diff changeset
950
45623
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
951 if __name__ == "__main__":
27024
1098ccc84d63 SF #660795
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
952 test_main()