Skip to content

Commit d7c5055

Browse files
committed
Use null handler to avoid noise on stderr (issue #53)
1 parent 65f5455 commit d7c5055

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

misc/notes/search-notes.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,24 @@
6767
unicode_string = str
6868
byte_string = bytes
6969

70+
# Compatibility with Python 2.6 which doesn't have logging.NullHandler.
71+
try:
72+
from logging import NullHandler
73+
except ImportError:
74+
75+
# This class was copied from the Python standard library, specifically
76+
# https://hg.python.org/cpython/file/771f28686022/Lib/logging/__init__.py#l1670
77+
class NullHandler(logging.Handler):
78+
79+
def handle(self, record):
80+
pass
81+
82+
def emit(self, record):
83+
pass
84+
85+
def createLock(self):
86+
self.lock = None
87+
7088
# Try to import the Levenshtein module, don't error out if it's not installed.
7189
try:
7290
import Levenshtein
@@ -110,6 +128,8 @@ def init_logging(self):
110128
self.logger.setLevel(logging.INFO)
111129
if all(map(os.isatty, (0, 1, 2))):
112130
self.logger.addHandler(logging.StreamHandler(sys.stderr))
131+
else:
132+
self.logger.addHandler(NullHandler())
113133

114134
def parse_args(self):
115135
"""Parse the command line arguments."""

0 commit comments

Comments
 (0)