Skip to content

Commit

Permalink
Decent logging
Browse files Browse the repository at this point in the history
  • Loading branch information
Lucashsmello committed May 5, 2019
1 parent 94bccba commit 3f372ca
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 32 deletions.
64 changes: 64 additions & 0 deletions src/loghandler.py
Original file line number Diff line number Diff line change
@@ -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)
52 changes: 20 additions & 32 deletions src/webAPI.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,19 @@
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__)
api = flask_restful.Api(app)

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()
Expand All @@ -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):
Expand All @@ -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:
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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):
"""
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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):
Expand All @@ -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()
Expand Down

0 comments on commit 3f372ca

Please sign in to comment.