1
1
import uuid
2
- from datetime import datetime
2
+ import datetime
3
3
4
4
from collections import Mapping , Sequence
5
5
6
6
from .utils import exceptions_from_error_tuple
7
7
from ._compat import text_type
8
8
9
9
10
- def datetime_to_json (dt ):
10
+ def _datetime_to_json (dt ):
11
11
return dt .strftime ("%Y-%m-%dT%H:%M:%SZ" )
12
12
13
13
@@ -17,7 +17,7 @@ class Event(Mapping):
17
17
def __init__ (self , data = {}):
18
18
self ._data = {
19
19
"event_id" : uuid .uuid4 ().hex ,
20
- "timestamp" : datetime_to_json ( datetime .utcnow () ),
20
+ "timestamp" : datetime .datetime . utcnow (),
21
21
"level" : "error" ,
22
22
}
23
23
@@ -124,6 +124,16 @@ def strip_frame(frame):
124
124
return frame , ({"vars" : meta } if meta is not None else None )
125
125
126
126
127
+ def convert_types (obj ):
128
+ if isinstance (obj , (datetime .datetime , datetime .date )):
129
+ return _datetime_to_json (obj )
130
+ if isinstance (obj , Mapping ):
131
+ return {k : convert_types (v ) for k , v in obj .items ()}
132
+ if isinstance (obj , Sequence ) and not isinstance (obj , (text_type , bytes )):
133
+ return [convert_types (v ) for v in obj ]
134
+ return obj
135
+
136
+
127
137
def strip_databag (obj , remaining_depth = 20 ):
128
138
assert not isinstance (obj , bytes ), "bytes should have been normalized before"
129
139
if remaining_depth <= 0 :
0 commit comments