Skip to content

Commit

Permalink
Better library logging
Browse files Browse the repository at this point in the history
  • Loading branch information
Madison Bahmer committed May 12, 2016
1 parent ced574a commit 3c8e615
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
10 changes: 10 additions & 0 deletions utils/scutils/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Set default logging handler to avoid "No handler found" warnings.
import logging
try: # Python 2.7+
from logging import NullHandler
except ImportError:
class NullHandler(logging.Handler):
def emit(self, record):
pass

logging.getLogger(__name__).addHandler(NullHandler())
10 changes: 6 additions & 4 deletions utils/scutils/settings_wrapper.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import importlib
import imp
import sys
import logging
log = logging.getLogger(__name__)


class SettingsWrapper(object):
Expand Down Expand Up @@ -46,12 +48,12 @@ def load_from_string(self, settings_string='', module_name='customsettings'):
mod = imp.new_module(module_name)
exec settings_string in mod.__dict__
except TypeError:
print "Could not import settings"
log.warning("Could not import settings")
self.my_settings = {}
try:
self.my_settings = self._convert_to_dict(mod)
except ImportError:
print "Settings unable to be loaded"
log.warning("Settings unable to be loaded")

return self.settings()

Expand All @@ -73,7 +75,7 @@ def _load_defaults(self, default='settings.py'):
settings = importlib.import_module(default)
self.my_settings = self._convert_to_dict(settings)
except ImportError:
print "No default settings found"
log.warning("No default settings found")

def _load_custom(self, settings_name='localsettings.py'):
'''
Expand All @@ -88,7 +90,7 @@ def _load_custom(self, settings_name='localsettings.py'):
settings = importlib.import_module(settings_name)
new_settings = self._convert_to_dict(settings)
except ImportError:
print "No override settings found"
log.info("No override settings found")

for key in new_settings:
if key in self.my_settings:
Expand Down
4 changes: 3 additions & 1 deletion utils/scutils/zookeeper_watcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import argparse
import sys
from time import sleep
import logging
log = logging.getLogger(__name__)


class ZookeeperWatcher():
Expand Down Expand Up @@ -103,7 +105,7 @@ def init_connections(self, no_init=False):
self.zoo_client = KazooClient(hosts=self.hosts)
self.zoo_client.start()
except Exception, e:
print "ZKWatcher Exception:", e
log.error("ZKWatcher Exception: " + e.message)
sleep(1)
continue

Expand Down

0 comments on commit 3c8e615

Please sign in to comment.