import re


def escape(text, replace_with=' '):
    """
    Used when passing arbitrary text to logger
    (which passes it to pgsql).
    It leaves string with only characters which are not
    special in postgres:

    https://www.postgresql.org/docs/current/sql-syntax-lexical.html
    """
    escaped_text = re.sub(
        '[^a-zA-Z0-9 \n\.\/\_]',
        replace_with,
        text
    )
    return escaped_text