Skip to content

Commit

Permalink
Use null handler to avoid noise on stderr (issue #53)
Browse files Browse the repository at this point in the history
  • Loading branch information
xolox committed Nov 1, 2015
1 parent 65f5455 commit d7c5055
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions misc/notes/search-notes.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,24 @@
unicode_string = str
byte_string = bytes

# Compatibility with Python 2.6 which doesn't have logging.NullHandler.
try:
from logging import NullHandler
except ImportError:

# This class was copied from the Python standard library, specifically
# https://hg.python.org/cpython/file/771f28686022/Lib/logging/__init__.py#l1670
class NullHandler(logging.Handler):

def handle(self, record):
pass

def emit(self, record):
pass

def createLock(self):
self.lock = None

# Try to import the Levenshtein module, don't error out if it's not installed.
try:
import Levenshtein
Expand Down Expand Up @@ -110,6 +128,8 @@ def init_logging(self):
self.logger.setLevel(logging.INFO)
if all(map(os.isatty, (0, 1, 2))):
self.logger.addHandler(logging.StreamHandler(sys.stderr))
else:
self.logger.addHandler(NullHandler())

def parse_args(self):
"""Parse the command line arguments."""
Expand Down

0 comments on commit d7c5055

Please sign in to comment.