From 77f3ffce67fd3820a58487e62d7b67ec08d12be5 Mon Sep 17 00:00:00 2001 From: Mark Wilson Date: Fri, 28 Jul 2017 21:25:51 +0100 Subject: [PATCH] Fix logging to work with python 2.6 Python 2.6 has a number of different areas: o setLevel silently ignores non integers, so strings cannot be used. o getLogger at the top level needs to be 'root' if submodules are to inherit correctly. Tidied up the library logging definition to follow the root logger. Addresses Issue: #67 --- rhsda.py | 9 +-------- rhsecapi.py | 16 +++++++++++----- 2 files changed, 12 insertions(+), 13 deletions(-) diff --git a/rhsda.py b/rhsda.py index d874299..3427c24 100644 --- a/rhsda.py +++ b/rhsda.py @@ -31,13 +31,7 @@ # Logging -logging.addLevelName(25, 'NOTICE') -consolehandler = logging.StreamHandler() -consolehandler.setLevel('DEBUG') -consolehandler.setFormatter(logging.Formatter("[%(levelname)-7s] %(name)s: %(message)s")) -logger = logging.getLogger('rhsda') -logger.setLevel('NOTICE') -logger.addHandler(consolehandler) +logger = logging.getLogger(__name__) # Establish cveFields namespace @@ -176,7 +170,6 @@ class ApiClient: def __init__(self, logLevel='notice'): self.cfg = Namespace() self.cfg.apiUrl = 'https://access.redhat.com/labs/securitydataapi' - logger.setLevel(logLevel.upper()) def _get_terminal_width(self): h, w, hp, wp = struct.unpack('HHHH', fcntl.ioctl(0, termios.TIOCGWINSZ, struct.pack('HHHH', 0, 0, 0, 0))) diff --git a/rhsecapi.py b/rhsecapi.py index 03a463b..7a9caa6 100755 --- a/rhsecapi.py +++ b/rhsecapi.py @@ -48,15 +48,21 @@ vers = {} vers['version'] = '1.0.1' vers['date'] = '2017/06/27' +logLevels = { + 'DEBUG': logging.DEBUG, + 'INFO': logging.INFO, + 'WARNING': logging.WARNING, + 'NOTICE': 25, + 'ERROR': logging.ERROR, +} + # Logging -logging.addLevelName(25, 'NOTICE') +logging.addLevelName(logLevels['NOTICE'], 'NOTICE') consolehandler = logging.StreamHandler() -consolehandler.setLevel('DEBUG') consolehandler.setFormatter(logging.Formatter("[%(levelname)-7s] %(name)s: %(message)s")) -logger = logging.getLogger('rhsecapi') -logger.setLevel('NOTICE') +logger = logging.getLogger() logger.addHandler(consolehandler) @@ -340,7 +346,7 @@ def parse_args(): o.outFormat = 'jsonpretty' else: o.outFormat = 'plaintext' - logger.setLevel(o.loglevel.upper()) + logger.setLevel(logLevels[o.loglevel.upper()]) return o