def _patch_stdout():
    """
    This function re-opens sys.stdout using utf-8 encoding.

    It's to be used in situations where process encoding / locale is not set to utf-8. In such
    situations when unicode sequence is logged, it would cause logging formatter to go in an infite
    loop on formatting a record.

    This function works around that by ensuring sys.stdout is always opened in utf-8 mode.
    """

    stdout_encoding = str(getattr(sys.stdout, "encoding", "none")).lower()

    if stdout_encoding not in ["utf8", "utf-8"] and PATCH_STDOUT:
        LOG.info(
            "Patching sys.stdout and re-opening it with utf-8 encoding (originally opened "
            "with %s encoding)..." % (stdout_encoding)
        )
        sys.stdout = open(
            sys.stdout.fileno(),
            mode="w",
            encoding="utf-8",
            errors="replace",
            buffering=1,
        )

    This function re-opens sys.stdout using utf-8 encoding.

    It's to be used in situations where process encoding / locale is not set to utf-8. In such
    situations when unicode sequence is logged, it would cause logging formatter to go in an infite
    loop on formatting a record.

    This function works around that by ensuring sys.stdout is always opened in utf-8 mode.
    
st2common/st2common/log.py