From 0303dc6e5210eee33de05a087a8ea40e80d43fb9 Mon Sep 17 00:00:00 2001 From: liangke Date: Tue, 4 Sep 2018 16:43:56 +0800 Subject: Python3 compatible Upgrade code compatibly with python-3.x Change-Id: I04f2e7a92d21f161a2e4cc904743043394616145 Issue-ID: MULTICLOUD-327 Signed-off-by: liangke --- pylog/onaplogging/mdcContext.py | 34 +++++++++++++++++++++++++++++++--- 1 file changed, 31 insertions(+), 3 deletions(-) (limited to 'pylog/onaplogging/mdcContext.py') diff --git a/pylog/onaplogging/mdcContext.py b/pylog/onaplogging/mdcContext.py index 8162b50..60075b4 100644 --- a/pylog/onaplogging/mdcContext.py +++ b/pylog/onaplogging/mdcContext.py @@ -13,7 +13,9 @@ import logging import threading +import io import os +import traceback import sys import functools @@ -21,7 +23,7 @@ import functools __all__ = ['patch_loggingMDC', 'MDC'] _replace_func_name = ['info', 'critical', 'fatal', 'debug', - 'error', 'warn', 'warning', 'findCaller'] + 'error', 'warn', 'warning', 'log', 'findCaller'] class MDCContext(threading.local): @@ -135,7 +137,20 @@ def error(self, msg, *args, **kwargs): self._log(logging.ERROR, msg, args, **kwargs) -def findCaller(self): +@fetchkeys +def log(self, level, msg, *args, **kwargs): + + if not isinstance(level, int): + if logging.raiseExceptions: + raise TypeError("level must be an integer") + else: + return + + if self.isEnabledFor(level): + self._log(level, msg, args, **kwargs) + + +def findCaller(self, stack_info=False): f = logging.currentframe() if f is not None: @@ -148,7 +163,20 @@ def findCaller(self): if filename == logging._srcfile or co.co_name == "replace": f = f.f_back continue - rv = (co.co_filename, f.f_lineno, co.co_name) + if sys.version_info > (3, 2): + sinfo = None + if stack_info: + sio = io.StringIO() + sio.write("Stack (most recent call last):\n") + traceback.print_stack(f, file=sio) + sinfo = sio.getvalue() + if sinfo[-1] == '\n': + sinfo = sinfo[:-1] + sio.close() + rv = (co.co_filename, f.f_lineno, co.co_name, sinfo) + else: + rv = (co.co_filename, f.f_lineno, co.co_name) + break return rv -- cgit 1.2.3-korg