From 71dd1a02940c70bb669f2dbcac2dcfe2e14546e0 Mon Sep 17 00:00:00 2001 From: "ying.yunlong" Date: Tue, 27 Mar 2018 10:41:48 +0800 Subject: Add vfc-nslcm log integration config Change-Id: I3b78eed809916415833aaef03d68c6ba597f64e1 Issue-ID: VFC-839 Signed-off-by: ying.yunlong --- lcm/log.yml | 50 +++++++++++++++++++++++++++++++ lcm/middleware.py | 60 +++++++++++++++++++++++++++++++++++++ lcm/pub/config/config.py | 5 ++++ lcm/settings.py | 78 ++++++++++++++++++++++++++---------------------- requirements.txt | 3 ++ 5 files changed, 161 insertions(+), 35 deletions(-) create mode 100644 lcm/log.yml create mode 100644 lcm/middleware.py diff --git a/lcm/log.yml b/lcm/log.yml new file mode 100644 index 00000000..4ae7ab16 --- /dev/null +++ b/lcm/log.yml @@ -0,0 +1,50 @@ +version: 1 +disable_existing_loggers: False + +loggers: + nslcm: + handlers: [nslcmlocal_handler, nslcm_handler] + level: "DEBUG" + propagate: False + django: + handlers: [django_handler] + level: "DEBUG" + propagate: False +handlers: + nslcmlocal_handler: + level: "DEBUG" + class: + "logging.handlers.RotatingFileHandler" + filename: "logs/runtime_nslcm.log" + formatter: + "standard" + maxBytes: 52428800 + backupCount: 10 + nslcm_handler: + level: "DEBUG" + class: + "logging.handlers.RotatingFileHandler" + filename: "/var/log/onap/vfc/nslcm/runtime_nslcm.log" + formatter: + "mdcFormat" + maxBytes: 52428800 + backupCount: 10 + django_handler: + level: "DEBUG" + class: + "logging.handlers.RotatingFileHandler" + filename: "logs/django.log" + formatter: + "standard" + maxBytes: 52428800 + backupCount: 10 +formatters: + standard: + format: + "%(asctime)s:[%(name)s]:[%(filename)s]-[%(lineno)d] [%(levelname)s]:%(message)s" + mdcFormat: + 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/lcm/middleware.py b/lcm/middleware.py new file mode 100644 index 00000000..7bf6868f --- /dev/null +++ b/lcm/middleware.py @@ -0,0 +1,60 @@ +# Copyright (c) 2017-2018 ZTE, 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 lcm.pub.config.config import FORWARDED_FOR_FIELDS, SERVICE_NAME + + +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): + # Fetch TRANSACTIONID Id and pass to plugin server + ReqeustID = request.META.get("HTTP_X_TRANSACTIONID", None) + if ReqeustID is None: + ReqeustID = uuid.uuid3(uuid.NAMESPACE_URL, SERVICE_NAME) + request.META["HTTP_X_TRANSACTIONID"] = ReqeustID + MDC.put("requestID", ReqeustID) + # generate the unique id + InovocationID = uuid.uuid3(uuid.NAMESPACE_DNS, SERVICE_NAME) + MDC.put("invocationID", InovocationID) + MDC.put("serviceName", SERVICE_NAME) + # access ip + MDC.put("serviceIP", self._getLastIp(request)) + + return None + + def process_response(self, request, response): + MDC.clear() + return response diff --git a/lcm/pub/config/config.py b/lcm/pub/config/config.py index a7213400..b9afbe64 100644 --- a/lcm/pub/config/config.py +++ b/lcm/pub/config/config.py @@ -33,6 +33,11 @@ DB_NAME = "vfcnfvolcm" DB_USER = "vfcnfvolcm" DB_PASSWD = "vfcnfvolcm" +# [MDC] +SERVICE_NAME = "nslcm" +FORWARDED_FOR_FIELDS = ["HTTP_X_FORWARDED_FOR", "HTTP_X_FORWARDED_HOST", + "HTTP_X_FORWARDED_SERVER"] + # [register] REG_TO_MSB_WHEN_START = True REG_TO_MSB_REG_URL = "/api/microservices/v1/services" diff --git a/lcm/settings.py b/lcm/settings.py index 37c923cd..e5f680bb 100644 --- a/lcm/settings.py +++ b/lcm/settings.py @@ -19,7 +19,10 @@ import redisco from lcm.pub.config.config import REDIS_HOST, REDIS_PORT, REDIS_PASSWD from lcm.pub.config.config import DB_NAME, DB_IP, DB_USER, DB_PASSWD, DB_PORT -from lcm.pub.config import config +from lcm.pub.config import config as pub_config +from logging import config as log_config +from onaplogging import monkey +monkey.patch_all() # Build paths inside the project like this: os.path.join(BASE_DIR, ...) BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) @@ -79,6 +82,7 @@ MIDDLEWARE_CLASSES = [ 'django.contrib.auth.middleware.SessionAuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware', + 'lcm.middleware.LogContextMiddleware', ] ROOT_URLCONF = 'lcm.urls' @@ -123,42 +127,46 @@ STATICFILES_DIRS = [ os.path.join(BASE_DIR, "static") ] -config.CATALOG_ROOT_PATH = os.path.join(STATICFILES_DIRS[0], "catalog") -config.CATALOG_URL_PATH = "static/catalog" - -LOGGING = { - 'version': 1, - 'disable_existing_loggers': True, - 'formatters': { - 'standard': { - 'format': '%(asctime)s:[%(name)s]:[%(filename)s]-[%(lineno)d] [%(levelname)s]:%(message)s', - }, - }, - 'filters': { - }, - 'handlers': { - 'lcm_handler': { - 'level': 'DEBUG', - 'class': 'logging.handlers.RotatingFileHandler', - 'filename': os.path.join(BASE_DIR, 'logs/runtime_lcm.log'), - 'formatter': 'standard', - 'maxBytes': 1024 * 1024 * 50, - 'backupCount': 5, - }, - }, - - 'loggers': { - 'lcm': { - 'handlers': ['lcm_handler'], - 'level': 'DEBUG', - 'propagate': False - }, - } -} +pub_config.CATALOG_ROOT_PATH = os.path.join(STATICFILES_DIRS[0], "catalog") +pub_config.CATALOG_URL_PATH = "static/catalog" +# +# LOGGING = { +# 'version': 1, +# 'disable_existing_loggers': True, +# 'formatters': { +# 'standard': { +# 'format': '%(asctime)s:[%(name)s]:[%(filename)s]-[%(lineno)d] [%(levelname)s]:%(message)s', +# }, +# }, +# 'filters': { +# }, +# 'handlers': { +# 'lcm_handler': { +# 'level': 'DEBUG', +# 'class': 'logging.handlers.RotatingFileHandler', +# 'filename': os.path.join(BASE_DIR, 'logs/runtime_lcm.log'), +# 'formatter': 'standard', +# 'maxBytes': 1024 * 1024 * 50, +# 'backupCount': 5, +# }, +# }, +# +# 'loggers': { +# 'lcm': { +# 'handlers': ['lcm_handler'], +# 'level': 'DEBUG', +# 'propagate': False +# }, +# } +# } +LOGGING_CONFIG = None +# yaml configuration of logging +LOGGING_FILE = os.path.join(BASE_DIR, 'lcm/log.yml') +log_config.yamlConfig(filepath=LOGGING_FILE, watchDog=True) if 'test' in sys.argv: - config.REG_TO_MSB_WHEN_START = False - config.DEPLOY_WORKFLOW_WHEN_START = False + pub_config.REG_TO_MSB_WHEN_START = False + pub_config.DEPLOY_WORKFLOW_WHEN_START = False DATABASES = {} DATABASES['default'] = { 'ENGINE': 'django.db.backends.sqlite3', diff --git a/requirements.txt b/requirements.txt index 687b38fe..c1dd615e 100644 --- a/requirements.txt +++ b/requirements.txt @@ -36,3 +36,6 @@ drf-yasg>=1.2.2 # for the validation feature flex>=6.11.1 swagger-spec-validator>=2.1.0 + +# for onap logging +onappylog>=1.0.6 \ No newline at end of file -- cgit 1.2.3-korg