7
7
import re
8
8
from datetime import date , datetime , time
9
9
import traceback
10
+ import importlib
10
11
11
12
from inspect import istraceback
12
13
13
- #Support order in python 2.7 and 3
14
+ # Support order in python 2.7 and 3
14
15
try :
15
16
from collections import OrderedDict
16
17
except ImportError :
@@ -34,7 +35,7 @@ def merge_record_extra(record, target, reserved):
34
35
:param reserved: dict or list with reserved keys to skip
35
36
"""
36
37
for key , value in record .__dict__ .items ():
37
- #this allows to have numeric keys
38
+ # this allows to have numeric keys
38
39
if (key not in reserved
39
40
and not (hasattr (key , "startswith" )
40
41
and key .startswith ('_' ))):
@@ -46,6 +47,7 @@ class JsonEncoder(json.JSONEncoder):
46
47
"""
47
48
A custom encoder extending the default JSONEncoder
48
49
"""
50
+
49
51
def default (self , obj ):
50
52
if isinstance (obj , (date , datetime , time )):
51
53
return self .format_datetime_obj (obj )
@@ -100,17 +102,17 @@ def __init__(self, *args, **kwargs):
100
102
to log record using string as key. If True boolean is passed, timestamp key
101
103
will be "timestamp". Defaults to False/off.
102
104
"""
103
- self .json_default = kwargs .pop ("json_default" , None )
104
- self .json_encoder = kwargs .pop ("json_encoder" , None )
105
- self .json_serializer = kwargs .pop ("json_serializer" , json .dumps )
105
+ self .json_default = self . _str_to_fn ( kwargs .pop ("json_default" , None ) )
106
+ self .json_encoder = self . _str_to_fn ( kwargs .pop ("json_encoder" , None ) )
107
+ self .json_serializer = self . _str_to_fn ( kwargs .pop ("json_serializer" , json .dumps ) )
106
108
self .json_indent = kwargs .pop ("json_indent" , None )
107
109
self .json_ensure_ascii = kwargs .pop ("json_ensure_ascii" , True )
108
110
self .prefix = kwargs .pop ("prefix" , "" )
109
111
reserved_attrs = kwargs .pop ("reserved_attrs" , RESERVED_ATTRS )
110
112
self .reserved_attrs = dict (zip (reserved_attrs , reserved_attrs ))
111
113
self .timestamp = kwargs .pop ("timestamp" , False )
112
114
113
- #super(JsonFormatter, self).__init__(*args, **kwargs)
115
+ # super(JsonFormatter, self).__init__(*args, **kwargs)
114
116
logging .Formatter .__init__ (self , * args , ** kwargs )
115
117
if not self .json_encoder and not self .json_default :
116
118
self .json_encoder = JsonEncoder
@@ -120,6 +122,21 @@ def __init__(self, *args, **kwargs):
120
122
self ._required_fields ))
121
123
self ._skip_fields .update (self .reserved_attrs )
122
124
125
+ def _str_to_fn (self , fn_as_str ):
126
+ """
127
+ If the argument is not a string, return whatever was passed in.
128
+ Parses a string such as package.module.function, imports the module
129
+ and returns the function.
130
+
131
+ :param fn_as_str: The string to parse. If not a string, return it.
132
+ """
133
+ if not isinstance (fn_as_str , str ):
134
+ return fn_as_str
135
+
136
+ path , _ , function = fn_as_str .rpartition ('.' )
137
+ module = importlib .import_module (path )
138
+ return getattr (module , function )
139
+
123
140
def parse (self ):
124
141
"""
125
142
Parses format string looking for substitutions
0 commit comments