|
15 | 15 | import logging |
16 | 16 | import os |
17 | 17 | import sys |
| 18 | +import traceback |
18 | 19 | import unittest |
19 | 20 | from unittest.mock import Mock, patch |
20 | 21 |
|
@@ -51,18 +52,33 @@ def test_handler_default_log_level(self): |
51 | 52 |
|
52 | 53 | def test_handler_error_exc_info(self): |
53 | 54 | processor, logger = set_up_test_logging(logging.NOTSET) |
| 55 | + |
| 56 | + class CustomException(Exception): |
| 57 | + pass |
| 58 | + |
| 59 | + try: |
| 60 | + raise CustomException("Custom exception") |
| 61 | + except CustomException as exception: |
| 62 | + exc_info = (type(exception), exception, exception.__traceback__) |
| 63 | + traceback_str = "".join(traceback.format_exception(*exc_info)) |
| 64 | + |
54 | 65 | exc_info_values = [ |
55 | 66 | # Don't know what caused it in my context, so I'm relying on mocks to replicate the behavior. |
56 | 67 | # First the `record.exc_info` becomes a string somehow, then `sys.exc_info` brings the tuple. |
57 | 68 | "Stringified exception", |
58 | | - (None, None, None), |
| 69 | + exc_info, |
59 | 70 | ] |
60 | 71 |
|
61 | 72 | with patch.object(sys, "exc_info", side_effect=exc_info_values): |
62 | 73 | logger.exception("Exception message") # Should not raise exception |
63 | 74 |
|
64 | 75 | assert processor.emit_count() == 1 |
65 | 76 |
|
| 77 | + attributes = processor.log_data_emitted[0].log_record.attributes._dict |
| 78 | + assert attributes["exception.type"] == "CustomException" |
| 79 | + assert attributes["exception.message"] == str(exc_info[1]) |
| 80 | + assert attributes["exception.stacktrace"] == traceback_str |
| 81 | + |
66 | 82 | def test_handler_custom_log_level(self): |
67 | 83 | processor, logger = set_up_test_logging(logging.ERROR) |
68 | 84 |
|
|
0 commit comments