def log_first_n_times()

in hugegraph-python-client/src/pyhugegraph/utils/log.py [0:0]


def log_first_n_times(level, message, n=1, *, logger_name=None, key="caller"):
    """
    Log only for the first n times.

    Args:
        logger_name (str): name of the logger to use. Will use the caller's module by default.
        key (str or tuple[str]): the string(s) can be one of "callers" or
            "message", which defines how to identify duplicated logs.
            For example, if called with `n=1, key="caller"`, this function
            will only log the first call from the same caller, regardless of
            the message content.
            If called with `n=1, key="message"`, this function will log the
            same content only once, even if they are called from different places.
            If called with `n=1, key=("caller", "message")`, this function
            will not log only if the same caller has logged the same message before.
    """
    if isinstance(key, str):
        key = (key,)
    assert len(key) > 0

    caller_module, caller_key = _identify_caller()
    hash_key = ()
    if "caller" in key:
        hash_key = hash_key + caller_key
    if "message" in key:
        hash_key = hash_key + (message,)

    LOG_COUNTER[hash_key] += 1
    if LOG_COUNTER[hash_key] <= n:
        logging.getLogger(logger_name or caller_module).log(level, message)