Skip to content

Instantly share code, notes, and snippets.

@hernantz
Created March 26, 2019 12:55
Show Gist options
  • Save hernantz/3f47e00af96468a71cd8f7fbcd387e3c to your computer and use it in GitHub Desktop.
Save hernantz/3f47e00af96468a71cd8f7fbcd387e3c to your computer and use it in GitHub Desktop.
Logs all extra args
import logging
class ExtraFormatter(logging.Formatter):
defs = set(logging.LogRecord(None, None, None, None, None, None, None).__dict__.keys())
def format(self, record):
extra = set(record.__dict__.keys()) - self.defs
if extra:
# Override message with extra info
record.msg = "{} {}".format(
self._join_extra(record, extra),
record.msg
)
return super(TributeExtraFormatter, self).format(record)
@staticmethod
def _join_extra(record, extra):
return ' '.join(['{}={}'.format(key, getattr(record, key)) for key in extra])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment