def quote_logvalue(value):
    """Return a value formatted for use in a logfmt log entry.

    The input is quoted if it contains spaces or quotes; otherwise returned unchanged
    """
    s = str(value)
    if " " in s or '"' in s:
        s = s.replace('"', "\\" + '"')
        return f'"{s}"'
    return s