def journald_json_formatter(output_file):
    """
    Create an output handler which turns journald's export format back into
    Eliot JSON with extra fields to identify the log origin.
    """
    accumulated = {}

    # XXX Factoring the parsing code separately from the IO would make this
    # whole thing nicer.
    def handle_output_line(line):
        if line:
            key, value = line.split(b"=", 1)
            accumulated[key] = value
        else:
            if accumulated:
                raw_message = accumulated.get(b"MESSAGE", b"{}")
                try:
                    message = json.loads(raw_message)
                except __HOLE__:
                    # Docker log messages are not JSON
                    message = dict(message=raw_message)

                message[u"_HOSTNAME"] = accumulated.get(
                    b"_HOSTNAME", b"<no hostname>"
                )
                message[u"_PROCESS_NAME"] = accumulated.get(
                    b"_SYSTEMD_UNIT", b"<no unit>"
                )
                output_file.write(json.dumps(message) + b"\n")
                accumulated.clear()
    return handle_output_line
dataset/ETHPy150Open ClusterHQ/flocker/admin/acceptance.py/journald_json_formatter