# Buggy code
def make_stderr_observer(
    levels=(LogLevel.warn, LogLevel.error, LogLevel.critical),
    show_source=False,
    format="standard",
    colour=False,
    _file=None,
    _categories=None,
):
    """
    Create an observer which prints logs to L{sys.stderr}.
    """
    if _file is None:
        _file = sys.__stderr__

    if _categories is None:
        from crossbar._log_categories import log_categories as _categories

    @provider(ILogObserver)
    def StandardErrorObserver(event):
        if event["log_level"] not in levels:
            return

        if event.get("log_system", "-") == "-":
            logSystem = "{:<10} {:>6}".format("Controller", os.getpid())
        else:
            logSystem = event["log_system"]

        if show_source and event.get("log_namespace") is not None:
            logSystem += " " + event.get("cb_namespace", event.get("log_namespace", ""))

        if event.get("log_category"):
            format_string = _categories.get(event["log_category"])
            if format_string:
                event = event.copy()
                event["log_format"] = format_string

        if event.get("log_format", None) is not None:
            eventText = formatEvent(event)
        else:
            eventText = ""

        if "log_failure" in event:
            # This is a traceback. Print it.
            eventText = eventText + event["log_failure"].getTraceback()

        if format == "standard":
            FORMAT_STRING = STANDARD_FORMAT
        elif format == "syslogd":
            FORMAT_STRING = SYSLOGD_FORMAT
        elif format == "none":
            FORMAT_STRING = NONE_FORMAT
        else:
            assert False

        if colour:
            # Errors are always red.
            fore = Fore.RED

            eventString = FORMAT_STRING.format(
                startcolour=fore,
                time=formatTime(event["log_time"]),
                system=logSystem,
                endcolour=Fore.RESET,
                text=formatEvent(event),
            )
        else:
            eventString = strip_ansi(
                FORMAT_STRING.format(
                    startcolour="",
                    time=formatTime(event["log_time"]),
                    system=logSystem,
                    endcolour="",
                    text=formatEvent(event),
                )
            )

        print(eventString, file=_file)

    return StandardErrorObserver


# Traceback (exceptions.AttributeError)
Unhandled Error
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/twisted/web/http.py", line 1439, in dataReceived
finishCallback(data[contentLength:])
File "/usr/local/lib/python2.7/dist-packages/twisted/web/http.py", line 1704, in _finishRequestBody
self.allContentReceived()
File "/usr/local/lib/python2.7/dist-packages/twisted/web/http.py", line 1767, in allContentReceived
req.requestReceived(command, path, version)
File "/usr/local/lib/python2.7/dist-packages/twisted/web/http.py", line 768, in requestReceived
self.process()
--- <exception caught here> ---
File "/usr/local/lib/python2.7/dist-packages/twisted/web/server.py", line 183, in process
self.render(resrc)
File "/usr/local/lib/python2.7/dist-packages/twisted/web/server.py", line 234, in render
body = resrc.render(self)
File "/usr/local/lib/python2.7/dist-packages/twisted/web/resource.py", line 250, in render
return m(request)
File "/usr/local/lib/python2.7/dist-packages/crossbar/router/longpoll.py", line 397, in render_POST
self.log.debug("WampLongPoll: creating new session ..")
exceptions.AttributeError: WampLongPollResourceOpen instance has no attribute 'log'

# Fixed code
def make_stderr_observer(
    levels=(LogLevel.warn, LogLevel.error, LogLevel.critical),
    show_source=False,
    format="standard",
    colour=False,
    _file=None,
    _categories=None,
):
    """
    Create an observer which prints logs to L{sys.stderr}.
    """
    if _file is None:
        _file = sys.__stderr__

    if _categories is None:
        from crossbar._log_categories import log_categories as _categories

    @provider(ILogObserver)
    def StandardErrorObserver(event):
        if event["log_level"] not in levels:
            return

        if event.get("log_system", "-") == "-":
            logSystem = "{:<10} {:>6}".format("Controller", os.getpid())
        else:
            logSystem = event["log_system"]

        if show_source and event.get("log_namespace") is not None:
            logSystem += " " + event.get("cb_namespace", event.get("log_namespace", ""))

        if event.get("log_category"):
            format_string = _categories.get(event["log_category"])
            if format_string:
                event = event.copy()
                event["log_format"] = format_string

        if event.get("log_format", None) is not None:
            eventText = formatEvent(event)
        else:
            eventText = ""

        if "log_failure" in event:
            # This is a traceback. Print it.
            eventText = eventText + event["log_failure"].getTraceback()

        if format == "standard":
            FORMAT_STRING = STANDARD_FORMAT
        elif format == "syslogd":
            FORMAT_STRING = SYSLOGD_FORMAT
        elif format == "none":
            FORMAT_STRING = NONE_FORMAT
        else:
            assert False

        if colour:
            # Errors are always red.
            fore = Fore.RED

            eventString = FORMAT_STRING.format(
                startcolour=fore,
                time=formatTime(event["log_time"]),
                system=logSystem,
                endcolour=Fore.RESET,
                text=eventText,
            )
        else:
            eventString = strip_ansi(
                FORMAT_STRING.format(
                    startcolour="",
                    time=formatTime(event["log_time"]),
                    system=logSystem,
                    endcolour="",
                    text=eventText,
                )
            )

        print(eventString, file=_file)

    return StandardErrorObserver