comparison src/core/log.py @ 1013:11409a6c16c7

core (log/standard backend): added "%(profile)s" format management
author Goffi <goffi@goffi.org>
date Mon, 05 May 2014 20:16:13 +0200
parents c8771279497e
children 0c361fdc76af
comparison
equal deleted inserted replaced
1012:c8771279497e 1013:11409a6c16c7
187 import inspect 187 import inspect
188 stack = inspect.stack() 188 stack = inspect.stack()
189 current_path = stack[0][1] 189 current_path = stack[0][1]
190 for frame_data in stack[:-1]: 190 for frame_data in stack[:-1]:
191 if frame_data[1] != current_path: 191 if frame_data[1] != current_path:
192 if _backend == C.LOG_BACKEND_STANDARD and "/logging/__init__.py" in frame_data[1]:
193 continue
192 break 194 break
193 195
194 frame = frame_data[0] 196 frame = frame_data[0]
195 args = inspect.getargvalues(frame) 197 args = inspect.getargvalues(frame)
196 try: 198 try:
198 except (TypeError, KeyError): 200 except (TypeError, KeyError):
199 try: 201 try:
200 try: 202 try:
201 profile = args.locals['self'].profile 203 profile = args.locals['self'].profile
202 except AttributeError: 204 except AttributeError:
203 profile = args.locals['self'].parent.profile 205 try:
206 profile = args.locals['self'].parent.profile
207 except AttributeError:
208 profile = args.locals['self'].host.profile # used in quick_frontend for single profile configuration
204 except Exception: 209 except Exception:
205 # we can't find profile, we return an empty value 210 # we can't find profile, we return an empty value
206 profile = '' 211 profile = ''
207 return profile 212 return profile
208 213
553 import logging 558 import logging
554 559
555 class SatFormatter(logging.Formatter): 560 class SatFormatter(logging.Formatter):
556 u"""Formatter which manage SàT specificities""" 561 u"""Formatter which manage SàT specificities"""
557 _format = fmt 562 _format = fmt
563 _with_profile = '%(profile)s' in fmt
558 564
559 def __init__(self, can_colors=False): 565 def __init__(self, can_colors=False):
560 super(SatFormatter, self).__init__(self._format) 566 super(SatFormatter, self).__init__(self._format)
561 self.can_colors = can_colors 567 self.can_colors = can_colors
562 568
563 def format(self, record): 569 def format(self, record):
570 if self._with_profile:
571 record.profile = _getProfile()
564 s = super(SatFormatter, self).format(record) 572 s = super(SatFormatter, self).format(record)
565 if self.with_colors and (self.can_colors or self.force_colors): 573 if self.with_colors and (self.can_colors or self.force_colors):
566 s = _ansiColors(record.levelname, s) 574 s = _ansiColors(record.levelname, s)
567 return s 575 return s
568 576