@@ -23,8 +23,12 @@ class FluentRecordFormatter(logging.Formatter, object):
2323 Best used with server storing data in an ElasticSearch cluster for example.
2424
2525 :param fmt: a dict with format string as values to map to provided keys.
26+ :param datefmt: strftime()-compatible date/time format string.
27+ :param style: (NOT USED)
28+ :param fill_missing_fmt_key: if True, do not raise a KeyError if the format
29+ key is not found. Put None if not found.s
2630 """
27- def __init__ (self , fmt = None , datefmt = None , style = '%' ):
31+ def __init__ (self , fmt = None , datefmt = None , style = '%' , fill_missing_fmt_key = False ):
2832 super (FluentRecordFormatter , self ).__init__ (None , datefmt )
2933
3034 if not fmt :
@@ -38,6 +42,8 @@ def __init__(self, fmt=None, datefmt=None, style='%'):
3842
3943 self .hostname = socket .gethostname ()
4044
45+ self .fill_missing_fmt_key = fill_missing_fmt_key
46+
4147 def format (self , record ):
4248 # Only needed for python2.6
4349 if sys .version_info [0 :2 ] <= (2 , 6 ) and self .usesTime ():
@@ -47,9 +53,18 @@ def format(self, record):
4753 super (FluentRecordFormatter , self ).format (record )
4854 # Add ours
4955 record .hostname = self .hostname
56+
5057 # Apply format
51- data = dict ([(key , value % record .__dict__ )
52- for key , value in self ._fmt_dict .items ()])
58+ data = {}
59+ for key , value in self ._fmt_dict .items ():
60+ try :
61+ value = value % record .__dict__
62+ except KeyError as exc :
63+ value = None
64+ if not self .fill_missing_fmt_key :
65+ raise exc
66+
67+ data [key ] = value
5368
5469 self ._structuring (data , record )
5570 return data
0 commit comments