[Kimchi-devel] [PATCH] [Wok 1/6] Create log_request() method for code simplification

Lucio Correia luciojhc at linux.vnet.ibm.com
Fri Sep 2 21:52:19 UTC 2016


Signed-off-by: Lucio Correia <luciojhc at linux.vnet.ibm.com>
---
 src/wok/control/base.py  | 37 +++++--------------------------------
 src/wok/control/utils.py |  1 -
 src/wok/reqlogger.py     | 26 ++++++++++++++++++++++++++
 src/wok/root.py          | 40 ++++------------------------------------
 4 files changed, 35 insertions(+), 69 deletions(-)

diff --git a/src/wok/control/base.py b/src/wok/control/base.py
index 04cf2cb..686a1aa 100644
--- a/src/wok/control/base.py
+++ b/src/wok/control/base.py
@@ -31,9 +31,9 @@ from wok.control.utils import get_class_name, internal_redirect, model_fn
 from wok.control.utils import parse_request, validate_method
 from wok.control.utils import validate_params
 from wok.exception import InvalidOperation, UnauthorizedError, WokException
-from wok.reqlogger import RequestRecord
+from wok.reqlogger import log_request
 from wok.stringutils import encode_value, utf8_dict
-from wok.utils import get_plugin_from_request, wok_log
+from wok.utils import wok_log
 
 
 # Default request log messages
@@ -157,16 +157,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 +211,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 +444,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/control/utils.py b/src/wok/control/utils.py
index 059c187..987dd00 100644
--- a/src/wok/control/utils.py
+++ b/src/wok/control/utils.py
@@ -24,7 +24,6 @@ import cherrypy
 import json
 from jsonschema import Draft3Validator, ValidationError, FormatChecker
 
-
 from wok.auth import USER_ROLES
 from wok.exception import InvalidParameter, OperationFailed
 from wok.utils import import_module, listPathModules
diff --git a/src/wok/reqlogger.py b/src/wok/reqlogger.py
index 9f1d2c8..b675478 100644
--- a/src/wok/reqlogger.py
+++ b/src/wok/reqlogger.py
@@ -18,6 +18,7 @@
 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA
 #
 
+import cherrypy
 import glob
 import json
 import logging
@@ -28,6 +29,7 @@ import time
 from cherrypy.process.plugins import BackgroundTask
 from tempfile import NamedTemporaryFile
 
+from wok.auth import USER_NAME
 from wok.config import config, get_log_download_path
 from wok.exception import InvalidParameter, OperationFailed
 from wok.message import WokMessage
@@ -63,6 +65,30 @@ REQUEST_LOG_FILE = "wok-req.log"
 WOK_REQUEST_LOGGER = "wok_request_logger"
 
 
+def log_request(code, params, exception, method, status, app=None, user=None,
+                ip=None):
+    if app is None:
+        app = cherrypy.request.app.root.domain
+
+    if user is None:
+        user = cherrypy.session.get(USER_NAME, 'N/A')
+
+    if ip is None:
+        ip = cherrypy.request.remote.ip
+
+    log_id = RequestRecord(
+        {'code': code, 'params': params},
+        exception,
+        app=app,
+        req=method,
+        status=status,
+        user=user,
+        ip=ip
+    ).log()
+
+    return log_id
+
+
 class RequestLogger(object):
     def __init__(self):
         log = os.path.join(config.get("logging", "log_dir"), REQUEST_LOG_FILE)
diff --git a/src/wok/root.py b/src/wok/root.py
index e54bf38..080b7f0 100644
--- a/src/wok/root.py
+++ b/src/wok/root.py
@@ -32,7 +32,7 @@ from wok.control import sub_nodes
 from wok.control.base import Resource
 from wok.control.utils import parse_request
 from wok.exception import MissingParameter
-from wok.reqlogger import RequestRecord
+from wok.reqlogger import log_request
 
 
 ROOT_REQUESTS = {
@@ -156,8 +156,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 +163,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 +173,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 +182,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