from typing import Union
import logging


def _level_to_int(level: Union[str, int]) -> int:
    """Convert a log level in str or int into an int.

    Args:
        level: Can be a string or int

    Returns:
        level as int.
    """
    if not isinstance(level, int):
        level = logging.getLevelName(level.upper())

    # In rare cases it might be false, but it shouldn't generally happen.
    if not isinstance(level, int):
        raise ValueError(f"Unknown level {level!r}.")

    return level