summaryrefslogtreecommitdiffstats
path: root/VNFs/DPPD-PROX/helper-scripts/rapid/rapid_log.py
diff options
context:
space:
mode:
authorLuc Provoost <luc.provoost@intel.com>2020-04-27 12:32:43 -0400
committerLuc Provoost <luc.provoost@intel.com>2020-05-15 11:55:09 +0200
commit4d932aced6931d756e07dbce6ab7cf04799ba5f4 (patch)
treee8ac2d1af449671f451450fb8ae106496a73798f /VNFs/DPPD-PROX/helper-scripts/rapid/rapid_log.py
parent44327c1d12591af41658a4c492fdee597fe4ab74 (diff)
Code rewrite and Python3 support
The code has been rewritten in multiple classes and files for better readability and maintainability. You should now also use Python3 to run the scripts. Some of the variables in the config files have moved to other sections. The pushgateway parameter moved to the [varia] section of the rapid environment file. Change-Id: Ic61a1bc3baa39447d42b21dfcc0f16c28fcdfa6e Signed-off-by: Luc Provoost <luc.provoost@intel.com>
Diffstat (limited to 'VNFs/DPPD-PROX/helper-scripts/rapid/rapid_log.py')
-rw-r--r--VNFs/DPPD-PROX/helper-scripts/rapid/rapid_log.py122
1 files changed, 122 insertions, 0 deletions
diff --git a/VNFs/DPPD-PROX/helper-scripts/rapid/rapid_log.py b/VNFs/DPPD-PROX/helper-scripts/rapid/rapid_log.py
new file mode 100644
index 00000000..bd25845f
--- /dev/null
+++ b/VNFs/DPPD-PROX/helper-scripts/rapid/rapid_log.py
@@ -0,0 +1,122 @@
+#!/usr/bin/python
+
+##
+## Copyright (c) 2010-2020 Intel Corporation
+##
+## 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.
+## See the License for the specific language governing permissions and
+## limitations under the License.
+##
+
+import logging
+from logging.handlers import RotatingFileHandler
+from logging import handlers
+import os
+import sys
+import time
+
+class bcolors(object):
+ HEADER = '\033[95m'
+ OKBLUE = '\033[94m'
+ OKGREEN = '\033[92m'
+ WARNING = '\033[93m'
+ FAIL = '\033[91m'
+ ENDC = '\033[0m'
+ BOLD = '\033[1m'
+ UNDERLINE = '\033[4m'
+ FLASH = '\033[5m'
+
+class RapidLog(object):
+ """
+ Class to deal with rapid logging
+ """
+ log = None
+
+ @staticmethod
+ def log_init(test_params):
+ # create formatters
+ screen_formatter = logging.Formatter("%(message)s")
+ file_formatter = logging.Formatter("%(asctime)s - %(levelname)s - %(message)s")
+
+ # get a top-level logger,
+ # set its log level,
+ # BUT PREVENT IT from propagating messages to the root logger
+ #
+ log = logging.getLogger()
+ numeric_level = getattr(logging, test_params['loglevel'].upper(), None)
+ if not isinstance(numeric_level, int):
+ raise ValueError('Invalid log level: %s' % loglevel)
+ log.setLevel(numeric_level)
+ log.propagate = 0
+
+ # create a console handler
+ # and set its log level to the command-line option
+ #
+ console_handler = logging.StreamHandler(sys.stdout)
+ #console_handler.setLevel(logging.INFO)
+ numeric_screenlevel = getattr(logging, test_params['screenloglevel'].upper(), None)
+ if not isinstance(numeric_screenlevel, int):
+ raise ValueError('Invalid screenlog level: %s' % screenloglevel)
+ console_handler.setLevel(numeric_screenlevel)
+ console_handler.setFormatter(screen_formatter)
+
+ # create a file handler
+ # and set its log level
+ #
+ log_file = 'RUN{}.{}.log'.format(test_params['environment_file'],test_params['test_file'])
+ file_handler = logging.handlers.RotatingFileHandler(log_file, backupCount=10)
+ #file_handler = log.handlers.TimedRotatingFileHandler(log_file, 'D', 1, 5)
+ file_handler.setLevel(numeric_level)
+ file_handler.setFormatter(file_formatter)
+
+ # add handlers to the logger
+ #
+ log.addHandler(file_handler)
+ log.addHandler(console_handler)
+
+ # Check if log exists and should therefore be rolled
+ needRoll = os.path.isfile(log_file)
+
+
+ # This is a stale log, so roll it
+ if needRoll:
+ # Add timestamp
+ log.debug('\n---------\nLog closed on %s.\n---------\n' % time.asctime())
+
+ # Roll over on application start
+ log.handlers[0].doRollover()
+
+ # Add timestamp
+ log.debug('\n---------\nLog started on %s.\n---------\n' % time.asctime())
+
+ log.debug("runrapid.py version: " + test_params['version'])
+ RapidLog.log = log
+
+ @staticmethod
+ def exception(exception_info):
+ RapidLog.log.exception(exception_info)
+ raise Exception(exception_info)
+
+ def critical(critical_info):
+ RapidLog.log.critical(critical_info)
+ raise Exception(critical_info)
+
+ @staticmethod
+ def error(error_info):
+ RapidLog.log.error(error_info)
+
+ @staticmethod
+ def debug(debug_info):
+ RapidLog.log.debug(debug_info)
+
+ @staticmethod
+ def info(info):
+ RapidLog.log.info(info)