twisted / twisted/klein

Using Klein with Twisted.logger

Open
#405 2 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Python
Stars
838
Forks
123
Avg merge
7h 58m
Merged PRs (30d)
12

Description

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"

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the minimal representative example in the issue and trace how Klein and Twisted logger observers format the request output. Determine whether Klein exposes a configurable formatter; done means the application can avoid the duplicated Twisted and Klein formatting while preserving the request log.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
backend, observability
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.