From ab89ca56c4557ce87e76aa41ee54bed333d9ca41 Mon Sep 17 00:00:00 2001 From: Michal Minar Date: Thu, 12 Sep 2013 12:14:33 +0200 Subject: logging improvements and fix Reduced length of tracing messages written to log by rendering just the first item of list, dict or set instead of whole argument. This will also improve execution time when running in debug mode. Fixed logging decorator of software job manager which previously rendered informations from uninteresting frame. --- src/python/lmi/providers/cmpi_logging.py | 51 +++++++++++++++++++++++--------- 1 file changed, 37 insertions(+), 14 deletions(-) (limited to 'src/python') diff --git a/src/python/lmi/providers/cmpi_logging.py b/src/python/lmi/providers/cmpi_logging.py index 1eb9e3a..ca24601 100644 --- a/src/python/lmi/providers/cmpi_logging.py +++ b/src/python/lmi/providers/cmpi_logging.py @@ -184,6 +184,41 @@ class CMPILogger(logging.getLoggerClass()): logging.setLoggerClass(CMPILogger) +def render_value(val): + """ + When logging values, we want to avoid excessively long messages caused + by rendering argument values like lists, dictionaries etc. + Let's shorten these iterable objects to just one or few items. + + :param val: Any value for rendering. + :returns: Representation string of value, possibly shortened. + :rtype: string + """ + if isinstance(val, list): + if len(val) < 2: + return repr(val) + else: + return "[%s, ... (%d more items)]" % ( + render_value(val[0]), len(val) - 1) + elif isinstance(val, dict): + if len(val) < 2: + return repr(val) + else: + key = next(iter(val)) + return '{%s: %s, ... (%d more items)}' % ( + render_value(key), render_value(val[key]), len(val) - 1) + elif isinstance(val, set): + if len(val) < 2: + return repr(val) + else: + return '{%s, ... (%d more items)}' % ( + render_value(val[0]), len(val) - 1) + elif isinstance(val, tuple): + return "(%s%s)" % ( + ", ".join(render_value(i) for i in val), + ", " if len(val) < 2 else '') + return repr(val) + def trace_function_or_method(is_method=False, frame_level=1): """ Factory for function and method decorators. Generated decorators @@ -211,18 +246,6 @@ def trace_function_or_method(is_method=False, frame_level=1): if not inspect.ismethod(func) and not inspect.isfunction(func): raise TypeError("func must be a function") - def _print_value(val): - """ - Used here for printing function arguments. Shortens the output - string, if that would be too long. - """ - if isinstance(val, list): - if len(val) < 2: - return str(val) - else: - return "[%s, ...]" % _print_value(val[0]) - return str(val) - module = func.__module__.split('.')[-1] frm = inspect.currentframe() for _ in range(frame_level): @@ -250,8 +273,8 @@ def trace_function_or_method(is_method=False, frame_level=1): "lineno" : lineno, "action" : "entering", "args" : ", ".join(chain( - (_print_value(a) for a in args), - ( "%s=%s"%(k, _print_value(v)) + (render_value(a) for a in args), + ( "%s=%s"%(k, render_value(v)) for k, v in kwargs.items()))) }) -- cgit