コード例 #1
0
ファイル: test_stdlib.py プロジェクト: rasto2211/structlog
    def test_formatter_unsets_stack_info(self, configure_for_pf, capsys, keep):
        """
        Stack traces doesn't get printed outside of the json document when
        keep_stack_info are set to False but preserved if set to True.
        """
        configure_logging(None)
        logger = logging.getLogger()

        formatter = ProcessorFormatter(
            processor=JSONRenderer(),
            keep_stack_info=keep,
            keep_exc_info=keep,
            foreign_pre_chain=[],
        )
        logger.handlers[0].setFormatter(formatter)

        logging.getLogger().warning("have a stack trace", stack_info=True)

        out, err = capsys.readouterr()

        assert "" == out

        if keep is False:
            assert 1 == err.count("Stack (most recent call last):")
        else:
            assert 2 == err.count("Stack (most recent call last):")
コード例 #2
0
ファイル: test_stdlib.py プロジェクト: xyzlat/structlog
    def test_other_handlers_get_original_record(
        self, configure_for_pf, capsys
    ):
        """
        Logging handlers that come after the handler with ProcessorFormatter
        should receive original, unmodified record.
        """
        configure_logging(None)

        handler1 = logging.StreamHandler()
        handler1.setFormatter(ProcessorFormatter(JSONRenderer()))
        handler2 = type("", (), {})()
        handler2.handle = call_recorder(lambda record: None)
        handler2.level = logging.INFO
        logger = logging.getLogger()
        logger.addHandler(handler1)
        logger.addHandler(handler2)

        logger.info("meh")

        assert 1 == len(handler2.handle.calls)

        handler2_record = handler2.handle.calls[0].args[0]

        assert "meh" == handler2_record.msg
コード例 #3
0
    def test_formatter_unsets_exc_info(self, configure_for_pf, capsys, keep):
        """
        Stack traces doesn't get printed outside of the json document when
        keep_exc_info are set to False but preserved if set to True.
        """
        configure_logging(None)
        logger = logging.getLogger()

        def format_exc_info_fake(logger, name, event_dict):
            event_dict = collections.OrderedDict(event_dict)
            del event_dict["exc_info"]
            event_dict["exception"] = "Exception!"
            return event_dict

        formatter = ProcessorFormatter(
            processor=JSONRenderer(),
            keep_stack_info=keep,
            keep_exc_info=keep,
            foreign_pre_chain=[format_exc_info_fake],
        )
        logger.handlers[0].setFormatter(formatter)

        try:
            raise RuntimeError("oh noo")
        except Exception:
            logging.getLogger().exception("seen worse")

        out, err = capsys.readouterr()
        assert "" == out
        if keep is False:
            assert (
                '{"event": "seen worse", "exception": "Exception!"}\n') == err
        else:
            assert "Traceback (most recent call last):" in err
コード例 #4
0
 def wrap_for_process_formatter(
         logger: logging.Logger, name: str,
         event_dict: Dict[str,
                          Any]) -> Tuple[Tuple[Any, ...], Dict[str, Any]]:
     """
     Replacement for structlog.stdlib.ProcessorFormatter.wrap_for_formatter that passes on _exc_info as
     exc_info to LogRecord.__init__
     """
     args, kwargs = ProcessorFormatter.wrap_for_formatter(
         logger=logger, name=name, event_dict=event_dict)
     event_dict = args[0] if args else kwargs.get('event_dict')
     if event_dict:
         kwargs['exc_info'] = event_dict.pop('_exc_info', None)
     return args, kwargs