def format(self, record): # Save the original format configured by the user # when the logger formatter was instantiated format_orig = self._fmt # Replace the original format with one customized by logging level if record.levelno == logging.DEBUG: self._fmt = LogFormatter.dbg_fmt self._style = logging.PercentStyle(self._fmt) elif record.levelno == logging.WARNING: self._fmt = LogFormatter.warn_fmt self._style = logging.PercentStyle(self._fmt) elif record.levelno == logging.INFO: self._fmt = LogFormatter.info_fmt self._style = logging.PercentStyle(self._fmt) elif record.levelno == logging.ERROR: self._fmt = LogFormatter.err_fmt self._style = logging.PercentStyle(self._fmt) # Call the original formatter class to do the grunt work result = logging.Formatter.format(self, record) # Restore the original format configured by the user self._fmt = format_orig return result
def format(self, record): if record.levelno <= logging.INFO: self._style = logging.PercentStyle(StyleFormatter.low_style) elif record.levelno <= logging.WARNING: self._style = logging.PercentStyle(StyleFormatter.medium_style) else: self._style = logging.PercentStyle(StyleFormatter.high_style) return logging.Formatter.format(self, record)
def format(self, record): if record.levelno == logging.INFO: self._style = logging.PercentStyle("%(message)s") else: self._style = logging.PercentStyle("%(levelname)s: %(message)s") if record.levelno == logging.WARNING and len(record.args) > 0: # trim the ugly warnings.warn message match = re.search(r"Warning:\s*(.*?)\s*warnings.warn\(", record.args[0], re.DOTALL) if match is not None: record.args = (match.group(1), ) return super().format(record)
def format(self, record): if record.name == "py.warnings": # trim the ugly warnings.warn message match = re.search(r"Warning:\s*(.*?)\s*warnings.warn\(", record.args[0], re.DOTALL) record.args = (match.group(1), ) self._style = logging.PercentStyle("WARNING: %(message)s") else: if record.levelno == logging.WARNING: self._style = logging.PercentStyle("%(message)s") else: self._style = logging.PercentStyle( "%(levelname)s: %(message)s") return super().format(record)
def format(self, record): """Uses contextstring if request_id is set, otherwise default.""" # NOTE(jecarey): If msg is not unicode, coerce it into unicode # before it can get to the python logging and # possibly cause string encoding trouble # NOTE(anand1712): If the msg has value which should not be printed we # remove it from here if (record.name == "wsme.api"): if 'Client-side error: Invalid input for field/attribute name' in record.msg: matchObj = re.match(r'(.*?)\.(.*?)\.(.*?$)', record.msg, re.M | re.I) if matchObj: record.msg = "Client-side error: Invalid input for field/attribute name.." record.msg += matchObj.group(3) else: record.msg = "Client-side error: Invalid input for field/attribute name" if "keystone_authtoken.password" in record.args: record.args = ("keystone_authtoken.password", "****") if not isinstance(record.msg, six.text_type): record.msg = six.text_type(record.msg) # store project info record.project = self.project record.version = self.version # store request info context = getattr(local.store, 'context', None) if context: d = _dictify_context(context) for k, v in d.items(): setattr(record, k, v) # NOTE(sdague): default the fancier formatting params # to an empty string so we don't throw an exception if # they get used for key in ('instance', 'color', 'user_identity'): if key not in record.__dict__: record.__dict__[key] = '' if record.__dict__.get('request_id'): fmt = CONF.logging_context_format_string else: fmt = CONF.logging_default_format_string if (record.levelno == logging.DEBUG and CONF.logging_debug_format_suffix): fmt += " " + CONF.logging_debug_format_suffix if sys.version_info < (3, 2): self._fmt = fmt else: self._style = logging.PercentStyle(fmt) self._fmt = self._style._fmt # Cache this on the record, Logger will respect our formatted copy if record.exc_info: record.exc_text = self.formatException(record.exc_info, record) return logging.Formatter.format(self, record)
def format(self, record): # Set to default format (in case none of the following applies) #self._fmt = dflt_fm format_orig = self._fmt # Replace the original format with one customized by logging level #the reason why we do the check here and not in init is, that this is more robust: the user could set self.info_fmt to None even after __init__ and it still works. if record.levelno == logging.DEBUG: self._fmt = self.dflt_fmt if self.dbg_fmt is None else self.dbg_fmt elif record.levelno == logging.INFO: self._fmt = self.dflt_fmt if self.info_fmt is None else self.info_fmt elif record.levelno == logging.WARNING: self._fmt = self.dflt_fmt if self.wrn_fmt is None else self.wrn_fmt elif record.levelno == logging.ERROR: self._fmt = self.dflt_fmt if self.err_fmt is None else self.err_fmt elif record.levelno == logging.CRITICAL: self._fmt = self.dflt_fmt if self.crt_fmt is None else self.crt_fmt self._style = logging.PercentStyle(self._fmt) # Call the original formatter class to do the grunt work result = logging.Formatter.format(self, record) self._fmt = format_orig return result
def format(self, record): """Uses default.""" # NOTE(jecarey): If msg is not unicode, coerce it into unicode # before it can get to the python logging and # possibly cause string encoding trouble if not isinstance(record.msg, six.text_type): record.msg = six.text_type(record.msg) # store project info record.project = self.project record.version = self.version fmt = CONF.logging_default_format_string if (record.levelno == logging.DEBUG and CONF.logging_debug_format_suffix): fmt += " " + CONF.logging_debug_format_suffix if sys.version_info < (3, 2): self._fmt = fmt else: self._style = logging.PercentStyle(fmt) self._fmt = self._style._fmt # Cache this on the record, Logger will respect our formatted copy if record.exc_info: record.exc_text = self.formatException(record.exc_info, record) return logging.Formatter.format(self, record)
def format(self, record: logging.LogRecord) -> str: """ Format the specified record as text. The record's attribute dictionary is used as the operand to a string formatting operation which yields the returned string. Before formatting the dictionary, a couple of preparatory steps are carried out. The message attribute of the record is computed using LogRecord.getMessage(). If the formatting string uses the time (as determined by a call to usesTime(), formatTime() is called to format the event time. If there is exception information, it is formatted using formatException() and appended to the message. """ if record.levelno == logging.DEBUG: self._fmt = self.dbg_fmt elif record.levelno == logging.INFO: self._fmt = self.info_fmt else: self._fmt = self.default_fmt self._style = logging.PercentStyle(self._fmt) record.message = record.getMessage() if self.usesTime(): record.asctime = self.formatTime(record, self.datefmt) s = self.formatMessage(record) if record.exc_info: # Cache the traceback text to avoid converting it multiple times # (it's constant anyway) if not record.exc_text: record.exc_text = self.formatException(record.exc_info) return s
def format(self, record): # custom formatters interface changes in python 3.2 if sys.version_info < (3, 2): self._fmt = "Foo " + self._fmt else: self._style = logging.PercentStyle("Foo " + self._style._fmt) self._fmt = self._style._fmt return logging.Formatter.format(self, record)
def format(self, record): ''' Depending on the level, change the formatting. ''' self._fmt = self.FORMATS.get( record.levelno, record.levelno) + ':%(name)s:%(funcName)s:%(lineno)s|%(message)s' self._style = logging.PercentStyle(self._fmt) return logging.Formatter.format(self, record)
def format(self, record): """Uses contextstring if request_id is set, otherwise default.""" # NOTE(jecarey): If msg is not unicode, coerce it into unicode # before it can get to the python logging and # possibly cause string encoding trouble if not isinstance(record.msg, six.text_type): record.msg = six.text_type(record.msg) # store project info record.project = self.project record.version = self.version context = _update_record_with_context(record) if context: # FIXME(dhellmann): We should replace these nova-isms with # more generic handling in the Context class. See the # app-agnostic-logging-parameters blueprint. instance = getattr(context, 'instance', None) instance_uuid = getattr(context, 'instance_uuid', None) instance_extra = '' if instance: instance_extra = (self.conf.instance_format % {'uuid': instance}) elif instance_uuid: instance_extra = (self.conf.instance_uuid_format % {'uuid': instance_uuid}) record.instance = instance_extra # NOTE(sdague): default the fancier formatting params # to an empty string so we don't throw an exception if # they get used for key in ('instance', 'color', 'user_identity'): if key not in record.__dict__: record.__dict__[key] = '' if record.__dict__.get('request_id'): fmt = self.conf.logging_context_format_string else: fmt = self.conf.logging_default_format_string if (record.levelno == logging.DEBUG and self.conf.logging_debug_format_suffix): fmt += " " + self.conf.logging_debug_format_suffix if sys.version_info < (3, 2): self._fmt = fmt else: self._style = logging.PercentStyle(fmt) self._fmt = self._style._fmt # Cache this on the record, Logger will respect our formatted copy if record.exc_info: record.exc_text = self.formatException(record.exc_info, record) return logging.Formatter.format(self, record)
def format(self, record): # Save the original format configured by the user # when the logger formatter was instantiated format_orig = self._fmt # Replace the original format with one customized by logging level if record.levelno == logging.INFO: self._fmt = self.info_fmt # For Python>3.2 self._style = logging.PercentStyle(self._fmt) # Call the original formatter class to do the grunt work result = logging.Formatter.format(self, record) # Restore the original format configured by the user self._fmt = format_orig # For Python>3.2 self._style = logging.PercentStyle(self._fmt) return result
def format(self, record): if record.levelno == logging.DEBUG: fmt = self.fmt + self.debug_suffix else: fmt = self.fmt if sys.version_info < (3, 2): self._fmt = fmt else: self._style = logging.PercentStyle(fmt) self._fmt = self._style._fmt return logging.Formatter.format(self, record)
def __init__(self, fmt='%(asctime)s - %(name)s - %(levelname)s - %(message)s'): logging.Formatter.__init__(self, fmt, "%d/%m/%y - %H:%M:%S") self._formatter = { logging.INFO: logging.PercentStyle('%(asctime)s - %(name)s - ' + _('%(levelname)s', c='g') + ' - %(message)s'), logging.ERROR: logging.PercentStyle('%(asctime)s - %(name)s - ' + _('%(levelname)s', c='r') + ' - %(message)s'), logging.WARNING: logging.PercentStyle('%(asctime)s - %(name)s - ' + _('%(levelname)s', c='y') + ' - %(message)s'), logging.CRITICAL: logging.PercentStyle('%(asctime)s - %(name)s - ' + _('%(levelname)s', c='5;31;1;49') + ' - %(message)s'), logging.DEBUG: logging.PercentStyle('%(asctime)s - %(name)s - ' + _('%(levelname)s', c='m') + ' - %(message)s'), 'DEFAULT': logging.PercentStyle('%(asctime)s - %(name)s - %(levelname)s - %(message)s'), }
def format(self, record): if record.levelno == LOG_LEVELS['DEBUG']: self._fmt = self.fmt_debug elif record.levelno == LOG_LEVELS['INFO']: self._fmt = self.fmt_info elif record.levelno == LOG_LEVELS['INFO_GREEN']: self._fmt = self.fmt_info elif record.levelno == LOG_LEVELS['INFO_BLUE']: self._fmt = self.fmt_info elif record.levelno == LOG_LEVELS['INFO_YELLOW']: self._fmt = self.fmt_info elif record.levelno == LOG_LEVELS['WARNING']: self._fmt = self.fmt_warning elif record.levelno >= LOG_LEVELS['ERROR']: self._fmt = self.fmt_error self._style = logging.PercentStyle(self._fmt) return logging.Formatter.format(self, record)
def format(self, record): """Uses contextstring if request_id is set, otherwise default.""" # NOTE(jecarey): If msg is not unicode, coerce it into unicode # before it can get to the python logging and # possibly cause string encoding trouble if not isinstance(record.msg, six.text_type): record.msg = six.text_type(record.msg) # store project info record.project = self.project record.version = self.version # store request info context = getattr(local.store, 'context', None) if context: d = _dictify_context(context) for k, v in d.items(): setattr(record, k, v) # NOTE(sdague): default the fancier formatting params # to an empty string so we don't throw an exception if # they get used for key in ('instance', 'color', 'user_identity'): if key not in record.__dict__: record.__dict__[key] = '' if record.__dict__.get('request_id'): fmt = CONF.logging_context_format_string else: fmt = CONF.logging_default_format_string if (record.levelno == logging.DEBUG and CONF.logging_debug_format_suffix): fmt += " " + CONF.logging_debug_format_suffix if sys.version_info < (3, 2): self._fmt = fmt else: self._style = logging.PercentStyle(fmt) self._fmt = self._style._fmt # Cache this on the record, Logger will respect our formatted copy if record.exc_info: record.exc_text = self.formatException(record.exc_info, record) return logging.Formatter.format(self, record)
def format(self, record): """ Format the received log record. Parameters ---------- record : logging.LogRecord """ if record.levelno == LOG_LEVELS['DEBUG']: self._fmt = self.fmt_debug elif record.levelno == LOG_LEVELS['INFO']: self._fmt = self.fmt_info elif record.levelno == LOG_LEVELS['WARNING']: self._fmt = self.fmt_warning elif record.levelno >= LOG_LEVELS['ERROR']: self._fmt = self.fmt_error self._style = logging.PercentStyle(self._fmt) return logging.Formatter.format(self, record)
def format(self, record): """Uses contextstring if request_id is set, otherwise default.""" # store project info record.project = self.project record.version = self.version # FIXME(dims): We need a better way to pick up the instance # or instance_uuid parameters from the kwargs from say # LOG.info or LOG.warn instance_extra = '' instance = getattr(record, 'instance', None) instance_uuid = getattr(record, 'instance_uuid', None) context = _update_record_with_context(record) if instance: try: instance_extra = (self.conf.instance_format % instance) except TypeError: instance_extra = instance elif instance_uuid: instance_extra = (self.conf.instance_uuid_format % { 'uuid': instance_uuid }) elif context: # FIXME(dhellmann): We should replace these nova-isms with # more generic handling in the Context class. See the # app-agnostic-logging-parameters blueprint. instance = getattr(context, 'instance', None) instance_uuid = getattr(context, 'instance_uuid', None) # resource_uuid was introduced in oslo_context's # RequestContext resource_uuid = getattr(context, 'resource_uuid', None) if instance: instance_extra = (self.conf.instance_format % { 'uuid': instance }) elif instance_uuid: instance_extra = (self.conf.instance_uuid_format % { 'uuid': instance_uuid }) elif resource_uuid: instance_extra = (self.conf.instance_uuid_format % { 'uuid': resource_uuid }) record.instance = instance_extra # NOTE(sdague): default the fancier formatting params # to an empty string so we don't throw an exception if # they get used for key in ('instance', 'color', 'user_identity', 'resource', 'user_name', 'project_name'): if key not in record.__dict__: record.__dict__[key] = '' # Set the "user_identity" value of "logging_context_format_string" # by using "logging_user_identity_format" and # get_logging_values of oslo.context. if context: record.user_identity = ( self.conf.logging_user_identity_format % _ReplaceFalseValue(_dictify_context(context))) if record.__dict__.get('request_id'): fmt = self.conf.logging_context_format_string else: fmt = self.conf.logging_default_format_string # Cache the formatted traceback on the record, Logger will # respect our formatted copy if record.exc_info: record.exc_text = self.formatException(record.exc_info, record) record.error_summary = _get_error_summary(record) if '%(error_summary)s' in fmt: # If we have been told explicitly how to format the error # summary, make sure there is always a default value for # it. record.error_summary = record.error_summary or '-' elif record.error_summary: # If we have not been told how to format the error and # there is an error to summarize, make sure the format # string includes the bits we need to include it. fmt += ': %(error_summary)s' if (record.levelno == logging.DEBUG and self.conf.logging_debug_format_suffix): fmt += " " + self.conf.logging_debug_format_suffix self._compute_iso_time(record) if sys.version_info < (3, 2): self._fmt = fmt else: self._style = logging.PercentStyle(fmt) self._fmt = self._style._fmt try: return logging.Formatter.format(self, record) except TypeError as err: # Something went wrong, report that instead so we at least # get the error message. record.msg = 'Error formatting log line msg={!r} err={!r}'.format( record.msg, err).replace('%', '*') return logging.Formatter.format(self, record)
def format(self, record): """Uses contextstring if request_id is set, otherwise default.""" # NOTE(jecarey): If msg is not unicode, coerce it into unicode # before it can get to the python logging and # possibly cause string encoding trouble if not isinstance(record.msg, six.text_type): record.msg = six.text_type(record.msg) # store project info record.project = self.project record.version = self.version # FIXME(dims): We need a better way to pick up the instance # or instance_uuid parameters from the kwargs from say # LOG.info or LOG.warn instance_extra = '' instance = getattr(record, 'instance', None) instance_uuid = getattr(record, 'instance_uuid', None) context = _update_record_with_context(record) if instance: try: instance_extra = (self.conf.instance_format % instance) except TypeError: instance_extra = instance elif instance_uuid: instance_extra = (self.conf.instance_uuid_format % {'uuid': instance_uuid}) elif context: # FIXME(dhellmann): We should replace these nova-isms with # more generic handling in the Context class. See the # app-agnostic-logging-parameters blueprint. instance = getattr(context, 'instance', None) instance_uuid = getattr(context, 'instance_uuid', None) # resource_uuid was introduced in oslo_context's # RequestContext resource_uuid = getattr(context, 'resource_uuid', None) if instance: instance_extra = (self.conf.instance_format % {'uuid': instance}) elif instance_uuid: instance_extra = (self.conf.instance_uuid_format % {'uuid': instance_uuid}) elif resource_uuid: instance_extra = (self.conf.instance_uuid_format % {'uuid': resource_uuid}) record.instance = instance_extra # NOTE(sdague): default the fancier formatting params # to an empty string so we don't throw an exception if # they get used for key in ('instance', 'color', 'user_identity', 'resource', 'user_name', 'project_name'): if key not in record.__dict__: record.__dict__[key] = '' # Set the "user_identity" value of "logging_context_format_string" # by using "logging_user_identity_format" and # "to_dict()" of oslo.context. if context: record.user_identity = ( self.conf.logging_user_identity_format % _ReplaceFalseValue(context.__dict__) ) if record.__dict__.get('request_id'): fmt = self.conf.logging_context_format_string else: fmt = self.conf.logging_default_format_string if (record.levelno == logging.DEBUG and self.conf.logging_debug_format_suffix): fmt += " " + self.conf.logging_debug_format_suffix if sys.version_info < (3, 2): self._fmt = fmt else: self._style = logging.PercentStyle(fmt) self._fmt = self._style._fmt # Cache this on the record, Logger will respect our formatted copy if record.exc_info: record.exc_text = self.formatException(record.exc_info, record) return logging.Formatter.format(self, record)
def format(self, record): s = level_strings.get(record.levelno, "\033[1;31mUNKNOWN_LEVEL("+str(record.levelno)+")") self._fmt = self.orig_format.format(s[0] + s[1] + " ") self._style = logging.PercentStyle(self._fmt) return logging.Formatter.format(self, record)
def format(self, record): self._fmt = self.style_dict.get(record.levelno, self.default_style) if six.PY3: self._style = logging.PercentStyle(self._fmt) return logging.Formatter.format(self, record)
def __init__(self): self._custom_root_style = logging.PercentStyle(self.custom_root_format) super().__init__(self.custom_format)