From 3f372ca3759bd7b0306d7d2aebc1dcacdd5a9915 Mon Sep 17 00:00:00 2001 From: Lucashsmello Date: Sun, 5 May 2019 13:58:44 -0300 Subject: [PATCH] Decent logging --- src/loghandler.py | 64 +++++++++++++++++++++++++++++++++++++++++++++++ src/webAPI.py | 52 +++++++++++++++----------------------- 2 files changed, 84 insertions(+), 32 deletions(-) create mode 100644 src/loghandler.py diff --git a/src/loghandler.py b/src/loghandler.py new file mode 100644 index 0000000..52c2003 --- /dev/null +++ b/src/loghandler.py @@ -0,0 +1,64 @@ +import logging +import logging.handlers +import flask +from flask_restful import Resource +import os +import zipfile +from io import BytesIO + +def zipdir(path, ziph): + # ziph is zipfile handle + for root, dirs, files in os.walk(path): + for f in files: + ziph.write(os.path.join(root, f),os.path.join('log',f)) + + +class LogResource(Resource): + def get(self): + global LOG_FILE,INSTALL_LOG_FILE,LOG_DIR + if('zipped' in flask.request.args): + mem_zip = BytesIO() + with zipfile.ZipFile(mem_zip, 'w', zipfile.ZIP_DEFLATED) as zf: + zipdir(LOG_DIR,zf) + mem_zip.seek(0) + R=flask.send_file(mem_zip, as_attachment=True, attachment_filename='gimxweb-log.zip',mimetype='application/zip',cache_timeout=4) + R.direct_passthrough=False + return R + with open(LOG_FILE,'r') as f: + d1=f.read() + with open(INSTALL_LOG_FILE,'r') as f: + d2=f.read() + return {'web':d1, 'install':d2} + + +def configureLogger(log_dir,app): + global LOG_FILE, INSTALL_LOG_FILE, LOGGER, INSTALL_LOGGER,LOG_DIR + if(not os.path.isdir(log_dir)): + os.mkdir(log_dir) + LOG_DIR=log_dir + LOG_FILE=os.path.join(log_dir,"gimx_webapi.log") + INSTALL_LOG_FILE=os.path.join(log_dir,"gimx_webapi_install.log") + app.logger.setLevel(logging.INFO) + formatter = logging.Formatter('%(asctime)s\t%(name)s\t%(levelname)s\t%(message)s', "%Y-%m-%d %H:%M:%S") + # create logger + LOGGER = logging.getLogger('gimx_webapi') + LOGGER.setLevel(logging.DEBUG) + h = logging.handlers.RotatingFileHandler(LOG_FILE,maxBytes=200000,backupCount=1) + h.setLevel(logging.INFO) + h.setFormatter(formatter) + LOGGER.addHandler(h) + + app.logger.handlers=[h] + + h = logging.StreamHandler() + h.setLevel(logging.INFO) + h.setFormatter(logging.Formatter('%(levelname)s: %(message)s')) + LOGGER.addHandler(h) + + + INSTALL_LOGGER = logging.getLogger('gimx_webapi_install') + INSTALL_LOGGER.setLevel(logging.DEBUG) + h = logging.handlers.RotatingFileHandler(INSTALL_LOG_FILE,maxBytes=200000,backupCount=1) + h.setLevel(logging.INFO) + h.setFormatter(formatter) + INSTALL_LOGGER.addHandler(h) diff --git a/src/webAPI.py b/src/webAPI.py index b5cc6a1..78899a1 100644 --- a/src/webAPI.py +++ b/src/webAPI.py @@ -12,12 +12,11 @@ from time import sleep from sys import argv import subprocess -import logging -import logging.handlers import gevent from gevent.pywsgi import WSGIServer from gevent import monkey; monkey.patch_all() import gevent_sse +from loghandler import configureLogger, LogResource CHANNEL = gevent_sse.Channel(history_size=1) app = Flask(__name__) @@ -25,6 +24,7 @@ APPDATA_DIR=os.path.expanduser('~')+"/.gimx-web" LAST_OPTS_FILE=os.path.join(APPDATA_DIR,"last_opts") +LOG_DIR=os.path.join(APPDATA_DIR,"log") GIMX_API_VERSION=1 with open('../version.txt','r') as f: VERSION=f.read().strip('\n').strip() @@ -33,27 +33,6 @@ #UPLOAD_FOLDER=os.path.join(APPDATA_DIR,"uploads") #app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER -def configureLogger(): - global LOGGER,app - # create logger - LOGGER = logging.getLogger('gimx_webapi') - LOGGER.setLevel(logging.DEBUG) - - h = logging.handlers.RotatingFileHandler('../log/gimx_webapi.log',maxBytes=250000,backupCount=1) - h.setLevel(logging.INFO) - - # create formatter - formatter = logging.Formatter('%(asctime)s\t%(name)s\t%(levelname)s\t%(message)s', "%Y-%m-%d %H:%M:%S") - h.setFormatter(formatter) - LOGGER.addHandler(h) - app.logger.setLevel(logging.INFO) - app.logger.handlers=[h] - - h = logging.StreamHandler() - h.setLevel(logging.INFO) - h.setFormatter(logging.Formatter('%(levelname)s: %(message)s')) - LOGGER.addHandler(h) -''' @app.before_request def pre_request_logging(): if(len(request.values)==0): @@ -68,9 +47,17 @@ def pre_request_logging(): @app.after_request def after_request_logging(response): - app.logger.info(('%s response: ' % str(request.url_rule)) + response.status + ', ' + response.data.decode('utf-8')) + url_rule=str(request.url_rule) + if('streamStatus' in url_rule or 'zip' in response.mimetype): + msg='%s response: %s' % (url_rule,str(response.status)) + else: + data=str(response.data.decode('utf-8')) + if(len(data)>100): + data=data[:100]+'...' + msg='%s response: %s, %s' % (url_rule,str(response.status),data) + app.logger.info(msg) return response -''' + def getLastUsedOptions(): opts=None try: @@ -149,13 +136,11 @@ def get(self): return ret def handleGimxStart(opts,wait_sec=None): - global APPDATA_DIR,LAST_OPTS_FILE + global LAST_OPTS_FILE if(isGimxInitialized()): return 1 if(startGimx(opts.split(),wait_sec)==False): return 2 - if(not os.path.isdir(APPDATA_DIR)): - os.mkdir(APPDATA_DIR) try: with open(LAST_OPTS_FILE,'w') as f: f.write(opts) @@ -254,7 +239,7 @@ def get(self, name=None): return {'conf_files':xmllist} with open(os.path.join(self.confdir,name),'rb') as f: data=io.BytesIO(f.read()) - return flask.send_file(data, as_attachment=True, attachment_filename=name) + return flask.send_file(data, as_attachment=True, attachment_filename=name,cache_timeout=4) def post(self): """ @@ -325,13 +310,13 @@ def post(self): f.save(fout_path) cmd.append(fout_path) - subprocess.check_call(cmd+['../../'], cwd='../auto_updater/') + install_out=subprocess.check_output(cmd+['../../'], cwd='../auto_updater/') + INSTALL_LOGGER.info(install_out) except subprocess.CalledProcessError as e: LOGGER.error(str(e)) return {'return_code':e.returncode} return {'return_code':0} - class Configurator(Resource): def get(self): config_params=GetConfigurationParameters() @@ -380,7 +365,9 @@ def GimxAddResource(api,res,route1,route2=None): api.add_resource(res,R1,R2) if __name__=="__main__": - configureLogger() + if(not os.path.isdir(APPDATA_DIR)): + os.mkdir(APPDATA_DIR) + configureLogger(LOG_DIR,app) LOGGER.info("version %s" % VERSION) port=80 if('-p' in argv): @@ -392,6 +379,7 @@ def GimxAddResource(api,res,route1,route2=None): GimxAddResource(api,CheckVersion,'version') GimxAddResource(api,Updater,'update') GimxAddResource(api,Configurator,'curconfig') + GimxAddResource(api,LogResource,'log') app.debug = False #psutil.net_if_addrs()