diff --git a/src/core/log.py b/src/core/log.py index a25e0686..f1ff98b2 100644 --- a/src/core/log.py +++ b/src/core/log.py @@ -94,7 +94,18 @@ class Logger(object): except AttributeError: if self.filter_name is not None: raise ValueError("Bad filter: filters must have a .filter method") - return self.fmt % record if self.fmt is not None else message + try: + return self.fmt % record + except TypeError: + return message + except KeyError as e: + if e.args[0] == 'profile': + # XXX: %(profile)s use some magic with introspection, for debugging purpose only *DO NOT* use in production + record['profile'] = _getProfile() + return self.fmt % record + else: + raise e + def debug(self, msg): self.log(C.LOG_LVL_DEBUG, msg) @@ -159,6 +170,30 @@ class FilterName(object): return self.filter(log_record) == 1 +def _getProfile(): + """Try to find profile value using introspection""" + import inspect + stack = inspect.stack() + current_path = stack[0][1] + for frame_data in stack[:-1]: + if frame_data[1] != current_path: + break + + frame = frame_data[0] + args = inspect.getargvalues(frame) + try: + profile = args.locals.get('profile') or args.locals['profile_key'] + except (TypeError, KeyError): + try: + try: + profile = args.locals['self'].profile + except AttributeError: + profile = args.locals['self'].parent.profile + except Exception: + # we can't find profile, we return an empty value + profile = '' + return profile + def _ansiColors(level, message): """Colorise message depending on level for terminals