import time


def logging_timeit(logger, format_str, result_funcs=None):
    """
    Timeit logging decorator with argument-based format string

    :param logger: info-level logger
    :param format_str: format string for logger, use execution time as "_time" format argument
    :param result_funcs: dict of "format argument name / function of result" pairs
    """
    result_funcs = result_funcs or {}

    def decorator(func):
        def wrapper(*args, **kwargs):
            started_time = time.time()

            result = func(*args, **kwargs)
            result_args = {name: res_func(result) for name, res_func in result_funcs.items()}

            execution_time = time.time() - started_time

            kwargs.update(result_args)
            logger.info(format_str.format(*args, _time=execution_time, **kwargs))

            return result
        return wrapper
    return decorator