Skip to content

Commit b7007c7

Browse files
authored
fix: include stacktrace for non-error logs (#69)
The Python logging framework allows users to pass an exc_info kwarg to the logging methods, in which case the stacktrace is included even if loggers other than Logger.exception() is used.
1 parent c105ec5 commit b7007c7

File tree

5 files changed

+30
-2
lines changed

5 files changed

+30
-2
lines changed

CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ This project adheres to [Semantic Versioning](http://semver.org/).
55

66
The format is based on [Keep a Changelog](http://keepachangelog.com/).
77

8+
## 4.2.5 - 2023-07-28
9+
10+
### Fixed
11+
12+
- Include stacktrace also for non-error log level if exc_info is present
13+
814
## 4.2.4 - 2022-04-26
915

1016
### Update

sap/cf_logging/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from sap.cf_logging.record.request_log_record import RequestWebRecord
1111
from sap.cf_logging.record.simple_log_record import SimpleLogRecord
1212

13-
__version__ = '4.2.4'
13+
__version__ = '4.2.5'
1414

1515
_SETUP_DONE = False
1616
FRAMEWORK = None

sap/cf_logging/record/simple_log_record.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ def format(self):
7575
'msg': self.getMessage(),
7676
})
7777

78-
if self.levelno == logging.ERROR and self.exc_info:
78+
if self.exc_info:
7979
stacktrace = ''.join(traceback.format_exception(*self.exc_info))
8080
stacktrace = format_stacktrace(stacktrace)
8181
record['stacktrace'] = stacktrace.split('\n')

setup.py

+3
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@
3838
'Programming Language :: Python :: 3.5',
3939
'Programming Language :: Python :: 3.6',
4040
'Programming Language :: Python :: 3.8',
41+
'Programming Language :: Python :: 3.9',
42+
'Programming Language :: Python :: 3.10',
43+
'Programming Language :: Python :: 3.11',
4144
'Topic :: Software Development :: Libraries :: Python Modules',
4245
'Topic :: Software Development',
4346
'Topic :: System :: Logging',

tests/test_job_logging.py

+19
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,25 @@ def test_exception_stacktrace():
6868

6969
assert error == {}
7070
assert 'ZeroDivisionError' in str(log_json['stacktrace'])
71+
assert 'ZeroDivisionError' in log_json["msg"]
72+
73+
74+
def test_exception_stacktrace_info_level():
75+
""" Test exception stacktrace is logged """
76+
cf_logging.init(level=logging.DEBUG)
77+
logger, stream = config_logger('cli.test')
78+
79+
try:
80+
return 1 / 0
81+
except ZeroDivisionError as exc:
82+
logger.info('zero division error', exc_info=exc)
83+
log_json = JSONDecoder().decode(stream.getvalue())
84+
_, error = JsonValidator(JOB_LOG_SCHEMA).validate(log_json)
85+
86+
assert error == {}
87+
assert 'ZeroDivisionError' in str(log_json['stacktrace'])
88+
assert 'ZeroDivisionError' in log_json["msg"]
89+
7190

7291
def test_custom_fields_set():
7392
""" Test custom fields are set up """

0 commit comments

Comments
 (0)