From 36d1dc45eb6add18273b03af4c02dc89966859b0 Mon Sep 17 00:00:00 2001 From: Rafael Zalamena Date: Thu, 29 Jun 2017 17:55:33 -0300 Subject: [PATCH] topolog: implement a logging abstraction The default logger (root) is already being used by Mininet, so to allow customizing logging output and configuring log files Topolog was created. Topolog is no more than a thin layer abstraction to call logging functions without using the 'root' logger. --- tests/topotests/lib/topogen.py | 4 ++ tests/topotests/lib/topolog.py | 70 ++++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+) create mode 100644 tests/topotests/lib/topolog.py diff --git a/tests/topotests/lib/topogen.py b/tests/topotests/lib/topogen.py index 2b2ea5dcb5..d4e08a3461 100644 --- a/tests/topotests/lib/topogen.py +++ b/tests/topotests/lib/topogen.py @@ -48,6 +48,7 @@ from mininet.log import setLogLevel from mininet.cli import CLI from lib import topotest +from lib.topolog import logger, logger_config CWD = os.path.dirname(os.path.realpath(__file__)) @@ -214,6 +215,9 @@ class Topogen(object): if log_level is None: log_level = self.config.get('topogen', 'verbosity') + # Set python logger level + logger_config.set_log_level(log_level) + # Run mininet setLogLevel(log_level) self.net.start() diff --git a/tests/topotests/lib/topolog.py b/tests/topotests/lib/topolog.py new file mode 100644 index 0000000000..0a192ce285 --- /dev/null +++ b/tests/topotests/lib/topolog.py @@ -0,0 +1,70 @@ +# +# topolog.py +# Library of helper functions for NetDEF Topology Tests +# +# Copyright (c) 2017 by +# Network Device Education Foundation, Inc. ("NetDEF") +# +# Permission to use, copy, modify, and/or distribute this software +# for any purpose with or without fee is hereby granted, provided +# that the above copyright notice and this permission notice appear +# in all copies. +# +# THE SOFTWARE IS PROVIDED "AS IS" AND NETDEF DISCLAIMS ALL WARRANTIES +# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL NETDEF BE LIABLE FOR +# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY +# DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, +# WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS +# ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE +# OF THIS SOFTWARE. +# + +""" +Logging utilities for topology tests. + +This file defines our logging abstraction. +""" + +import logging + +# Helper dictionary to convert Topogen logging levels to Python's logging. +DEBUG_TOPO2LOGGING = { + 'debug': logging.DEBUG, + 'info': logging.INFO, + 'output': logging.INFO, + 'warning': logging.WARNING, + 'error': logging.ERROR, + 'critical': logging.CRITICAL, +} + +# +# Logger class definition +# + +class Logger(object): + """ + Logger class that encapsulates logging functions, internaly it uses Python + logging module with a separated instance instead of global. + + Default logging level is 'info'. + """ + + def __init__(self): + self.logger = logging.Logger('topolog', level=logging.INFO) + self.handler = logging.StreamHandler() + self.handler.setFormatter( + logging.Formatter(fmt='%(asctime)s %(levelname)s: %(message)s') + ) + self.logger.addHandler(self.handler) + + def set_log_level(self, level): + "Set the logging level" + self.logger.setLevel(DEBUG_TOPO2LOGGING.get(level)) + +# +# Global variables +# + +logger_config = Logger() +logger = logger_config.logger -- 2.39.5