Esempio n. 1
0
def format_exc_info(logger, name, event_dict):
    """
    Replace an `exc_info` field by an `exception` string field:

    If *event_dict* contains the key ``exc_info``, there are two possible
    behaviors:

    - If the value is a tuple, render it into the key ``exception``.
    - If the value true but no tuple, obtain exc_info ourselves and render
      that.

    If there is no ``exc_info`` key, the *event_dict* is not touched.
    This behavior is analogue to the one of the stdlib's logging.

    >>> from structlog.processors import format_exc_info
    >>> try:
    ...     raise ValueError
    ... except ValueError:
    ...     format_exc_info(None, None, {'exc_info': True})# doctest: +ELLIPSIS
    {'exception': 'Traceback (most recent call last):...
    """
    exc_info = event_dict.pop('exc_info', None)
    if exc_info:
        if not isinstance(exc_info, tuple):
            exc_info = sys.exc_info()
        event_dict['exception'] = _format_exception(exc_info)
    return event_dict
Esempio n. 2
0
 def test_formats(self, exc_info):
     """
     The passed exc_info is formatted.
     """
     assert _format_exception(exc_info).startswith(
         "Traceback (most recent call last):\n"
     )
Esempio n. 3
0
def format_exc_info(logger, name, event_dict):
    """
    Replace an `exc_info` field by an `exception` string field:

    If *event_dict* contains the key ``exc_info``, there are two possible
    behaviors:

    - If the value is a tuple, render it into the key ``exception``.
    - If the value true but no tuple, obtain exc_info ourselves and render
      that.

    If there is no ``exc_info`` key, the *event_dict* is not touched.
    This behavior is analogue to the one of the stdlib's logging.

    >>> from structlog.processors import format_exc_info
    >>> try:
    ...     raise ValueError
    ... except ValueError:
    ...     format_exc_info(None, None, {'exc_info': True})# doctest: +ELLIPSIS
    {'exception': 'Traceback (most recent call last):...
    """
    exc_info = event_dict.pop('exc_info', None)
    if exc_info:
        if not isinstance(exc_info, tuple):
            exc_info = sys.exc_info()
        event_dict['exception'] = _format_exception(exc_info)
    return event_dict
Esempio n. 4
0
 def test_formats(self, exc_info):
     """
     The passed exc_info is formatted.
     """
     assert _format_exception(exc_info).startswith(
         "Traceback (most recent call last):\n"
     )
Esempio n. 5
0
 def __call__(self, logger, name, event_dict):
     exc = event_dict.pop("exception", None)
     if exc is None:
         exc_info = _figure_out_exc_info(event_dict.pop("exc_info", None))
         if exc_info:
             exc = _format_exception(exc_info)
     if exc:
         print(exc, file=self._file)
     return event_dict
Esempio n. 6
0
 def __call__(self, logger, name, event_dict):
     exc = event_dict.pop("exception", None)
     if exc is None:
         exc_info = _figure_out_exc_info(event_dict.pop("exc_info", None))
         if exc_info:
             exc = _format_exception(exc_info)
     if exc:
         print(exc, file=self._file)
     return event_dict
Esempio n. 7
0
 def test_no_trailing_nl(self, exc_info, monkeypatch):
     """
     Trailing newlines are snipped off but if the string does not contain
     one nothing is removed.
     """
     from structlog._frames import traceback
     monkeypatch.setattr(traceback, "print_exception",
                         lambda *a: a[-1].write("foo"))
     assert "foo" == _format_exception(exc_info)
Esempio n. 8
0
def log_exception_info(logger, method_name, event_dict):
    """
    Replace an ``exc_info`` field by a ``message`` string field:
    """
    exc_info = event_dict.pop("exc_info", None)
    if exc_info:
        event_dict["message"] = _format_exception(
            _figure_out_exc_info(exc_info))

    return event_dict
Esempio n. 9
0
 def test_no_trailing_nl(self, exc_info, monkeypatch):
     """
     Trailing newlines are snipped off but if the string does not contain
     one nothing is removed.
     """
     from structlog._frames import traceback
     monkeypatch.setattr(
         traceback, "print_exception",
         lambda *a: a[-1].write("foo")
     )
     assert "foo" == _format_exception(exc_info)
Esempio n. 10
0
 def __call__(self, logger, name, event_dict):
     exc = event_dict.pop('exception', None)
     if exc is None:
         exc_info = event_dict.pop('exc_info', None)
         if exc_info:
             if not isinstance(exc_info, tuple):
                 exc_info = sys.exc_info()
             exc = _format_exception(exc_info)
     if exc:
         print(exc, file=self._file)
     return event_dict
Esempio n. 11
0
 def __call__(self, logger, name, event_dict):
     exc = event_dict.pop('exception', None)
     if exc is None:
         exc_info = event_dict.pop('exc_info', None)
         if exc_info:
             if not isinstance(exc_info, tuple):
                 exc_info = sys.exc_info()
             exc = _format_exception(exc_info)
     if exc:
         print(exc, file=self._file)
     return event_dict
Esempio n. 12
0
def format_exc_info(logger, name, event_dict):
    """
    Replace an `exc_info` field by an `exception` string field:

    If *event_dict* contains the key ``exc_info``, there are two possible
    behaviors:

    - If the value is a tuple, render it into the key ``exception``.
    - If the value is an Exception *and* you're running Python 3, render it
      into the key ``exception``.
    - If the value true but no tuple, obtain exc_info ourselves and render
      that.

    If there is no ``exc_info`` key, the *event_dict* is not touched.
    This behavior is analogue to the one of the stdlib's logging.
    """
    exc_info = event_dict.pop("exc_info", None)
    if exc_info:
        event_dict["exception"] = _format_exception(_figure_out_exc_info(exc_info))
    return event_dict
Esempio n. 13
0
def format_exc_info(logger, name, event_dict):
    """
    Replace an `exc_info` field by an `exception` string field:

    If *event_dict* contains the key ``exc_info``, there are two possible
    behaviors:

    - If the value is a tuple, render it into the key ``exception``.
    - If the value is an Exception *and* you're running Python 3, render it
      into the key ``exception``.
    - If the value true but no tuple, obtain exc_info ourselves and render
      that.

    If there is no ``exc_info`` key, the *event_dict* is not touched.
    This behavior is analogue to the one of the stdlib's logging.
    """
    exc_info = event_dict.pop('exc_info', None)
    if exc_info:
        event_dict['exception'] = _format_exception(
            _figure_out_exc_info(exc_info))
    return event_dict
Esempio n. 14
0
 def test_returns_str(self, exc_info):
     """
     Always returns a native string.
     """
     assert isinstance(_format_exception(exc_info), str)
Esempio n. 15
0
 def test_formats(self, exc_info):
     assert _format_exception(exc_info).startswith(
         'Traceback (most recent call last):\n'
     )
Esempio n. 16
0
 def test_returns_str(self, exc_info):
     """
     Always returns a native string.
     """
     assert isinstance(_format_exception(exc_info), str)
Esempio n. 17
0
 def test_returns_str(self, exc_info):
     assert isinstance(_format_exception(exc_info), str)