15
15
# specific language governing permissions and limitations
16
16
# under the License.
17
17
18
- import ecs_logging
19
- import structlog
20
- from unittest import mock
18
+ import json
21
19
from io import StringIO
20
+ from unittest import mock
22
21
23
22
import pytest
23
+ import structlog
24
+
25
+ import ecs_logging
24
26
25
27
26
28
class NotSerializable :
27
29
def __repr__ (self ):
28
30
return "<NotSerializable>"
29
31
30
32
31
- def make_event_dict ():
33
+ @pytest .fixture
34
+ def event_dict ():
32
35
return {
33
36
"event" : "test message" ,
34
37
"log.logger" : "logger-name" ,
@@ -37,20 +40,29 @@ def make_event_dict():
37
40
}
38
41
39
42
40
- def test_conflicting_event_dict ():
43
+ @pytest .fixture
44
+ def event_dict_with_exception ():
45
+ return {
46
+ "event" : "test message" ,
47
+ "log.logger" : "logger-name" ,
48
+ "foo" : "bar" ,
49
+ "exception" : "<stack trace here>" ,
50
+ }
51
+
52
+
53
+ def test_conflicting_event_dict (event_dict ):
41
54
formatter = ecs_logging .StructlogFormatter ()
42
- event_dict = make_event_dict ()
43
55
event_dict ["foo.bar" ] = "baz"
44
56
with pytest .raises (TypeError ):
45
57
formatter (None , "debug" , event_dict )
46
58
47
59
48
60
@mock .patch ("time.time" )
49
- def test_event_dict_formatted (time , spec_validator ):
61
+ def test_event_dict_formatted (time , spec_validator , event_dict ):
50
62
time .return_value = 1584720997.187709
51
63
52
64
formatter = ecs_logging .StructlogFormatter ()
53
- assert spec_validator (formatter (None , "debug" , make_event_dict () )) == (
65
+ assert spec_validator (formatter (None , "debug" , event_dict )) == (
54
66
'{"@timestamp":"2020-03-20T16:16:37.187Z","log.level":"debug",'
55
67
'"message":"test message",'
56
68
'"baz":"<NotSerializable>",'
@@ -80,3 +92,19 @@ def test_can_be_set_as_processor(time, spec_validator):
80
92
'"message":"test message","custom":"key","dot":{"ted":1},'
81
93
'"ecs":{"version":"1.6.0"}}\n '
82
94
)
95
+
96
+
97
+ def test_exception_log_is_ecs_compliant_when_used_with_format_exc_info (
98
+ event_dict_with_exception ,
99
+ ):
100
+ formatter = ecs_logging .StructlogFormatter ()
101
+ formatted_event_dict = json .loads (
102
+ formatter (None , "debug" , event_dict_with_exception )
103
+ )
104
+
105
+ assert (
106
+ "exception" not in formatted_event_dict
107
+ ), "The key 'exception' at the root of a log is not ECS-compliant"
108
+ assert "error" in formatted_event_dict
109
+ assert "stack_trace" in formatted_event_dict ["error" ]
110
+ assert "<stack trace here>" in formatted_event_dict ["error" ]["stack_trace" ]
0 commit comments