Commit c6688cba by Heechul Kim

log api added

parent f03ea50e
import os
import re
import ast
import etcd
import uuid
import logging
import logging.config
from datetime import datetime
from porch.database import etcdc
from porch.config import OS_LOG
from porch.config import APP_LOG
logging.config.fileConfig('logging.conf')
log = logging.getLogger('porch')
def get_log(type):
"""Get log list.
"""
d_log = dict()
if not type:
return d_log
s_rsc = '{}/log/{}'.format(etcdc.prefix, type)
try:
r = etcdc.read(s_rsc, recursive=True, sorted=True)
except etcd.EtcdKeyNotFound as e:
log.error(e)
else:
for child in r.children:
if child.value is not None:
logname = os.path.basename(child.key)
d_log[logname] = dict()
d = ast.literal_eval(child.value)
if type == 'os':
d_res = _parse_log(type, d['log'])
elif type == 'app':
d_res = d
elif type == 'file':
d_res = d
if not len(d_res):
continue
d_log[logname] = d_res
finally:
return d_log
def _parse_log(type, s_logfile):
"""Parse logfile."""
d_res = dict()
try:
with open(s_logfile, 'r') as f:
for line in f:
m = re.match(r".*\s-\s(.*)\sTASKEND\s(\w+)\s.*", line)
if m is not None:
if m.group(1) not in d_res:
d_res[m.group(1)] = list()
d_res[m.group(1)].append(m.group(2))
except Exception as e:
log.error(e)
finally:
return d_res
def read_log(type, name):
"""Read app log
type: string, 'app' - currently only 'app' is supported.
name: string, logname (date isoformat)
"""
s_log = ''
s_rsc = '{}/log/{}/{}'.format(etcdc.prefix, type, name)
try:
r = etcdc.read(s_rsc)
except etcd.EtcdKeyNotFound as e:
log.error(e)
else:
d = ast.literal_eval(r.value)
try:
with open(d['log'], 'r') as f:
s_log = f.read()
except Exception as e:
log.error(e)
finally:
return s_log
def delete_log(type, name):
"""Delete log.
"""
if not name: # <name> should be specified.
return (False, 'name should be specified.')
# Get log file.
s_rsc = '{}/log/{}/{}'.format(etcdc.prefix, type, name)
try:
r = etcdc.read(s_rsc)
except etcd.EtcdKeyNotFound as e:
log.error(e)
return (False, e)
else:
d = ast.literal_eval(r.value)
try:
os.unlink(d['log'])
except:
pass
try:
etcdc.delete(s_rsc)
except:
pass
return (True, 'log {} is deleted.'.format(name))
import logging
import logging.config
from flask import request
from flask import abort
from flask_restplus import Resource
from flask_jwt_extended import jwt_required
from flask_jwt_extended import create_access_token
from flask_jwt_extended import get_jwt_identity
from porch.api.restplus import api
from porch.decorators import authz_required
from porch.api.log.bizlogic import get_log
from porch.api.log.bizlogic import read_log
from porch.api.log.bizlogic import delete_log
log = logging.getLogger('porch')
ns = api.namespace('log', description="Operations for log")
@ns.route('/<string:type>')
class LogCollection(Resource):
@authz_required('user')
def get(self, type):
"""Returns list of log."""
d_log = get_log(type)
return d_log
@ns.route('/<string:type>/<string:name>')
@ns.response(404, 'Log not found.')
class LogItem(Resource):
@authz_required('user')
def get(self, type, name):
"""Returns the log information."""
i_ret_code = 200
s_log = read_log(type, name)
return s_log
@ns.response(204, "The log is successfully deleted.")
@authz_required('admin')
def delete(self, type, name):
"""Deletes the log."""
(b_ret, s_msg) = delete_log(type, name)
if not b_ret:
d_msg = {'error': s_msg}
return d_msg, 404
return None, 204
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment