refine logging usage in weather module

This commit is contained in:
Brian S. Stephan 2018-01-09 10:25:11 -06:00
parent a226cfd020
commit 138a0cdc2a
1 changed files with 22 additions and 20 deletions

View File

@ -8,7 +8,7 @@ from django.conf import settings
wu_base_url = 'http://api.wunderground.com/api/{0:s}/'.format(settings.WEATHER_WEATHER_UNDERGROUND_API_KEY)
log = logging.getLogger('weather.lib')
logger = logging.getLogger(__name__)
def get_conditions_for_query(queryitems):
@ -19,26 +19,27 @@ def get_conditions_for_query(queryitems):
query = query.replace(' ', '_')
try:
logger.info("looking up conditions for {0:s}".format(query))
url = wu_base_url + ('{0:s}/q/{1:s}.json'.format('conditions', query))
log.debug("calling %s", url)
logger.debug("calling %s", url)
resp = requests.get(url)
condition_data = resp.json()
except IOError as e:
log.error("error while making conditions query")
log.exception(e)
logger.error("error while making conditions query")
logger.exception(e)
raise
# condition data is loaded. the rest of this is obviously specific to
# http://www.wunderground.com/weather/api/d/docs?d=data/conditions
log.debug(condition_data)
logger.debug(condition_data)
try:
# just see if we have current_observation data
current = condition_data['current_observation']
except KeyError as e:
# ok, try to see if the ambiguous results stuff will help
log.debug(e)
log.debug("potentially ambiguous results, checking")
logger.debug(e)
logger.debug("potentially ambiguous results, checking")
try:
results = condition_data['response']['results']
reply = "Multiple results, try one of the following zmw codes:"
@ -50,8 +51,8 @@ def get_conditions_for_query(queryitems):
return reply
except KeyError as e:
# now we really know something is wrong
log.error("error or bad query in conditions lookup")
log.exception(e)
logger.error("error or bad query in conditions lookup")
logger.exception(e)
return "No results."
else:
try:
@ -120,8 +121,8 @@ def get_conditions_for_query(queryitems):
return _prettify_weather_strings(reply.rstrip())
except KeyError as e:
log.error("error or unexpected results in conditions reply")
log.exception(e)
logger.error("error or unexpected results in conditions reply")
logger.exception(e)
return "Error parsing results."
@ -133,25 +134,26 @@ def get_forecast_for_query(queryitems):
query = query.replace(' ', '_')
try:
logger.info("looking up forecast for {0:s}".format(query))
url = wu_base_url + ('{0:s}/q/{1:s}.json'.format('forecast', query))
resp = requests.get(url)
forecast_data = resp.json()
except IOError as e:
log.error("error while making forecast query")
log.exception(e)
logger.error("error while making forecast query")
logger.exception(e)
raise
# forecast data is loaded. the rest of this is obviously specific to
# http://www.wunderground.com/weather/api/d/docs?d=data/forecast
log.debug(forecast_data)
logger.debug(forecast_data)
try:
# just see if we have forecast data
forecasts = forecast_data['forecast']['txt_forecast']
except KeyError as e:
# ok, try to see if the ambiguous results stuff will help
log.debug(e)
log.debug("potentially ambiguous results, checking")
logger.debug(e)
logger.debug("potentially ambiguous results, checking")
try:
results = forecast_data['response']['results']
reply = "Multiple results, try one of the following zmw codes:"
@ -165,8 +167,8 @@ def get_forecast_for_query(queryitems):
return reply
except KeyError as e:
# now we really know something is wrong
log.error("error or bad query in forecast lookup")
log.exception(e)
logger.error("error or bad query in forecast lookup")
logger.exception(e)
return "No results."
else:
try:
@ -177,8 +179,8 @@ def get_forecast_for_query(queryitems):
return _prettify_weather_strings(reply.rstrip())
except KeyError as e:
log.error("error or unexpected results in forecast reply")
log.exception(e)
logger.error("error or unexpected results in forecast reply")
logger.exception(e)
return "Error parsing results."