diff options
author | liangke <lokyse@163.com> | 2018-03-15 16:40:22 +0800 |
---|---|---|
committer | liangke <lokyse@163.com> | 2018-03-21 10:44:48 +0800 |
commit | 43caad57874925f46284cfaa8849d6d86a21a2bf (patch) | |
tree | d1447300ece42a4908b842ba1ce3a7e84c347245 /vio | |
parent | e71f617b40c067d8c25f822d0ceb99d5b2fa09ed (diff) |
Fix vio logging format
Update the logging format.
Add context middleware to set MDC attributes.
Change-Id: Ie6fc6848f38ebc74312b1831a5bb609f4060b406
Issue-ID: MULTICLOUD-151
Signed-off-by: liangke <lokyse@163.com>
Diffstat (limited to 'vio')
-rw-r--r-- | vio/requirements.txt | 2 | ||||
-rw-r--r-- | vio/vio/middleware.py | 59 | ||||
-rw-r--r-- | vio/vio/pub/config/config.py | 4 | ||||
-rw-r--r-- | vio/vio/pub/config/log.yml | 8 | ||||
-rw-r--r-- | vio/vio/settings.py | 1 |
5 files changed, 69 insertions, 5 deletions
diff --git a/vio/requirements.txt b/vio/requirements.txt index 9c2a4b4..32384a9 100644 --- a/vio/requirements.txt +++ b/vio/requirements.txt @@ -23,4 +23,4 @@ mock==2.0.0 unittest_xml_reporting==1.12.0 # for onap logging -onappylog>=1.0.5 +onappylog>=1.0.6 diff --git a/vio/vio/middleware.py b/vio/vio/middleware.py new file mode 100644 index 0000000..161562e --- /dev/null +++ b/vio/vio/middleware.py @@ -0,0 +1,59 @@ +# Copyright (c) 2017-2018 VMware, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at: +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + + +import uuid +from onaplogging.mdcContext import MDC +from vio.pub.config.config import SERVICE_NAME +from vio.pub.config.config import FORWARDED_FOR_FIELDS + + +class LogContextMiddleware(object): + + # the last IP behind multiple proxies, if no exist proxies + # get local host ip. + def _getLastIp(self, request): + + ip = "" + try: + for field in FORWARDED_FOR_FIELDS: + if field in request.META: + if ',' in request.META[field]: + parts = request.META[field].split(',') + ip = parts[-1].strip().split(":")[0] + else: + ip = request.META[field].split(":")[0] + + if ip == "": + ip = request.META.get("HTTP_HOST").split(":")[0] + + except Exception: + pass + + return ip + + def process_request(self, request): + + ReqeustID = request.META.get("HTTP_X_TRANSACTIONID", None) + if ReqeustID is None: + ReqeustID = uuid.uuid3(uuid.NAMESPACE_URL, SERVICE_NAME) + MDC.put("requestID", ReqeustID) + InovocationID = uuid.uuid3(uuid.NAMESPACE_DNS, SERVICE_NAME) + MDC.put("invocationID", InovocationID) + MDC.put("serviceName", SERVICE_NAME) + MDC.put("serviceIP", self._getLastIp(request)) + return None + + def process_response(self, request, response): + + MDC.clear() + return response diff --git a/vio/vio/pub/config/config.py b/vio/vio/pub/config/config.py index b434aef..47bc3b2 100644 --- a/vio/vio/pub/config/config.py +++ b/vio/vio/pub/config/config.py @@ -33,6 +33,10 @@ REDIS_HOST = '127.0.0.1' REDIS_PORT = '6379' REDIS_PASSWD = '' +# [MDC] +SERVICE_NAME = "multicloud-vio" +FORWARDED_FOR_FIELDS = ["HTTP_X_FORWARDED_FOR", "HTTP_X_FORWARDED_HOST", + "HTTP_X_FORWARDED_SERVER"] # [mysql] # DB_IP = '127.0.0.1' # DB_PORT = 3306 diff --git a/vio/vio/pub/config/log.yml b/vio/vio/pub/config/log.yml index 82eb814..5cced4f 100644 --- a/vio/vio/pub/config/log.yml +++ b/vio/vio/pub/config/log.yml @@ -12,14 +12,14 @@ handlers: class: "logging.handlers.RotatingFileHandler" filename: "/var/log/onap/multicloud/vio/vio.log" formatter: "mdcFormat" - maxBytes: 1024*1024*50 + maxBytes: 52428800 backupCount: 10 formatters: standard: - format: "%(asctime)s:[%(name)s]:[%(filename)s]-[%(lineno)d] [%(levelname)s]:%(message)s" + format: "%(asctime)s|||||%(name)s||%(thread)||%(funcName)s||%(levelname)s||%(message)s" mdcFormat: - format: "%(asctime)s:[%(name)s]:[%(filename)s]-[%(lineno)d] [%(levelname)s]:[%(mdc)s]: %(message)s" - mdcfmt: "{requestID}" + format: "%(asctime)s|||||%(name)s||%(thread)s||%(funcName)s||%(levelname)s||%(message)s||||%(mdc)s \t" + mdcfmt: "{requestID} {invocationID} {serviceName} {serviceIP}" datefmt: "%Y-%m-%d %H:%M:%S" (): onaplogging.mdcformatter.MDCFormatter diff --git a/vio/vio/settings.py b/vio/vio/settings.py index e82b523..42330b7 100644 --- a/vio/vio/settings.py +++ b/vio/vio/settings.py @@ -51,6 +51,7 @@ MIDDLEWARE_CLASSES = [ 'django.contrib.auth.middleware.SessionAuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware', + 'vio.middleware.LogContextMiddleware', ] ROOT_URLCONF = 'vio.urls' |