Skip to content

Commit dc5c0c7

Browse files
committed
Include exception traceback when logging using logging.exception
Fixes robotframework#3828.
1 parent 47a5a74 commit dc5c0c7

File tree

5 files changed

+34
-7
lines changed

5 files changed

+34
-7
lines changed

atest/resources/atest_resource.robot

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -143,10 +143,14 @@ Get Output File
143143
[Return] ${file}
144144

145145
File Should Contain
146-
[Arguments] ${path} @{expected}
146+
[Arguments] ${path} @{expected} ${count}=None
147147
${exp} = Catenate @{expected}
148148
${file} = Get Output File ${path}
149-
Should Contain ${file} ${exp}
149+
IF not ${count}
150+
Should Contain ${file} ${exp}
151+
ELSE
152+
Should Contain X Times ${file} ${exp} ${count}
153+
END
150154

151155
File Should Not Contain
152156
[Arguments] ${path} @{expected}
@@ -213,8 +217,8 @@ Stderr Should Be Empty
213217
Should Be Empty ${stderr} Errors in test execution:\n${stderr}
214218

215219
Stderr Should Contain
216-
[Arguments] @{expected}
217-
File Should Contain ${STDERR_FILE} @{expected}
220+
[Arguments] @{expected} ${count}=None
221+
File Should Contain ${STDERR_FILE} @{expected} count=${count}
218222

219223
Stderr Should Not Contain
220224
[Arguments] @{expected}
@@ -229,8 +233,8 @@ Stderr Should Contain Regexp
229233
File Should Contain Regexp ${STDERR_FILE} @{expected}
230234

231235
Stdout Should Contain
232-
[Arguments] @{expected}
233-
File Should Contain ${STDOUT_FILE} @{expected}
236+
[Arguments] @{expected} ${count}=None
237+
File Should Contain ${STDOUT_FILE} @{expected} count=${count}
234238

235239
Stdout Should Not Contain
236240
[Arguments] @{expected}

atest/robot/test_libraries/logging_with_logging.robot

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,16 @@ Log with custom levels
2828
Check log message ${tc.kws[0].msgs[4]} between warning and error WARN
2929
Check log message ${tc.kws[0].msgs[5]} above error ERROR
3030

31+
Log exception
32+
${tc} = Check test case ${TEST NAME}
33+
${message} = Catenate SEPARATOR=\n
34+
... Error occurred!
35+
... Traceback (most recent call last):
36+
... ${SPACE*2}File "*", line 54, in log_exception
37+
... ${SPACE*4}raise ValueError('Bang!')
38+
... ValueError: Bang!
39+
Check log message ${tc.kws[0].msgs[0]} ${message} ERROR pattern=True
40+
3141
Messages below threshold level are ignored fully
3242
${tc}= Check test case ${TEST NAME}
3343
Should be empty ${tc.kws[0].msgs}
@@ -63,4 +73,4 @@ Logging when timeout is in use
6373
Check log message ${tc.kws[0].msgs[1]} something
6474

6575
Suppress errors from logging module
66-
Stderr Should Not Contain Traceback
76+
Stderr Should Contain Traceback count=1

atest/testdata/test_libraries/LibUsingPyLogging.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,12 @@ def log_with_custom_levels():
4949
logging.log(logging.WARNING+5, 'between warning and error')
5050
logging.log(logging.ERROR*100,'above error')
5151

52+
def log_exception():
53+
try:
54+
raise ValueError('Bang!')
55+
except ValueError:
56+
logging.exception('Error occurred!')
57+
5258
def log_invalid_message():
5359
logging.info(InvalidMessage())
5460

atest/testdata/test_libraries/logging_with_logging.robot

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ Log with custom levels
1515
[Setup] Set log level trace
1616
Log with custom levels
1717

18+
Log exception
19+
Log exception
20+
1821
Messages below threshold level are ignored fully
1922
[Setup] Set log level warn
2023
Log invalid message

src/robot/output/pyloggingconf.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
from contextlib import contextmanager
1717
import logging
18+
import traceback
1819

1920
from robot.utils import get_error_details, unic
2021

@@ -58,6 +59,9 @@ class RobotHandler(logging.Handler):
5859

5960
def emit(self, record):
6061
message, error = self._get_message(record)
62+
if record.exc_info:
63+
tb_lines = traceback.format_exception(*record.exc_info)
64+
message = ''.join([message, '\n'] + tb_lines).rstrip()
6165
method = self._get_logger_method(record.levelno)
6266
method(message)
6367
if error:

0 commit comments

Comments
 (0)