[Kimchi-devel] [PATCH] [Wok 1/7] Create log_request() method for code simplification
Lucio Correia
luciojhc at linux.vnet.ibm.com
Wed Aug 31 18:10:10 UTC 2016
Signed-off-by: Lucio Correia <luciojhc at linux.vnet.ibm.com>
---
src/wok/control/base.py | 47 +++++++++++++++++------------------------------
src/wok/root.py | 41 ++++-------------------------------------
2 files changed, 21 insertions(+), 67 deletions(-)
diff --git a/src/wok/control/base.py b/src/wok/control/base.py
index 04cf2cb..f4443c8 100644
--- a/src/wok/control/base.py
+++ b/src/wok/control/base.py
@@ -43,6 +43,20 @@ RESOURCE_DEFAULT_LOG = "WOKRES0001L"
LOG_DISABLED_METHODS = ['GET']
+def log_request(code, params, exception, method, status, user=None):
+ log_id = RequestRecord(
+ {'code': code, 'params': params},
+ exception,
+ app=get_plugin_from_request(),
+ req=method,
+ status=status,
+ user=user or cherrypy.session.get(USER_NAME, 'N/A'),
+ ip=cherrypy.request.remote.ip
+ ).log()
+
+ return log_id
+
+
class Resource(object):
"""
A Resource represents a single entity in the API (such as a Virtual
@@ -157,16 +171,7 @@ class Resource(object):
# log request
code = self.getRequestMessage(method, action_name)
reqParams = utf8_dict(self.log_args, request)
- RequestRecord(
- reqParams,
- details,
- app=get_plugin_from_request(),
- msgCode=code,
- req=method,
- status=status,
- user=cherrypy.session.get(USER_NAME, 'N/A'),
- ip=cherrypy.request.remote.ip
- ).log()
+ log_request(code, reqParams, details, method, status)
wrapper.__name__ = action_name
wrapper.exposed = True
@@ -220,16 +225,7 @@ class Resource(object):
# log request
if method not in LOG_DISABLED_METHODS:
code = self.getRequestMessage(method)
- RequestRecord(
- self.log_args,
- details,
- app=get_plugin_from_request(),
- msgCode=code,
- req=method,
- status=status,
- user=cherrypy.session.get(USER_NAME, 'N/A'),
- ip=cherrypy.request.remote.ip
- ).log()
+ log_request(code, self.log_args, details, method, status)
return result
@@ -462,16 +458,7 @@ class Collection(object):
# log request
code = self.getRequestMessage(method)
reqParams = utf8_dict(self.log_args, params)
- RequestRecord(
- reqParams,
- details,
- app=get_plugin_from_request(),
- msgCode=code,
- req=method,
- status=status,
- user=cherrypy.session.get(USER_NAME, 'N/A'),
- ip=cherrypy.request.remote.ip
- ).log()
+ log_request(code, reqParams, details, method, status)
class AsyncCollection(Collection):
diff --git a/src/wok/root.py b/src/wok/root.py
index e54bf38..8f2f764 100644
--- a/src/wok/root.py
+++ b/src/wok/root.py
@@ -29,10 +29,9 @@ from wok import template
from wok.i18n import messages
from wok.config import paths as wok_paths
from wok.control import sub_nodes
-from wok.control.base import Resource
+from wok.control.base import log_request, Resource
from wok.control.utils import parse_request
from wok.exception import MissingParameter
-from wok.reqlogger import RequestRecord
ROOT_REQUESTS = {
@@ -156,8 +155,6 @@ class WokRoot(Root):
details = None
method = 'POST'
code = self.getRequestMessage(method, 'login')
- app = 'wok'
- ip = cherrypy.request.remote.ip
try:
params = parse_request()
@@ -165,18 +162,7 @@ class WokRoot(Root):
password = params['password']
except KeyError, item:
details = e = MissingParameter('WOKAUTH0003E', {'item': str(item)})
-
- RequestRecord(
- params,
- details,
- app=app,
- msgCode=code,
- req=method,
- status=400,
- user='N/A',
- ip=ip
- ).log()
-
+ log_request(code, params, details, method, 400)
raise cherrypy.HTTPError(400, e.message)
try:
@@ -186,16 +172,7 @@ class WokRoot(Root):
status = e.status
raise
finally:
- RequestRecord(
- params,
- details,
- app=app,
- msgCode=code,
- req=method,
- status=status,
- user='N/A',
- ip=ip
- ).log()
+ log_request(code, params, details, method, status)
return json.dumps(user_info)
@@ -204,19 +181,9 @@ class WokRoot(Root):
method = 'POST'
code = self.getRequestMessage(method, 'logout')
params = {'username': cherrypy.session.get(auth.USER_NAME, 'N/A')}
- ip = cherrypy.request.remote.ip
auth.logout()
- RequestRecord(
- params,
- None,
- app='wok',
- msgCode=code,
- req=method,
- status=200,
- user=params['username'],
- ip=ip
- ).log()
+ log_request(code, params, None, method, 200, user=params['username'])
return '{}'
--
1.9.1
More information about the Kimchi-devel
mailing list