annotate Lib/test/test_logging.py @ 58925:eecf1bef052d 2.6

Merged revisions 78207 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r78207 | ezio.melotti | 2010-02-17 01:26:09 +0200 (Wed, 17 Feb 2010) | 1 line #7930: fix stripid ........
author Ezio Melotti <ezio.melotti@gmail.com>
date Tue, 16 Feb 2010 23:29:44 +0000
parents cf190ff2a72c
children 9eabba6ded09
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 #
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
3 # Copyright 2001-2004 by Vinay Sajip. All Rights Reserved.
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
45623
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
21 Copyright (C) 2001-2002 Vinay Sajip. All Rights Reserved.
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
51476
4ea122973914 Fixed missing import.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 51475
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
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
48 import weakref
45375
6550659dbc9d Move test_logging over to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 41114
diff changeset
49
6550659dbc9d Move test_logging over to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 41114
diff changeset
50
45623
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
51 class BaseTest(unittest.TestCase):
45375
6550659dbc9d Move test_logging over to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 41114
diff changeset
52
45623
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
53 """Base class for logging tests."""
45375
6550659dbc9d Move test_logging over to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 41114
diff changeset
54
45623
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
55 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
56 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
57 message_num = 0
45375
6550659dbc9d Move test_logging over to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 41114
diff changeset
58
45623
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
59 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
60 """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
61 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
62 logger_dict = logging.getLogger().manager.loggerDict
45375
6550659dbc9d Move test_logging over to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 41114
diff changeset
63 logging._acquireLock()
6550659dbc9d Move test_logging over to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 41114
diff changeset
64 try:
45623
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
65 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
66 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
67 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
68 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
69 finally:
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
70 logging._releaseLock()
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
71
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
72 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
73 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
74
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
75 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
76 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
77 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
78 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
79 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
80 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
81
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
82 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
83 """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
84 level."""
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
85 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
86 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
87 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
88 logging._acquireLock()
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
89 try:
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
90 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
91 logging._levelNames.update(self.saved_level_names)
45375
6550659dbc9d Move test_logging over to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 41114
diff changeset
92 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
93 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
94 logging._handlerList[:] = self.saved_handler_list
45375
6550659dbc9d Move test_logging over to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 41114
diff changeset
95 loggerDict = logging.getLogger().manager.loggerDict
6550659dbc9d Move test_logging over to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 41114
diff changeset
96 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
97 loggerDict.update(self.saved_loggers)
45375
6550659dbc9d Move test_logging over to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 41114
diff changeset
98 finally:
6550659dbc9d Move test_logging over to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 41114
diff changeset
99 logging._releaseLock()
6550659dbc9d Move test_logging over to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 41114
diff changeset
100
45623
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
101 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
102 """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
103 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
104 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
105 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
106 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
107 try:
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
108 stream.reset()
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
109 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
110 except AttributeError:
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
111 # 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
112 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
113 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
114 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
115 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
116 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
117 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
118 actual)
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
119 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
120 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
121 if s:
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
122 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
123
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
124 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
125 """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
126 integer."""
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
127 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
128 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
129
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 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
132 """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
133
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
134 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
135 #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
136 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
137
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
138 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
139 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
140 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
141 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
142 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
143 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
144
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
145 # 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
146 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
147 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
148
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
149 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
150 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
151 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
152 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
153
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
154 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
155 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
156 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
157 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
158 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
159
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
160 # 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
161 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
162 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
163 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
164
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
165 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
166
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
167 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
168 ('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
169 ('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
170 ('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
171 ('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
172 ('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
173 ('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
174 ('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
175 ('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
176 ('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
177 ('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
178 ('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
179 ])
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 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
182 # 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
183 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
184
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
185 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
186 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
187 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
188 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
189
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
190 # 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
191 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
192 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
193
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
194 # 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
195 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
196 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
197 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
198
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
199 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
200 ('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
201 ('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
202 ])
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 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
205 #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
206 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
207
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
208 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
209 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
210 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
211 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
212 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
213 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
214 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
215
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
216 # 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
217 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
218 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
219 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
220 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
221 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
222 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
223
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
224 # 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
225 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
226 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
227 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
228 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
229
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
230 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
231 ('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
232 ('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
233 ('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
234 ('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
235 ('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
236 ('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
237 ])
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 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
240 # 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
241 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
242
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
243 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
244 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
245 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
246 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
247
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
248 # 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
249 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
250 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
251 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
252 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
253
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
254 # 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
255 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
256 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
257
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
258 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
259 ('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
260 ('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
261 ('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
262 ('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
263 ])
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 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
267
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
268 """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
269
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
270 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
271 # 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
272 # filter.
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
273 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
274 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
275 try:
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
276 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
277 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
278 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
279 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
280 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
281
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
282 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
283 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
284 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
285 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
286
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
287 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
288 ('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
289 ('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
290 ])
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
291 finally:
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
292 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
293
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 # 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
297 # 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
298 # 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
299 # 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
300 # 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
301 # logging system.
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
302 #
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
303 SILENT = 120
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
304 TACITURN = 119
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
305 TERSE = 118
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
306 EFFUSIVE = 117
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
307 SOCIABLE = 116
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
308 VERBOSE = 115
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
309 TALKATIVE = 114
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
310 GARRULOUS = 113
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
311 CHATTERBOX = 112
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
312 BORING = 111
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
313
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
314 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
315
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 # 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
318 # 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
319 #
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
320 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
321 SILENT : 'Silent',
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
322 TACITURN : 'Taciturn',
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
323 TERSE : 'Terse',
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
324 EFFUSIVE : 'Effusive',
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
325 SOCIABLE : 'Sociable',
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
326 VERBOSE : 'Verbose',
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
327 TALKATIVE : 'Talkative',
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
328 GARRULOUS : 'Garrulous',
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
329 CHATTERBOX : 'Chatterbox',
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
330 BORING : 'Boring',
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
331 }
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 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
334
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
335 """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
336
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
337 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
338 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
339
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
340 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
341
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
342 """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
343
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
344 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
345 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
346
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 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
349
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
350 """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
351
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
352 # 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
353 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
354
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
355 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
356 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
357 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
358 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
359
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
360 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
361 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
362 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
363
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
364 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
365 # 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
366 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
367 # 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
368 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
369 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
370 ('Verbose', '5'),
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
371 ('Sociable', '6'),
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
372 ('Effusive', '7'),
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
373 ('Terse', '8'),
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
374 ('Taciturn', '9'),
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
375 ('Silent', '10'),
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
376 ])
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 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
379 # 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
380 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
381 try:
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
382 # 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
383 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
384 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
385 ('Sociable', '6'),
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
386 ('Effusive', '7'),
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
387 ('Terse', '8'),
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
388 ('Taciturn', '9'),
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
389 ('Silent', '10'),
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
390 ])
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
391 finally:
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
392 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
393
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
394 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
395 # 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
396 # 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
397 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
398 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
399 garr = GarrulousFilter()
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
400 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
401 try:
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
402 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
403 first_lines = [
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
404 # 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
405 ('Boring', '1'),
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
406 ('Chatterbox', '2'),
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
407 ('Talkative', '4'),
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
408 ('Verbose', '5'),
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
409 ('Sociable', '6'),
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
410 ('Effusive', '7'),
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
411 ('Terse', '8'),
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
412 ('Taciturn', '9'),
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
413 ('Silent', '10'),
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
414 ]
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
415 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
416
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
417 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
418 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
419 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
420 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
421 # 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
422 # and 'Taciturn'
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
423 ('Boring', '11'),
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
424 ('Chatterbox', '12'),
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
425 ('Talkative', '14'),
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
426 ('Verbose', '15'),
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
427 ('Effusive', '17'),
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
428 ('Terse', '18'),
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
429 ('Silent', '20'),
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
430 ])
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
431 finally:
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
432 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
433 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
434 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
435
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 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
438
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
439 """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
440
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
441 # 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
442 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
443
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
444 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
445 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
446 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
447 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
448 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
449 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
450 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
451
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
452 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
453 self.mem_hdlr.close()
46183
c440a5737149 Correctly call the base class tearDown();
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 45623
diff changeset
454 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
455
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
456 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
457 # 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
458 # 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
459 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
460 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
461 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
462 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
463 # 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
464 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
465 lines = [
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
466 ('DEBUG', '1'),
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
467 ('INFO', '2'),
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
468 ('WARNING', '3'),
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
469 ]
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
470 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
471 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
472 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
473 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
474 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
475 # 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
476 # flush.
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
477 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
478 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
479 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
480
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
481 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
482 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
483
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 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
486 """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
487 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
488 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
489
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 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
492
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
493 """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
494
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
495 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
496
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
497 # 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
498 config0 = """
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
499 [loggers]
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
500 keys=root
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
501
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
502 [handlers]
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
503 keys=hand1
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
504
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
505 [formatters]
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
506 keys=form1
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
507
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
508 [logger_root]
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
509 level=WARNING
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
510 handlers=hand1
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
511
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
512 [handler_hand1]
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
513 class=StreamHandler
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
514 level=NOTSET
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
515 formatter=form1
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
516 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
517
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
518 [formatter_form1]
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
519 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
520 datefmt=
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
521 """
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 # 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
524 config1 = """
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
525 [loggers]
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
526 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
527
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
528 [handlers]
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
529 keys=hand1
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
530
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
531 [formatters]
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
532 keys=form1
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
533
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
534 [logger_root]
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
535 level=WARNING
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
536 handlers=
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
537
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
538 [logger_parser]
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
539 level=DEBUG
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
540 handlers=hand1
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
541 propagate=1
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
542 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
543
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
544 [handler_hand1]
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
545 class=StreamHandler
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
546 level=NOTSET
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
547 formatter=form1
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
548 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
549
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
550 [formatter_form1]
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
551 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
552 datefmt=
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
553 """
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 # 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
556 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
557
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
558 # 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
559 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
560
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
561 # 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
562 config4 = """
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
563 [loggers]
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
564 keys=root
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
565
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
566 [handlers]
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
567 keys=hand1
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
568
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
569 [formatters]
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
570 keys=form1
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
571
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
572 [logger_root]
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
573 level=NOTSET
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
574 handlers=hand1
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
575
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
576 [handler_hand1]
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
577 class=StreamHandler
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
578 level=NOTSET
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
579 formatter=form1
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
580 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
581
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
582 [formatter_form1]
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
583 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
584 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
585 datefmt=
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
586 """
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
587
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
588 # 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
589 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
590
48980
5fd39b84fc29 Issue #3726: Allowed spaces in separators in logging configuration files.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 48010
diff changeset
591 # 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
592 config6 = """
5fd39b84fc29 Issue #3726: Allowed spaces in separators in logging configuration files.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 48010
diff changeset
593 [loggers]
5fd39b84fc29 Issue #3726: Allowed spaces in separators in logging configuration files.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 48010
diff changeset
594 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
595
5fd39b84fc29 Issue #3726: Allowed spaces in separators in logging configuration files.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 48010
diff changeset
596 [handlers]
5fd39b84fc29 Issue #3726: Allowed spaces in separators in logging configuration files.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 48010
diff changeset
597 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
598
5fd39b84fc29 Issue #3726: Allowed spaces in separators in logging configuration files.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 48010
diff changeset
599 [formatters]
5fd39b84fc29 Issue #3726: Allowed spaces in separators in logging configuration files.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 48010
diff changeset
600 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
601
5fd39b84fc29 Issue #3726: Allowed spaces in separators in logging configuration files.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 48010
diff changeset
602 [logger_root]
5fd39b84fc29 Issue #3726: Allowed spaces in separators in logging configuration files.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 48010
diff changeset
603 level=WARNING
5fd39b84fc29 Issue #3726: Allowed spaces in separators in logging configuration files.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 48010
diff changeset
604 handlers=
5fd39b84fc29 Issue #3726: Allowed spaces in separators in logging configuration files.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 48010
diff changeset
605
5fd39b84fc29 Issue #3726: Allowed spaces in separators in logging configuration files.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 48010
diff changeset
606 [logger_parser]
5fd39b84fc29 Issue #3726: Allowed spaces in separators in logging configuration files.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 48010
diff changeset
607 level=DEBUG
5fd39b84fc29 Issue #3726: Allowed spaces in separators in logging configuration files.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 48010
diff changeset
608 handlers=hand1
5fd39b84fc29 Issue #3726: Allowed spaces in separators in logging configuration files.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 48010
diff changeset
609 propagate=1
5fd39b84fc29 Issue #3726: Allowed spaces in separators in logging configuration files.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 48010
diff changeset
610 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
611
5fd39b84fc29 Issue #3726: Allowed spaces in separators in logging configuration files.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 48010
diff changeset
612 [handler_hand1]
5fd39b84fc29 Issue #3726: Allowed spaces in separators in logging configuration files.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 48010
diff changeset
613 class=StreamHandler
5fd39b84fc29 Issue #3726: Allowed spaces in separators in logging configuration files.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 48010
diff changeset
614 level=NOTSET
5fd39b84fc29 Issue #3726: Allowed spaces in separators in logging configuration files.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 48010
diff changeset
615 formatter=form1
5fd39b84fc29 Issue #3726: Allowed spaces in separators in logging configuration files.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 48010
diff changeset
616 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
617
5fd39b84fc29 Issue #3726: Allowed spaces in separators in logging configuration files.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 48010
diff changeset
618 [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
619 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
620 level=NOTSET
5fd39b84fc29 Issue #3726: Allowed spaces in separators in logging configuration files.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 48010
diff changeset
621 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
622 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
623
5fd39b84fc29 Issue #3726: Allowed spaces in separators in logging configuration files.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 48010
diff changeset
624 [formatter_form1]
5fd39b84fc29 Issue #3726: Allowed spaces in separators in logging configuration files.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 48010
diff changeset
625 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
626 datefmt=
5fd39b84fc29 Issue #3726: Allowed spaces in separators in logging configuration files.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 48010
diff changeset
627
5fd39b84fc29 Issue #3726: Allowed spaces in separators in logging configuration files.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 48010
diff changeset
628 [formatter_form2]
5fd39b84fc29 Issue #3726: Allowed spaces in separators in logging configuration files.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 48010
diff changeset
629 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
630 datefmt=
5fd39b84fc29 Issue #3726: Allowed spaces in separators in logging configuration files.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 48010
diff changeset
631 """
5fd39b84fc29 Issue #3726: Allowed spaces in separators in logging configuration files.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 48010
diff changeset
632
45623
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
633 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
634 try:
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
635 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
636 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
637 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
638 f.close()
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
639 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
640 finally:
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
641 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
642
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
643 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
644 # 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
645 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
646 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
647 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
648 # 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
649 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
650 # 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
651 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
652 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
653 ('ERROR', '2'),
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
654 ], stream=output)
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
655 # 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
656 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
657
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
658 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
659 # 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
660 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
661 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
662 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
663 # 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
664 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
665 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
666 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
667 ('INFO', '1'),
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
668 ('ERROR', '2'),
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
669 ], stream=output)
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
670 # 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
671 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
672
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
673 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
674 # 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
675 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
676
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
677 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
678 # 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
679 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
680
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
681 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
682 # 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
683 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
684 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
685 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
686 try:
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
687 raise RuntimeError()
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
688 except RuntimeError:
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
689 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
690 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
691 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
692 "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
693 # 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
694 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
695
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
696 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
697 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
698
48980
5fd39b84fc29 Issue #3726: Allowed spaces in separators in logging configuration files.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 48010
diff changeset
699 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
700 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
701
45623
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
702 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
703
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
704 """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
705 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
706
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
707 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
708
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
709 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
710 """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
711 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
712 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
713 while True:
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
714 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
715 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
716 break
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
717 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
718 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
719 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
720 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
721 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
722 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
723 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
724
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
725 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
726 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
727
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
728 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
729 # 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
730 # terminate.
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
731 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
732 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
733 return
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
734 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
735
28824
0cd8d570c591 New version from Vinaj, should solve the threading problems (hopefully).
Guido van Rossum <guido@python.org>
parents: 27932
diff changeset
736
27024
1098ccc84d63 SF #660795
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
737 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
738
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
739 """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
740 purposes."""
27024
1098ccc84d63 SF #660795
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
741
1098ccc84d63 SF #660795
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
742 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
743 log_output = ""
27024
1098ccc84d63 SF #660795
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
744
1098ccc84d63 SF #660795
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
745 def __init__(self, host='localhost',
1098ccc84d63 SF #660795
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
746 port=logging.handlers.DEFAULT_TCP_LOGGING_PORT,
1098ccc84d63 SF #660795
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
747 handler=LogRecordStreamHandler):
1098ccc84d63 SF #660795
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
748 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
749 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
750 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
751 self.finished = threading.Event()
27024
1098ccc84d63 SF #660795
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
752
1098ccc84d63 SF #660795
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
753 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
754 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
755 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
756 self.timeout)
1848d75bbdb3 Backout the last hack and add in this new one.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36825
diff changeset
757 if rd:
1848d75bbdb3 Backout the last hack and add in this new one.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36825
diff changeset
758 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
759 # 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
760 self.finished.set()
36423
29bfbb588d93 Explicitly close the server socket.
Martin v. Löwis <martin@v.loewis.de>
parents: 36350
diff changeset
761 # close the listen socket
29bfbb588d93 Explicitly close the server socket.
Martin v. Löwis <martin@v.loewis.de>
parents: 36350
diff changeset
762 self.server_close()
27024
1098ccc84d63 SF #660795
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
763
1098ccc84d63 SF #660795
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
764
45623
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
765 class SocketHandlerTest(BaseTest):
27024
1098ccc84d63 SF #660795
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
766
45623
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
767 """Test for SocketHandler objects."""
27024
1098ccc84d63 SF #660795
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
768
45623
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
769 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
770 """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
771 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
772 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
773 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
774 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
775 self.threads = [
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
776 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
777 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
778 thread.start()
27024
1098ccc84d63 SF #660795
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
779
45623
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
780 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
781 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
782 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
783 self.root_logger.addHandler(self.sock_hdlr)
27024
1098ccc84d63 SF #660795
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
784
45623
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
785 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
786 """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
787 try:
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
788 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
789 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
790 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
791 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
792 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
793 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
794 finally:
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
795 BaseTest.tearDown(self)
27024
1098ccc84d63 SF #660795
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
796
45623
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
797 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
798 """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
799 # 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
800 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
801 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
802 return self.tcpserver.log_output
27024
1098ccc84d63 SF #660795
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
803
45623
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
804 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
805 # 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
806 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
807 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
808 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
809 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
810
45451
8f994d697747 #2165: fix test_logging failure on some machines.
Georg Brandl <georg@python.org>
parents: 45375
diff changeset
811
45623
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
812 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
813
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
814 """Test memory persistence of logger objects."""
45375
6550659dbc9d Move test_logging over to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 41114
diff changeset
815
45623
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
816 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
817 """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
818 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
819 self._survivors = {}
45375
6550659dbc9d Move test_logging over to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 41114
diff changeset
820
45623
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
821 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
822 """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
823 them."""
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
824 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
825 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
826 self._survivors[key] = weakref.ref(obj)
45375
6550659dbc9d Move test_logging over to doctest.
Brett Cannon <bcannon@gmail.com>
parents: 41114
diff changeset
827
45623
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
828 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
829 """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
830 # 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
831 gc.collect()
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
832 dead = []
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
833 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
834 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
835 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
836 if dead:
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
837 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
838 "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
839
45623
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
840 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
841 # 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
842 # 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
843 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
844 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
845 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
846 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
847 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
848 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
849 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
850 ('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
851 ])
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
852 del foo
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
853 # 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
854 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
855 # 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
856 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
857 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
858 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
859 ('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
860 ('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
861 ])
36307
beceb8e66b0b Added test for fileConfig. Contributed by Shane Hathaway.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 36163
diff changeset
862
51475
c17f9281a856 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: 49109
diff changeset
863
49009
833d3ecfbbf0 Issue #3772: Fixed regression problem in StreamHandler.emit().
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 48980
diff changeset
864 class EncodingTest(BaseTest):
833d3ecfbbf0 Issue #3772: Fixed regression problem in StreamHandler.emit().
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 48980
diff changeset
865 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
866 # 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
867 log = logging.getLogger("test")
833d3ecfbbf0 Issue #3772: Fixed regression problem in StreamHandler.emit().
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 48980
diff changeset
868 fn = tempfile.mktemp(".log")
833d3ecfbbf0 Issue #3772: Fixed regression problem in StreamHandler.emit().
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 48980
diff changeset
869 # 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
870 data = "foo\x80"
833d3ecfbbf0 Issue #3772: Fixed regression problem in StreamHandler.emit().
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 48980
diff changeset
871 try:
833d3ecfbbf0 Issue #3772: Fixed regression problem in StreamHandler.emit().
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 48980
diff changeset
872 handler = logging.FileHandler(fn)
833d3ecfbbf0 Issue #3772: Fixed regression problem in StreamHandler.emit().
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 48980
diff changeset
873 log.addHandler(handler)
833d3ecfbbf0 Issue #3772: Fixed regression problem in StreamHandler.emit().
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 48980
diff changeset
874 try:
833d3ecfbbf0 Issue #3772: Fixed regression problem in StreamHandler.emit().
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 48980
diff changeset
875 # 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
876 log.warning(data)
833d3ecfbbf0 Issue #3772: Fixed regression problem in StreamHandler.emit().
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 48980
diff changeset
877 finally:
833d3ecfbbf0 Issue #3772: Fixed regression problem in StreamHandler.emit().
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 48980
diff changeset
878 log.removeHandler(handler)
833d3ecfbbf0 Issue #3772: Fixed regression problem in StreamHandler.emit().
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 48980
diff changeset
879 handler.close()
833d3ecfbbf0 Issue #3772: Fixed regression problem in StreamHandler.emit().
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 48980
diff changeset
880 # 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
881 f = open(fn)
833d3ecfbbf0 Issue #3772: Fixed regression problem in StreamHandler.emit().
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 48980
diff changeset
882 try:
833d3ecfbbf0 Issue #3772: Fixed regression problem in StreamHandler.emit().
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 48980
diff changeset
883 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
884 finally:
833d3ecfbbf0 Issue #3772: Fixed regression problem in StreamHandler.emit().
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 48980
diff changeset
885 f.close()
833d3ecfbbf0 Issue #3772: Fixed regression problem in StreamHandler.emit().
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 48980
diff changeset
886 finally:
833d3ecfbbf0 Issue #3772: Fixed regression problem in StreamHandler.emit().
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 48980
diff changeset
887 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
888 os.remove(fn)
36307
beceb8e66b0b Added test for fileConfig. Contributed by Shane Hathaway.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 36163
diff changeset
889
51475
c17f9281a856 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: 49109
diff changeset
890 def test_encoding_cyrillic_unicode(self):
c17f9281a856 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: 49109
diff changeset
891 log = logging.getLogger("test")
c17f9281a856 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: 49109
diff changeset
892 #Get a message in Unicode: Do svidanya in Cyrillic (meaning goodbye)
c17f9281a856 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: 49109
diff changeset
893 message = u'\u0434\u043e \u0441\u0432\u0438\u0434\u0430\u043d\u0438\u044f'
c17f9281a856 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: 49109
diff changeset
894 #Ensure it's written in a Cyrillic encoding
c17f9281a856 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: 49109
diff changeset
895 writer_class = codecs.getwriter('cp1251')
53180
cf190ff2a72c Issue #5768: Change to Unicode output logic and test case for same.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 51476
diff changeset
896 writer_class.encoding = 'cp1251'
51475
c17f9281a856 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: 49109
diff changeset
897 stream = cStringIO.StringIO()
c17f9281a856 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: 49109
diff changeset
898 writer = writer_class(stream, 'strict')
c17f9281a856 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: 49109
diff changeset
899 handler = logging.StreamHandler(writer)
c17f9281a856 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: 49109
diff changeset
900 log.addHandler(handler)
c17f9281a856 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: 49109
diff changeset
901 try:
c17f9281a856 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: 49109
diff changeset
902 log.warning(message)
c17f9281a856 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: 49109
diff changeset
903 finally:
c17f9281a856 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: 49109
diff changeset
904 log.removeHandler(handler)
c17f9281a856 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: 49109
diff changeset
905 handler.close()
c17f9281a856 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: 49109
diff changeset
906 # check we wrote exactly those bytes, ignoring trailing \n etc
c17f9281a856 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: 49109
diff changeset
907 s = stream.getvalue()
c17f9281a856 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: 49109
diff changeset
908 #Compare against what the data should be when encoded in CP-1251
c17f9281a856 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: 49109
diff changeset
909 self.assertEqual(s, '\xe4\xee \xf1\xe2\xe8\xe4\xe0\xed\xe8\xff\n')
c17f9281a856 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: 49109
diff changeset
910
c17f9281a856 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: 49109
diff changeset
911
45623
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
912 # 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
913 # 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
914 # 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
915 @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
916 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
917 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
918 CustomLevelsAndFiltersTest, MemoryHandlerTest,
49009
833d3ecfbbf0 Issue #3772: Fixed regression problem in StreamHandler.emit().
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 48980
diff changeset
919 ConfigFileTest, SocketHandlerTest, MemoryTest,
833d3ecfbbf0 Issue #3772: Fixed regression problem in StreamHandler.emit().
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 48980
diff changeset
920 EncodingTest)
36307
beceb8e66b0b Added test for fileConfig. Contributed by Shane Hathaway.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 36163
diff changeset
921
45623
cf3a80f1da7d Refactor test_logging to use unittest. This should finally solve the flakiness
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
922 if __name__ == "__main__":
27024
1098ccc84d63 SF #660795
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
923 test_main()