Skip to content

Commit

Permalink
Reconstruct full traceback instead of decorator only part. Bump to 4.…
Browse files Browse the repository at this point in the history
…9.1 (#48)

[
  • Loading branch information
penguinolog committed Oct 2, 2018
1 parent 2cd4a09 commit 23a70c7
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 17 deletions.
13 changes: 6 additions & 7 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,7 @@ after_success:
- coveralls

jobs:
fast_finish: true
include:
- stage: test
<<: *python34

- <<: *static_analysis
name: "PyLint"
install:
Expand All @@ -83,9 +79,6 @@ jobs:
script:
- mypy --strict logwrap

- <<: *test_cythonized
<<: *python34

- <<: *code_style_check
name: "PEP8"
install:
Expand All @@ -101,6 +94,12 @@ jobs:
script:
- pydocstyle logwrap

- stage: test
<<: *python34

- <<: *test_cythonized
<<: *python34

- stage: deploy
# This prevents job from appearing in test plan unless commit is tagged:
if: tag IS present
Expand Down
2 changes: 1 addition & 1 deletion logwrap/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"bind_args_kwargs",
)

__version__ = "4.9.0"
__version__ = "4.9.1"
__author__ = "Alexey Stepanov"
__author_email__ = "penguinolog@gmail.com"
__maintainers__ = {
Expand Down
16 changes: 13 additions & 3 deletions logwrap/_log_wrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import functools
import inspect
import logging
import sys
import traceback
import typing

import logwrap as core
Expand Down Expand Up @@ -552,12 +554,20 @@ def _make_exc_record(self, name: str, arguments: str) -> None:
:type name: str
:type arguments: str
"""
exc_info = sys.exc_info()
stack = traceback.extract_stack()
tb = traceback.extract_tb(exc_info[2])
full_tb = stack[:2] + tb # cut decorator and build full traceback
exc_line = traceback.format_exception_only(*exc_info[:2])
# Make standard traceback string
tb_text = "Traceback (most recent call last):\n" + "".join(traceback.format_list(full_tb)) + "".join(exc_line)

self._logger.log( # type: ignore
level=self.exc_level,
msg="Failed: \n{name!r}({arguments})".format(
name=name, arguments=arguments if self.log_call_args_on_exc else ""
msg="Failed: \n{name!r}({arguments})\n{tb_text}".format(
name=name, arguments=arguments if self.log_call_args_on_exc else "", tb_text=tb_text
),
exc_info=True,
exc_info=False,
)

def _get_function_wrapper(self, func: typing.Callable) -> typing.Callable:
Expand Down
17 changes: 11 additions & 6 deletions test/test_log_wrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@
from unittest import mock


class AnyStringWith(str):
def __eq__(self, other):
return self in other


# noinspection PyUnusedLocal,PyMissingOrEmptyDocstring
class TestLogWrap(unittest.TestCase):
def setUp(self):
Expand Down Expand Up @@ -260,8 +265,8 @@ def func():
),
mock.call(
level=logging.WARNING,
msg="Failed: \n'func'()",
exc_info=True
msg=AnyStringWith("Failed: \n'func'()"),
exc_info=False
),
],
log.mock_calls,
Expand Down Expand Up @@ -600,8 +605,8 @@ def func(test_arg1, test_arg2):
),
mock.call(
level=logging.ERROR,
msg="Failed: \n'func'()",
exc_info=True
msg=AnyStringWith("Failed: \n'func'()"),
exc_info=False
),
],
log.mock_calls,
Expand Down Expand Up @@ -635,8 +640,8 @@ def func(test_arg1, test_arg2):
),
mock.call(
level=logging.ERROR,
msg="Failed: \n'func'()",
exc_info=True
msg=AnyStringWith("Failed: \n'func'()"),
exc_info=False
),
],
log.mock_calls,
Expand Down

0 comments on commit 23a70c7

Please sign in to comment.