def test_nop_no_args(self): """ If no positional args are passed, nothing happens. """ formatter = PositionalArgumentsFormatter() assert {} == formatter(None, None, {})
def test_formats_tuple(self): """ Positional arguments as simple types are rendered. """ formatter = PositionalArgumentsFormatter() event_dict = formatter(None, None, {'event': '%d %d %s', 'positional_args': (1, 2, 'test')}) assert '1 2 test' == event_dict['event'] assert 'positional_args' not in event_dict
def test_args_removed_if_empty(self): """ If remove_positional_args is True and positional_args is (), still remove them. Regression test for https://github.com/hynek/structlog/issues/82. """ formatter = PositionalArgumentsFormatter() assert {} == formatter(None, None, {"positional_args": ()})
def test_formats_dict(self): """ Positional arguments as dict are rendered. """ formatter = PositionalArgumentsFormatter() event_dict = formatter(None, None, {'event': '%(foo)s bar', 'positional_args': ( {'foo': 'bar'},)}) assert 'bar bar' == event_dict['event'] assert 'positional_args' not in event_dict
def test_positional_args_retained(self): """ Positional arguments are retained if remove_positional_args argument is set to False. """ formatter = PositionalArgumentsFormatter(remove_positional_args=False) positional_args = (1, 2, 'test') event_dict = formatter( None, None, {'event': '%d %d %s', 'positional_args': positional_args}) assert 'positional_args' in event_dict assert positional_args == event_dict['positional_args']
def test_formats_dict(self): """ Positional arguments as dict are rendered. """ formatter = PositionalArgumentsFormatter() event_dict = formatter( None, None, {"event": "%(foo)s bar", "positional_args": ({"foo": "bar"},)}, ) assert "bar bar" == event_dict["event"] assert "positional_args" not in event_dict
def test_formats_tuple(self): """ Positional arguments as simple types are rendered. """ formatter = PositionalArgumentsFormatter() event_dict = formatter( None, None, {"event": "%d %d %s", "positional_args": (1, 2, "test")}, ) assert "1 2 test" == event_dict["event"] assert "positional_args" not in event_dict
def test_positional_args_retained(self): """ Positional arguments are retained if remove_positional_args argument is set to False. """ formatter = PositionalArgumentsFormatter(remove_positional_args=False) positional_args = (1, 2, "test") event_dict = formatter( None, None, {"event": "%d %d %s", "positional_args": positional_args}, ) assert "positional_args" in event_dict assert positional_args == event_dict["positional_args"]
def getLogger(name): return wrap_logger( logging.getLogger(name), processors=[ filter_by_level, add_logger_name, add_caller_info, #local_var_info, unorder_dict, TimeStamper(fmt="ISO", utc=False), format_exc_info, PositionalArgumentsFormatter(), alternate_dev_formatter() ], wrapper_class=BoundLogger, )
def setup(sentry: str, debug: bool = False) -> None: processors = [ filter_by_level, add_log_level, add_logger_name, PositionalArgumentsFormatter(), StackInfoRenderer(), format_exc_info, UnicodeDecoder(), ] configure( logger_factory=LoggerFactory(), wrapper_class=BoundLogger, cache_logger_on_first_use=True, ) if debug: styles = ConsoleRenderer.get_default_level_styles() styles["debug"] = DIM processors += [ TimeStamper(fmt="%Y-%m-%d %H:%M:%S"), ConsoleRenderer(level_styles=styles), ] else: handler = StreamHandler() formatter = CustomJsonFormatter("%(levelname)s %(name)s %(message)s") handler.setFormatter(formatter) for module in ("tornado", "tortoise", "aiomysql"): getLogger(module).addHandler(handler) sentry_logging = LoggingIntegration(level=INFO, event_level=ERROR) init(sentry, integrations=[sentry_logging]) processors.append(JSONRenderer()) handler = StreamHandler() configure(processors=processors) log = get_logger("api") log.addHandler(handler) log.propagate = False log.setLevel(DEBUG if debug else INFO)