klein icon indicating copy to clipboard operation
klein copied to clipboard

Using Klein with Twisted.logger

Open aunsbjerg opened this issue 4 years ago • 2 comments

I have an application that is based on twisted and uses klein for a web interface. I am starting the reactor manually, as per https://klein.readthedocs.io/en/latest/examples/alternativerunning.html#example-manually-running-the-reactor and that's working fine. So far, I have used the legacy twisted.python log interface in all my application, but am moving to the new twisted.logger log interface.

The issue I'm facing now is that all log messages from Klein seem to be formatted twice, first with twisted log formatting and then with kleins own formatting. Example: 2020-11-08T10:58:09+0100 [twisted.python.log#info] "127.0.0.1" - - [08/Nov/2020:09:58:09 +0000] ....

Is there any way to change Kleins log formatting?

Minimal representative example:

import sys
from klein import Klein
from twisted.logger import Logger
from twisted.internet import reactor, endpoints
from twisted.web.server import Site
from twisted.logger import (
    Logger,
    textFileLogObserver,
    globalLogPublisher,
    LogLevel,
    LogLevelFilterPredicate,
    FilteringLogObserver,
)

class WebAPI:
    app = Klein()

    def __init__(self):
        endpoint = endpoints.TCP4ServerEndpoint(reactor, 8080)
        endpoint.listen(Site(self.app.resource()))

    @app.route("/", branch=True)
    def index(self, _):
        return "<html>ok</html>"


log_predicate = LogLevelFilterPredicate(LogLevel.debug)
globalLogPublisher.addObserver(
    FilteringLogObserver(
        observer=textFileLogObserver(sys.stdout),   
        predicates=[log_predicate],
    )
)

logger = Logger(__name__)
logger.info("log from main")

api = WebAPI()
reactor.run()

When running the app and accessing index on localhost, the log output is like this:

2020-11-08T10:58:03+0100 [__main__#info] log from main
2020-11-08T10:58:03+0100 [-] Site starting on 8080
2020-11-08T10:58:03+0100 [twisted.web.server.Site#info] Starting factory <twisted.web.server.Site object at 0x7f925bd73d10>
2020-11-08T10:58:09+0100 [twisted.python.log#info] "127.0.0.1" - - [08/Nov/2020:09:58:09 +0000] "GET / HTTP/1.1" 200 15 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.183 Safari/537.36"

aunsbjerg avatar Sep 28 '20 11:09 aunsbjerg

@aunsbjerg is there some code we can look at?

wsanchez avatar Sep 28 '20 17:09 wsanchez

@wsanchez sorry for late reply, updated issue with example.

aunsbjerg avatar Nov 08 '20 10:11 aunsbjerg