From 45bc57a19598c92fc4a64681bce629c1a74a03b8 Mon Sep 17 00:00:00 2001 From: Pjotr Prins Date: Sun, 19 Jun 2016 08:32:16 +0000 Subject: logger: adding functionality for SQL views.py: simplified code --- wqflask/utility/logger.py | 19 ++++++++++++++++++- wqflask/wqflask/views.py | 32 +++++++++++++++----------------- 2 files changed, 33 insertions(+), 18 deletions(-) (limited to 'wqflask') diff --git a/wqflask/utility/logger.py b/wqflask/utility/logger.py index 83b196a7..81202b05 100644 --- a/wqflask/utility/logger.py +++ b/wqflask/utility/logger.py @@ -31,28 +31,45 @@ import string from utility.tools import LOG_LEVEL class GNLogger: - """A stub for multiple parameter logging""" + """A logger class with some additional functionality, such as + multiple parameter logging, SQL logging, timing, colors, and lazy + functions. + + """ def __init__(self,name): self.logger = logging.getLogger(name) def setLevel(self,value): + """Set the undelying log level""" self.logger.setLevel(value) def debug(self,*args): + """Call logging.debug for multiple args""" self.collect(self.logger.debug,*args) def info(self,*args): + """Call logging.info for multiple args""" self.collect(self.logger.info,*args) def warning(self,*args): + """Call logging.warning for multiple args""" self.collect(self.logger.warning,*args) self.logger.warning(self.collect(*args)) def error(self,*args): + """Call logging.error for multiple args""" self.collect(self.logger.error,*args) + def debugf(self,*args): + """Call logging.debug for multiple args""" + self.collect(self.logger.debug,*args) + + def sql(self, sqlcommand, fun = None): + """Log SQL command, optionally invoking a timed fun""" + def collect(self,fun,*args): + """Collect arguments and use fun to output one by one""" for a in args: fun(a) diff --git a/wqflask/wqflask/views.py b/wqflask/wqflask/views.py index 95b273a7..88cce263 100644 --- a/wqflask/wqflask/views.py +++ b/wqflask/wqflask/views.py @@ -119,30 +119,28 @@ def search_page(): else: return render_template("data_sharing.html", **template_vars.__dict__) else: - key = "search_results:v1:" + json.dumps(request.args, sort_keys=True) logger.debug("key is:", pf(key)) + result = None if USE_REDIS: with Bench("Trying Redis cache"): + key = "search_results:v1:" + json.dumps(request.args, sort_keys=True) result = Redis.get(key) + if result: + logger.info("Redis cache hit on search results!") + result = pickle.loads(result) else: logger.info("Skipping Redis cache (USE_REDIS=False)") - result = None - if result: - logger.info("Redis cache hit on search results!") - logger.debug("USE_REDIS=",USE_REDIS) - with Bench("Loading results"): - result = pickle.loads(result) - else: - logger.info("calling search_results.SearchResultPage") - logger.info("request.args is", request.args) - the_search = search_results.SearchResultPage(request.args) - result = the_search.__dict__ - - logger.debugf("result: ", lambda: pf(result)) - if USE_REDIS: - Redis.set(key, pickle.dumps(result, pickle.HIGHEST_PROTOCOL)) - Redis.expire(key, 60*60) + logger.info("calling search_results.SearchResultPage") + logger.info("request.args is", request.args) + the_search = search_results.SearchResultPage(request.args) + result = the_search.__dict__ + + logger.debugf("result: ", lambda: pf(result)) + + if USE_REDIS: + Redis.set(key, pickle.dumps(result, pickle.HIGHEST_PROTOCOL)) + Redis.expire(key, 60*60) if result['search_term_exists']: return render_template("search_result_page.html", **result) -- cgit v1.2.3