]> git.puffer.fish Git - matthieu/frr.git/commitdiff
topolog: implement a logging abstraction
authorRafael Zalamena <rzalamena@gmail.com>
Thu, 29 Jun 2017 20:55:33 +0000 (17:55 -0300)
committerDonald Sharp <sharpd@cumulusnetworks.com>
Wed, 28 Nov 2018 01:22:11 +0000 (20:22 -0500)
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
tests/topotests/lib/topolog.py [new file with mode: 0644]

index 2b2ea5dcb55505228c9f88e617f5bbbd9b5e645e..d4e08a346104aeceea2c16ae638a2f0d6e0fc8ba 100644 (file)
@@ -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 (file)
index 0000000..0a192ce
--- /dev/null
@@ -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