# Buggy code
def format_log(ret):
    """
    Format the state into a log message
    """
    msg = ""
    if isinstance(ret, dict):
        # Looks like the ret may be a valid state return
        if "changes" in ret:
            # Yep, looks like a valid state return
            chg = ret["changes"]
            if not chg:
                if ret["comment"]:
                    msg = ret["comment"]
                else:
                    msg = "No changes made for {0[name]}".format(ret)
            elif isinstance(chg, dict):
                if "diff" in chg:
                    if isinstance(chg["diff"], string_types):
                        msg = "File changed:\n{0}".format(chg["diff"])
                if all([isinstance(x, dict) for x in chg.values()]):
                    if all([("old" in x and "new" in x) for x in chg.values()]):
                        # This is the return data from a package install
                        msg = "Installed Packages:\n"
                        for pkg in chg:
                            old = chg[pkg]["old"] or "absent"
                            new = chg[pkg]["new"] or "absent"
                            msg += "{0} changed from {1} to {2}\n".format(pkg, old, new)
            if not msg:
                msg = str(ret["changes"])
            if ret["result"] is True or ret["result"] is None:
                log.info(msg)
            else:
                log.error(msg)
    else:
        # catch unhandled data
        log.info(str(ret))


# Traceback (UnicodeEncodeError)
The minion function caused an exception: Traceback (most recent call last):
File "c:\salt\bin\lib\site-packages\salt\minion.py", line 1146, in _thread_return
return_data = func(*args, **kwargs)
File "c:\salt\bin\lib\site-packages\salt\modules\state.py", line 515, in highstate
whitelist=kwargs.get('whitelist')
File "c:\salt\bin\lib\site-packages\salt\state.py", line 2971, in call_highstate
return self.state.call_high(high)
File "c:\salt\bin\lib\site-packages\salt\state.py", line 2070, in call_high
ret = dict(list(disabled.items()) + list(self.call_chunks(chunks).items()))
File "c:\salt\bin\lib\site-packages\salt\state.py", line 1626, in call_chunks
running = self.call_chunk(low, running, chunks)
File "c:\salt\bin\lib\site-packages\salt\state.py", line 1880, in call_chunk
running[tag] = self.call(low, chunks, running)
File "c:\salt\bin\lib\site-packages\salt\state.py", line 1605, in call
format_log(ret)
File "c:\salt\bin\lib\site-packages\salt\state.py", line 209, in format_log
'{2}\n'.format(pkg, old, new)
UnicodeEncodeError: 'ascii' codec can't encode character u'\xf1' in position 43: ordinal not in range(128)

# Fixed code
def format_log(ret):
    """
    Format the state into a log message
    """
    msg = ""
    if isinstance(ret, dict):
        # Looks like the ret may be a valid state return
        if "changes" in ret:
            # Yep, looks like a valid state return
            chg = ret["changes"]
            if not chg:
                if ret["comment"]:
                    msg = ret["comment"]
                else:
                    msg = "No changes made for {0[name]}".format(ret)
            elif isinstance(chg, dict):
                if "diff" in chg:
                    if isinstance(chg["diff"], string_types):
                        msg = "File changed:\n{0}".format(chg["diff"])
                if all([isinstance(x, dict) for x in chg.values()]):
                    if all([("old" in x and "new" in x) for x in chg.values()]):
                        # This is the return data from a package install
                        msg = "Installed Packages:\n"
                        for pkg in chg:
                            old = chg[pkg]["old"] or "absent"
                            new = chg[pkg]["new"] or "absent"
                            msg += "{0!r} changed from {1!r} to {2!r}\n".format(
                                pkg, old, new
                            )
            if not msg:
                msg = str(ret["changes"])
            if ret["result"] is True or ret["result"] is None:
                log.info(msg)
            else:
                log.error(msg)
    else:
        # catch unhandled data
        log.info(str(ret))