78
79
int(config['logstash']['version'])
84
class ExtraLogger(logging.Logger):
86
"""A logger that handles passing 'extra' arguments to all logging calls.
88
Tired of having to write:
90
logger.info("Some message", extra=extra)
94
Now you can install this class as the Logger class:
97
>>> logging.setLoggerClass(ExtraLogger)
99
Then, to use it, get your logger as usual:
101
>>> logger = logging.getLogger(__name__)
103
...and set your extra arguments once only:
105
>>> logger.set_extra_args(dict(foo='bar', baz='123'))
107
And those arguments will be included in all normal log messages:
109
>>> logger.info("Hello World") # message will contain extra args set above
111
Extra arguments can be passed to individual logging calls, and will take
112
priority over any set with the set_extra_args call.
116
def __init__(self, *args, **kwargs):
117
super().__init__(*args, **kwargs)
118
self._extra_args = dict()
120
def set_extra_args(self, extra_args):
121
"""Set the 'extra' arguments you want to be passed to every log message.
123
:param extra_args: A mapping type that contains the extra arguments you
125
:raises TypeError: if extra_args is not a mapping type.
128
if not isinstance(extra_args, collections.Mapping):
129
raise TypeError("extra_args must be a mapping")
130
self._extra_args = extra_args
132
def makeRecord(self, name, level, fn, lno, msg, args, exc_info,
133
func=None, user_extra=None, sinfo=None):
134
extra = self._extra_args.copy()
136
extra.update(user_extra)
137
return super().makeRecord(name, level, fn, lno, msg, args, exc_info,