From 21d14548c6c3799f36f0997a30a3853344dcc96b Mon Sep 17 00:00:00 2001 From: Pjotr Prins Date: Sat, 18 Jun 2016 12:14:39 +0000 Subject: Logging settings --- etc/default_settings.py | 2 +- wqflask/base/webqtlCaseData.py | 6 ++++-- wqflask/runserver.py | 6 ------ wqflask/utility/logger.py | 15 +++++++++------ wqflask/utility/tools.py | 25 ++++++++++++++++++++++++- 5 files changed, 38 insertions(+), 16 deletions(-) diff --git a/etc/default_settings.py b/etc/default_settings.py index bdcb9c5d..0fafb474 100644 --- a/etc/default_settings.py +++ b/etc/default_settings.py @@ -34,7 +34,7 @@ SERVER_PORT = 5003 SECRET_HMAC_CODE = '\x08\xdf\xfa\x93N\x80\xd9\\H@\\\x9f`\x98d^\xb4a;\xc6OM\x946a\xbc\xfc\x80:*\xebc' # Behavioural settings (defaults) note that logger and log levels can -# be overridden at the module level +# be overridden at the module level and with enviroment settings WEBSERVER_MODE = 'DEV' # Python webserver mode (DEBUG|DEV|PROD) LOG_LEVEL = 'WARNING' # Logger mode (DEBUG|INFO|WARNING|ERROR|CRITICAL) DEBUG_LOG_LEVEL = 1 # Debug log level (0-5) diff --git a/wqflask/base/webqtlCaseData.py b/wqflask/base/webqtlCaseData.py index 23f893fc..c80fcb65 100644 --- a/wqflask/base/webqtlCaseData.py +++ b/wqflask/base/webqtlCaseData.py @@ -26,7 +26,10 @@ from utility.logger import getLogger logger = getLogger(__name__) -logger.info("Mr. Mojo Risin 2") + +import utility.tools + +utility.tools.show_settings() class webqtlCaseData(object): """one case data in one trait""" @@ -72,4 +75,3 @@ class webqtlCaseData(object): return "%2.3f" % self.variance else: return "x" - diff --git a/wqflask/runserver.py b/wqflask/runserver.py index 0d91c57a..9339acf8 100644 --- a/wqflask/runserver.py +++ b/wqflask/runserver.py @@ -16,12 +16,6 @@ GREEN = '\033[92m' BOLD = '\033[1m' ENDC = '\033[0m' -# We always output this information on startup -print "runserver.py: ****** The webserver has the following configuration ******" -keylist = app.config.keys() -keylist.sort() -for k in keylist: - print("%s %s%s%s%s" % (k,BLUE,BOLD,app.config[k],ENDC)) logger.info("GN2 is running. Visit %shttp://localhost:5003/%s" % (BLUE,ENDC)) diff --git a/wqflask/utility/logger.py b/wqflask/utility/logger.py index 0549a7fa..479f6c46 100644 --- a/wqflask/utility/logger.py +++ b/wqflask/utility/logger.py @@ -13,6 +13,11 @@ # import utility.logger # logger = utility.logger.getLogger(__name__ ) # +# To override global behaviour set the LOG_LEVEL in default_settings.py +# or use an environment variable, e.g. +# +# env LOG_LEVEL=INFO ./bin/genenetwork2 +# # To override log level for a module replace that with, for example, # # import logging @@ -22,18 +27,16 @@ # We'll add more overrides soon. import logging - from utility.tools import LOG_LEVEL -print("Set global log level to "+LOG_LEVEL) - -log_level = getattr(logging, LOG_LEVEL.upper()) -logging.basicConfig(level=log_level) - # Get the module logger. You can override log levels at the # module level def getLogger(name, level = None): logger = logging.getLogger(name) if level: logger.setLevel(level) + else: + logger.setLevel(LOG_LEVEL) + + logger.debug("Log "+name+" at level "+logging.getLevelName(logger.getEffectiveLevel())) return logger diff --git a/wqflask/utility/tools.py b/wqflask/utility/tools.py index 1b31857e..bfb379f2 100644 --- a/wqflask/utility/tools.py +++ b/wqflask/utility/tools.py @@ -50,7 +50,7 @@ def get_setting(command_id,guess=None): command = value(guess) if command == None or command == "": raise Exception(command_id+' setting unknown or faulty (update default_settings.py?).') - logger.info("Set "+command_id+"="+str(command)) + logger.debug("Set "+command_id+"="+str(command)) return command def get_setting_bool(id): @@ -143,6 +143,29 @@ def locate_ignore_error(name, subdir=None): def tempdir(): return valid_path(get_setting("TEMPDIR","/tmp")) +BLUE = '\033[94m' +GREEN = '\033[92m' +BOLD = '\033[1m' +ENDC = '\033[0m' + +def show_settings(): + from utility.tools import LOG_LEVEL + + print("Set global log level to "+BLUE+LOG_LEVEL+ENDC) + log_level = getattr(logging, LOG_LEVEL.upper()) + logging.basicConfig(level=log_level) + + logger.info(BLUE+"Mr. Mojo Risin 2"+ENDC) + print "runserver.py: ****** The webserver has the following configuration ******" + keylist = app.config.keys() + keylist.sort() + for k in keylist: + try: + print("%s %s%s%s%s" % (k,BLUE,BOLD,get_setting(k),ENDC)) + except: + print("%s %s%s%s%s" % (k,GREEN,BOLD,app.config[k],ENDC)) + + # Cached values WEBSERVER_MODE = get_setting('WEBSERVER_MODE') LOG_LEVEL = get_setting('LOG_LEVEL') -- cgit v1.2.3