diff options
author | Michael O'Brien <frank.obrien@amdocs.com> | 2018-09-10 15:47:22 +0000 |
---|---|---|
committer | Gerrit Code Review <gerrit@onap.org> | 2018-09-10 15:47:22 +0000 |
commit | 915f857e3330ae376ee23fd8499b3ffa7c5c4ff9 (patch) | |
tree | a8143c37945e11f0662944fbc3f18bc6f8dcb6b7 /pylog/onaplogging/mdcformatter.py | |
parent | a53c031c37451d604b56eedca7eed96d93b7730a (diff) | |
parent | 0303dc6e5210eee33de05a087a8ea40e80d43fb9 (diff) |
Merge "Python3 compatible"
Diffstat (limited to 'pylog/onaplogging/mdcformatter.py')
-rw-r--r-- | pylog/onaplogging/mdcformatter.py | 59 |
1 files changed, 47 insertions, 12 deletions
diff --git a/pylog/onaplogging/mdcformatter.py b/pylog/onaplogging/mdcformatter.py index 253420d..01056a4 100644 --- a/pylog/onaplogging/mdcformatter.py +++ b/pylog/onaplogging/mdcformatter.py @@ -9,7 +9,7 @@ # 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 sys import logging @@ -19,15 +19,33 @@ class MDCFormatter(logging.Formatter): to enrich log message. """ - def __init__(self, fmt=None, mdcfmt=None, datefmt=None): + def __init__(self, fmt=None, mdcfmt=None, datefmt=None, style="%"): """ :param fmt: build-in format string contains standard Python %-style mapping keys :param mdcFmt: mdc format with '{}'-style mapping keys :param datefmt: Date format to use + :param style: style mapping keys in python3 """ + if sys.version_info > (3, 2): + super(MDCFormatter, self).__init__(fmt=fmt, datefmt=datefmt, + style=style) + elif sys.version_info > (2, 7): + super(MDCFormatter, self).__init__(fmt=fmt, datefmt=datefmt) + else: + logging.Formatter.__init__(self, fmt, datefmt) + + self.style = style + self._mdc_tag = "%(mdc)s" + if sys.version_info > (3, 2): + if self.style not in logging._STYLES: + raise ValueError('Style must be one of: %s' % ','.join( + logging._STYLES.keys())) + if self.style == "{": + self._mdc_tag = "{mdc}" + elif self.style == "$": + self._mdc_tag = "${mdc}" - super(MDCFormatter, self).__init__(fmt=fmt, datefmt=datefmt) self._tmpfmt = self._fmt if mdcfmt: self._mdcFmt = mdcfmt @@ -60,7 +78,7 @@ class MDCFormatter(logging.Formatter): elif len(st) > 0: st.pop() - keys = filter(lambda x: x[1:-1].strip('\n \t ') != "", keys) + keys = list(filter(lambda x: x[1:-1].strip('\n \t ') != "", keys)) words = None if keys: words = map(lambda x: x[1:-1], keys) @@ -88,15 +106,25 @@ class MDCFormatter(logging.Formatter): the output of mdc message: 'key1=value1 key3=' """ - mdcIndex = self._fmt.find('%(mdc)s') + mdcIndex = self._fmt.find(self._mdc_tag) if mdcIndex == -1: - return super(MDCFormatter, self).format(record) + if sys.version_info > (2, 7): + return super(MDCFormatter, self).format(record) + else: + return logging.Formatter.format(self, record) mdcFmtkeys, mdcFmtWords = self._mdcfmtKey() - if mdcFmtWords is None: - self._fmt = self._fmt.replace("%(mdc)s", "") - return super(MDCFormatter, self).format(record) + if mdcFmtWords is None: + if sys.version_info > (3, 2): + self._style = logging._STYLES[self.style][0]( + self._fmt.replace(self._mdc_tag, "")) + else: + self._fmt = self._fmt.replace(self._mdc_tag, "") + if sys.version_info > (2, 7): + return super(MDCFormatter, self).format(record) + else: + return logging.Formatter.format(self, record) mdc = record.__dict__.get('mdc', None) res = {} @@ -109,12 +137,19 @@ class MDCFormatter(logging.Formatter): del mdc try: mdcstr = self._replaceStr(keys=mdcFmtkeys).format(**res) - self._fmt = self._fmt.replace("%(mdc)s", mdcstr) - s = super(MDCFormatter, self).format(record) + if sys.version_info > (3, 2): + self._style = logging._STYLES[self.style][0]( + self._fmt.replace(self._mdc_tag, mdcstr)) + else: + self._fmt = self._fmt.replace(self._mdc_tag, mdcstr) + if sys.version_info > (2, 7): + s = super(MDCFormatter, self).format(record) + else: + s = logging.Formatter.format(self, record) return s except KeyError as e: - print("The mdc key %s format is wrong" % e.message) + print("The mdc key %s format is wrong" % str(e)) except Exception: raise |