Skip to content

Instantly share code, notes, and snippets.

@saml
Created August 21, 2018 14:28
Show Gist options
  • Select an option

  • Save saml/dbc4d7e462fc9747a9cda5c077449764 to your computer and use it in GitHub Desktop.

Select an option

Save saml/dbc4d7e462fc9747a9cda5c077449764 to your computer and use it in GitHub Desktop.
import logging
logging.basicConfig(level='DEBUG')
_log = logging.getLogger(__name__)
class Ctx:
def __init__(self, d):
self.d = d
def keys(self):
return self.d.keys()
def __getitem__(self, item):
return self.d[item]
ctx = Ctx(dict(a=1, b=2, c={'d': 3}))
_log.info('Hello', extra=ctx)
"""
Traceback (most recent call last):
File "a.py", line 13, in <module>
_log.info('Hello', extra=ctx)
File "/usr/lib64/python2.7/logging/__init__.py", line 1174, in info
self._log(INFO, msg, args, **kwargs)
File "/usr/lib64/python2.7/logging/__init__.py", line 1292, in _log
record = self.makeRecord(self.name, level, fn, lno, msg, args, exc_info, func, extra)
File "/usr/lib64/python2.7/logging/__init__.py", line 1268, in makeRecord
for key in extra:
File "a.py", line 10, in __getitem__
return self.d[item]
KeyError: 0
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment