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