def LogMaxFilter(level, inclusive=True):
    """
    Create a level-based filter that caps the maximum allowed log level.

    Levels can be compared either exclusively or inclusively to the
    threshold.

    This method returns a comparison function rather than an object with
    a ``filter`` method, so it is not compatible with `logging` before
    Python 3.2.
    """
    comparator = level.__ge__ if inclusive else level.__gt__

    def filter(log_record):
        return comparator(log_record.levelno)
    return filter
