Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using Klein with Twisted.logger #405

Open
aunsbjerg opened this issue Sep 28, 2020 · 2 comments
Open

Using Klein with Twisted.logger #405

aunsbjerg opened this issue Sep 28, 2020 · 2 comments
Labels

Comments

@aunsbjerg
Copy link

aunsbjerg commented Sep 28, 2020

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"
@wsanchez wsanchez added the bug label Sep 28, 2020
@wsanchez
Copy link
Member

@aunsbjerg is there some code we can look at?

@aunsbjerg
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants